Initial commit
[maepadweb.git] / maepadweb.py
blobba971ed16f60f9a16f67c2fa76d4d4aab7bbcdbb
2 import minidb
3 import urlparse
4 import BaseHTTPServer
5 import json
7 class MaePad(object):
8 class Node(object):
9 UNKNOWN, TEXT, SKETCH, CHECKLIST = range(4)
11 class NodeFlag(object):
12 NONE = 0
13 SKETCHLINES, SKETCHGRAPH, WORDRAP = (1 << x for x in range(3))
15 class ChecklistStyle(object):
16 CHECKED, BOLD, STRIKE = (1 << x for x in range(3))
18 class checklists(object):
19 __slots__ = {
20 'idx': int,
21 'nodeid': int,
22 'name': str,
23 'style': int,
24 'color': int,
25 'ord': int,
28 @classmethod
29 def sorted(cls, l):
30 c = lambda a, b: cmp((a.ord, a.idx), (b.ord, b.idx))
31 return sorted(l, c)
34 class nodes(object):
35 __slots__ = {
36 'nodeid': int,
37 'parent': int,
38 'bodytype': int,
39 'name': str,
40 'body': str,
41 'nameblob': str,
42 'bodyblob': str,
43 'lastmodified': int,
44 'ord': int,
45 'flags': int,
48 @classmethod
49 def sorted(cls, l):
50 c = lambda a, b: cmp(a.ord, b.ord)
51 return sorted(l, c)
54 class MaePadServer(BaseHTTPServer.BaseHTTPRequestHandler):
55 db = None
57 def start_output(self, status=200, content_type='text/html'):
58 self.send_response(status)
59 self.send_header('Content-type', content_type)
60 self.end_headers()
62 def send_json(self, data):
63 self.start_output(content_type='text/plain')
64 self.wfile.write(json.dumps(data))
65 self.wfile.close()
67 def send_html(self, data):
68 self.start_output(content_type='text/html')
69 self.wfile.write(data)
70 self.wfile.close()
72 def _nodelist__json(self):
73 self.send_json([(x.nodeid, x.name, x.bodytype) for x in self.db.load(MaePad.nodes)])
75 def do_GET(self):
76 url = urlparse.urlparse(self.path)
77 query = urlparse.parse_qs(url.query)
78 path = filter(None, url.path.split('/'))
80 if len(path) == 0:
81 mode = 'index'
82 elif len(path) == 1:
83 mode = path[0]
84 else:
85 self.start_output(404, 'text/plain')
86 self.wfile.write("404'd!")
87 self.wfile.close()
89 if mode == 'index':
90 self.send_html('<h1>yo</h1>')
91 else:
92 attr_name = '_' + mode.replace('.', '__')
93 if hasattr(self, attr_name):
94 getattr(self, attr_name)()
97 #if __name__ == '__main__':
98 # db = minidb.Store('memos.db')
99 # for node in MaePad.nodes.sorted(db.load(MaePad.nodes)):
100 # print node.name
101 # if node.bodytype == MaePad.Node.CHECKLIST:
102 # for item in MaePad.checklists.sorted(
103 # db.load(MaePad.checklists, nodeid=node.nodeid)):
104 # if item.style == MaePad.ChecklistStyle.CHECKED:
105 # print ' XX ', item.name
106 # else:
107 # print ' ', item.name
109 MaePadServer.db = minidb.Store('memos.db')
111 server = BaseHTTPServer.HTTPServer(('', 8888), MaePadServer)
112 server.serve_forever()