#!/usr/local/bin/python # """A clone of Tropy. Original Tropy was written by Hiroshi Yuki. http://www.hyuki.com/tropy/ http://www.hyuki.com/d/200511.html#i20051103183338 """ # # Copyright (c) 2005 Satoshi Fukutomi . # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # import re import os import cgi import md5 from random import choice from time import time from urlparse import urljoin # # config # your_site = "http://localhost:8000/" your_css = "/common.css" self_url = "/tropy/tropy.cgi" data_dir = "/tmp/tropy" page_size = 8192 # Bytes wait_sec = 5 isid = re.compile(r"[0-9a-f]{32}").search def header(title="", id="", wait=False): print 'Content-Type: text/html; charset=utf-8' print print '' print '' print '' print '' print '%s - FukTropy' % title.strip() print ' ' % your_site print ' ' % your_site print ' ' print ' ' % your_css print '' if wait: print '' print '' print '

Please wait %d seconds.

' % wait_sec else: print '' print '
' print '

' if id: print ' Edit' % (self_url, id) else: print ' Edit' print ' Create' % self_url if id: print ' Permalink' % (self_url, id) else: print ' Permalink' print ' Random' % self_url print '

' print '

%s

' % title def footer(id=""): print '

Powered by FukTropy.

' print '
' print '' print '' class Page: def __init__(self, id="", data=""): """Constructor. You should send id or data. """ if id: self.id = id self.read() try: self.title = self.data[0] self.body = self.data[1:] except IndexError: self.title = "" self.body = [] else: self.id = md5.new("%d %s" % (time(), data)).hexdigest() self.data = data.splitlines(True) self.title = "" self.body = [] def read(self): try: f = file(os.path.join(data_dir, self.id)) self.data = f.readlines() f.close() except IOError: self.data = [] def sync(self): f = file(os.path.join(data_dir, self.id), "wb") for i in self.data: f.write(i) f.close() def remove(self): try: os.remove(os.path.join(data_dir, self.id)) except OSError: pass def update(self, data): if data: self.data = data.splitlines(True) self.sync() else: self.remove() # End of Page def randompage(): ids = os.listdir(data_dir) if ids: return choice(ids) else: return "" def view(id="", wait=False): if not isid(id): id = randompage() if id: page = Page(id=id) if page.title: header(cgi.escape(page.title), id=id, wait=wait) print '

' for line in page.body: line = cgi.escape(line) line = line.replace("\r", "").replace("\n", "
") print line print '

' footer() else: error("No such page.") else: header("FukTropy") footer() def edit(id=""): if isid(id): page = Page(id=id) if page.title: title = page.title body = page.body header(cgi.escape(title), id=id) else: error("No such page.") return else: title = "" id = "" body = [] header("FukTropy") print '

' % self_url print '' print '' % id if title: print '
' else: print '
' print '' print '

' footer() def error(msg=""): if not msg: msg = "Something Wrong" header("ERROR: %s " % msg) print '

%s

' % msg footer() def write(id="", msg=""): if len(msg) > page_size: error("Too large page.") elif isid(id): page = Page(id=id) page.update(msg) if msg: view(id) else: view() elif not id: page = Page(data=msg) page.sync() view(page.id) else: view() def main(): q = os.environ.get("QUERY_STRING", "") if isid(q): id = q else: id = "" form = cgi.FieldStorage() cmd = form.getfirst("cmd") id = form.getfirst("id", id) msg = form.getfirst("msg", "") if cmd == "edit": edit(id) elif cmd == "write": write(id, msg) elif id: view(id) else: view(wait=True) if __name__ == "__main__": main()