import appuifw import anydbm import e32 import re import key_codes print """ wikkit Copyright 2005, Christopher Schmidt MIT License, Share and Enjoy """ r_endword = re.compile(r'[A-Za-z0-9]+$') r_startword = re.compile(r'^[A-Za-z0-9]+') class Application: def getword(self, input, pos): """From Sean B. Palmer, http://inamidst.com/sbp/""" try: eword = r_endword.search(input[:pos]).group(0) except AttributeError: eword = '' try: sword = r_startword.match(input[pos:]).group(0) except AttributeError: sword = '' if not (eword or sword): return None return eword + sword def upload(self): import urllib d = {} for i in self.db.keys(): d[i] = self.db[i].replace(u'\u2029', "\n\n") s = urllib.urlencode(d) uri = appuifw.query(u"URI to POST to?", "text") u = urllib.urlopen("%s"%uri, data=s) appuifw.note(u"Uploaded data.", "info") def open_page(self, page): """Change the current page, saving the text and opening a new one.""" self.db[self.current_page] = appuifw.app.body.get() if not self.history: self.old_page.append(self.current_page) self.history = 0 self.current_page = page if self.current_page in self.db: appuifw.app.body.set(self.db[self.current_page]) else: appuifw.app.body.set(u"") self.db.sync() appuifw.app.title=u"%s"%page def callback_new(self): """Open a new wiki page.""" new_page = appuifw.query(u"Title?", "text") self.open_page(new_page) def callback_save(self): """Save the current page, and sync.""" self.db[self.current_page] = appuifw.app.body.get() self.db.sync() def __init__(self): """Open an anydbm file, open the "main" wiki page.""" self.db = anydbm.open("C:\\wikidb", "c") self.current_page = "main" self.old_page = [] self.history = 0 if not u'main' in self.db: self.db[u'main'] = u"Main Page" self.orig_menu = appuifw.app.menu appuifw.app.menu = [(u"New", self.callback_new), (u"Open", self.launch_list), (u"Save",self.callback_save), (u"Upload to Web",self.upload), (u"Exit", self.callback_exit)] self.orig_exit = appuifw.app.exit_key_handler appuifw.app.exit_key_handler=self.callback_exit appuifw.app.body = appuifw.Text(self.db[u'main']) self.old_title = appuifw.app.title appuifw.app.title = u"main" appuifw.app.body.bind(key_codes.EKeyDevice3,self.callback_key) appuifw.app.body.bind(key_codes.EKeyYes,self.callback_back) self.lock = e32.Ao_lock() self.lock.wait() def callback_back(self): if self.old_page: self.history = 1 self.open_page(self.old_page.pop()) def callback_key(self): i = self.getword(appuifw.app.body.get(), appuifw.app.body.get_pos()) if i: self.open_page(i) def callback_exit(self): """Cleanup after ourselves.""" self.callback_save() appuifw.app.exit_key_handler = self.orig_exit appuifw.app.title = self.old_title self.db.close() self.lock.signal() def launch_list(self): self.text_body = appuifw.app.body appuifw.app.body = appuifw.Listbox(self.db.keys(),self.list_switch) def list_switch(self): i = appuifw.app.body.current() appuifw.app.body = self.text_body self.open_page(self.db.keys()[i]) A = Application()