~danyellawson/pypad/pypad

« back to all changes in this revision

Viewing changes to pypad.py

  • Committer: Danyel Lawson
  • Date: 2013-02-08 03:02:40 UTC
  • Revision ID: danyellawson@gmail.com-20130208030240-fukq0rfmxdxi4pez
Fixed numerous bugs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
<title>
29
29
pypad 
30
30
</title>
 
31
<style>
 
32
p a {margit-right:10;padding-right:10;}
 
33
</style>
31
34
</head>
32
35
<body>
33
36
<p><h1>pypad - the notepad webserver</h1></p>
67
70
        form.setAttribute("action", "/note/%s" % note["id"])
68
71
        body.appendChild(form)
69
72
        form.appendChild(wrap_tag(doc, doc.createTextNode("id")))
70
 
        id = doc.createElement("input")
71
 
        id.setAttribute("title", "id")
72
 
        #id.setAttribute("type", "hidden")
73
 
        id.setAttribute("readonly", "true")
74
 
        id.setAttribute("disabled", "true")
75
 
        id.setAttribute("name", "id")
76
 
        id.setAttribute("value", note["id"])
77
 
        form.appendChild(wrap_tag(doc, id))
 
73
        note_id = doc.createElement("input")
 
74
        note_id.setAttribute("title", "id")
 
75
        #note_id.setAttribute("type", "hidden")
 
76
        note_id.setAttribute("readonly", "true")
 
77
        note_id.setAttribute("disabled", "true")
 
78
        note_id.setAttribute("name", "id")
 
79
        note_id.setAttribute("value", note["id"])
 
80
        form.appendChild(wrap_tag(doc, note_id))
78
81
        form.appendChild(wrap_tag(doc, doc.createTextNode("title")))
79
82
        title = doc.createElement("input")
80
83
        title.setAttribute("title", "title")
112
115
                body.appendChild(row)
113
116
                hover = 'edit "%s"' % notes[note]["title"]
114
117
                path = "/note/%s" % notes[note]["id"]
115
 
                id = doc.createElement("a")
116
 
                id.setAttribute("title", hover)
117
 
                id.setAttribute("href", path)
118
 
                row.appendChild(id)
119
 
                id.appendChild(doc.createTextNode(notes[note]["id"]))
 
118
                note_id = doc.createElement("a")
 
119
                note_id.setAttribute("title", hover)
 
120
                note_id.setAttribute("href", path)
 
121
                row.appendChild(note_id)
 
122
                note_id.appendChild(doc.createTextNode(notes[note]["id"]))
120
123
                title = doc.createElement("a")
121
124
                title.setAttribute("title", hover)
122
125
                title.setAttribute("href", path)
148
151
        root = tree and tree.getroot() or get_tree().getroot()
149
152
        notes = {}
150
153
        for note in root.findall("note"):
151
 
                id = note.find("id").text
 
154
                note_id = note.find("id").text
152
155
                title = note.find("title").text or ""
153
156
                text = note.find("text").text or ""
154
 
                notes[id] = {"id":id, "title":title, "text":text}
 
157
                notes[note_id] = {"id":note_id, "title":title, "text":text}
155
158
        return notes
156
159
 
157
160
 
179
182
                                web_page = note_from_template(notes[path[1]])
180
183
                        else: 
181
184
                                root = tree.getroot()
182
 
                                new_id = root.get("next_id")
 
185
                                new_id = root.get("next_id") or "0"
183
186
                                root.set("next_id", str(int(new_id) + 1))
184
187
                                note = xml.etree.ElementTree.SubElement(root, "note")
185
 
                                id = xml.etree.ElementTree.SubElement(note, "id")
186
 
                                id.text = new_id
 
188
                                note_id = xml.etree.ElementTree.SubElement(note, "id")
 
189
                                note_id.text = new_id
187
190
                                xml.etree.ElementTree.SubElement(note, "title")
188
191
                                xml.etree.ElementTree.SubElement(note, "text")
189
192
                                tree.write("pypad.xml", encoding="utf-8")
208
211
                path = [part for part in path.split("/") if part]
209
212
                if len(path) == 2 and path[0] == "note" and path[1] in notes:
210
213
                        for note in root.getiterator("note"):
211
 
                                id = note.find("id")
212
 
                                if id.text == path[1]:
 
214
                                note_id = note.find("id")
 
215
                                if note_id.text == path[1]:
213
216
                                        form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type']})
214
217
                                        title = note.find("title")
215
 
                                        notes[id.text]["title"] = title.text = form.getvalue("title", "")
 
218
                                        notes[note_id.text]["title"] = title.text = form.getvalue("title", "")
216
219
                                        text = note.find("text")
217
 
                                        notes[id.text]["text"] = text.text = form.getvalue("text", "")
 
220
                                        notes[note_id.text]["text"] = text.text = form.getvalue("text", "")
218
221
                                        tree.write("pypad.xml", encoding="utf-8")
219
 
                                        web_page = note_from_template(notes[id.text])
 
222
                                        web_page = note_from_template(notes[note_id.text])
220
223
                                        break
221
224
                if web_page is None:
222
225
                        web_page = notes_from_template(notes)
232
235
 
233
236
        web_server = BaseHTTPServer.HTTPServer((address, port), pypadHTTPRequestHandler)
234
237
        while not stop_server:
235
 
                web_server.handle_request()
 
238
                try:
 
239
                        web_server.handle_request()
 
240
                except:
 
241
                        pass
236
242
 
237
243
 
238
244
def sigint_handler(signal, frame):