예제 #1
0
파일: http.py 프로젝트: brotchie/keepnote
    def do_POST(self):

        parts, nodeid, filename = self.parse_path()

        content_len = int(self.headers.get("Content-length", 0))
        data = self.rfile.read(content_len)

        try:
            if nodeid == "":
                # pure command                
                if parts.query == "save":
                    self.server.conn.save()
                    
                elif parts.query == "index":
                    query = plist.loads(data)
                    res = self.server.conn.index(query)
                    if hasattr(res, "next"):
                        res = list(res)
                    self.send_response(httplib.OK)
                    self.send_header("content_type", "text/xml")
                    self.end_headers()
                    self.wfile.write(XML_HEADER)
                    self.wfile.write(plist.dumps(res).encode("utf8"))
                    return 

            elif not filename:
                # update node
                attr = plist.loads(data)
                attr["nodeid"] = nodeid
                self.server.conn.update_node(nodeid, attr)

            else:
                # write file
                params = urlparse.parse_qs(parts.query)
                if params.get("mode", "r") == ["a"]:
                    stream = self.server.conn.open_file(nodeid, filename, "a")
                else:
                    stream = self.server.conn.open_file(nodeid, filename, "w")
                stream.write(data)
                stream.close()

            self.send_response(httplib.OK)
            self.send_header("content_type", "text/plain")
            self.end_headers()
            
            
        except Exception, e:
            # FIX response
            keepnote.log_error()
            self.send_error(httplib.NOT_FOUND, "cannot create node: " + str(e))
예제 #2
0
    def do_POST(self):

        parts, nodeid, filename = self.parse_path()

        content_len = int(self.headers.get("Content-length", 0))
        data = self.rfile.read(content_len)

        try:
            if nodeid == "":
                # pure command
                if parts.query == "save":
                    self.server.conn.save()

                elif parts.query == "index":
                    query = plist.loads(data)
                    res = self.server.conn.index(query)
                    if hasattr(res, "next"):
                        res = list(res)
                    self.send_response(httplib.OK)
                    self.send_header("content_type", "text/xml")
                    self.end_headers()
                    self.wfile.write(XML_HEADER)
                    self.wfile.write(plist.dumps(res).encode("utf8"))
                    return

            elif not filename:
                # update node
                attr = plist.loads(data)
                attr["nodeid"] = nodeid
                self.server.conn.update_node(nodeid, attr)

            else:
                # write file
                params = urlparse.parse_qs(parts.query)
                if params.get("mode", "r") == ["a"]:
                    stream = self.server.conn.open_file(nodeid, filename, "a")
                else:
                    stream = self.server.conn.open_file(nodeid, filename, "w")
                stream.write(data)
                stream.close()

            self.send_response(httplib.OK)
            self.send_header("content_type", "text/plain")
            self.end_headers()

        except Exception as e:
            # FIX response
            keepnote.log_error()
            self.send_error(httplib.NOT_FOUND, "cannot create node: " + str(e))
예제 #3
0
    def do_PUT(self):
        """
        PUT action handler
        """

        parts, nodeid, filename = self.parse_path()

        # read attr
        content_len = int(self.headers.get("Content-length", 0))

        try:
            if filename is None:
                # create node
                data = self.rfile.read(content_len)
                attr = plist.loads(data)
                attr["nodeid"] = nodeid
                self.server.conn.create_node(nodeid, attr)

            elif filename.endswith("/"):
                # create dir
                self.server.conn.create_dir(nodeid, filename)

            else:
                # create file
                data = self.rfile.read(content_len)
                stream = self.server.conn.open_file(nodeid, filename, "w")
                stream.write(data)
                stream.close()

            self.send_response(httplib.OK)
            self.send_header("content_type", "text/plain")
            self.end_headers()

        except Exception as e:
            # FIX response
            keepnote.log_error()
            self.send_error(httplib.NOT_FOUND, "cannot create node: " + str(e))
예제 #4
0
파일: http.py 프로젝트: brotchie/keepnote
    def do_PUT(self):
        """
        PUT action handler
        """

        parts, nodeid, filename = self.parse_path()
        
        # read attr
        content_len = int(self.headers.get("Content-length", 0))
        
        try:
            if filename is None:
                # create node
                data = self.rfile.read(content_len)
                attr = plist.loads(data)
                attr["nodeid"] = nodeid
                self.server.conn.create_node(nodeid, attr)
                
            elif filename.endswith("/"):
                # create dir
                self.server.conn.create_dir(nodeid, filename)

            else:
                # create file
                data = self.rfile.read(content_len)
                stream = self.server.conn.open_file(nodeid, filename, "w")
                stream.write(data)
                stream.close()

            self.send_response(httplib.OK)
            self.send_header("content_type", "text/plain")
            self.end_headers()
            
        except Exception, e:
            # FIX response
            keepnote.log_error()
            self.send_error(httplib.NOT_FOUND, "cannot create node: " + str(e))
예제 #5
0
    def test_read_write_string(self):
        data = {
            "version": [1, 0, 3],
            "kind": "nice",
            "measure": 3.03,
            "use_feature": True
        }
        plist_xml = """\
<dict>
    <key>kind</key><string>nice</string>
    <key>version</key><array>
        <integer>1</integer>
        <integer>0</integer>
        <integer>3</integer>
    </array>
    <key>use_feature</key><true/>
    <key>measure</key><real>3.030000</real>
</dict>
"""
        elm = plist.loads(plist_xml)
        self.assertEqual(elm, data)

        text = plist.dumps(data, indent=4)
        self.assertEqual(text, plist_xml)
예제 #6
0
 def loads_data(self, data):
     if self._version == 2:
         return json.loads(data)
     else:
         return plist.loads(data)