#!/usr/bin/python2.3 import cgi, sys, os import cgitb; cgitb.enable() import re import cPickle as pickle from htmltmpl import TemplateManager, TemplateProcessor import urllib2 import aasql settings = 'settings' def main(): form = cgi.FieldStorage() print "Content-Type: text/html" print aat = Aat(form) aat.mode() class Aat: def __init__(self, form): self.form = form self.debug = 0 self.fetch = '' self.aa = [] self.createtable = 0 # create table flag def mode(self): """ Figure out if we are going to lookup the query or Fetch the URI if it's in the query Or we need to run setup """ if not os.path.isfile(settings): self.d = { 'HOST': self.form.getvalue("host"), 'DB': self.form.getvalue("db"), 'USER': self.form.getvalue("user"), 'PASSWD': self.form.getvalue("passwd"), } pickle.dump(self.d, file(settings, 'wb'), pickle.HIGHEST_PROTOCOL) self.createtable = 1 try: self.sql = aasql.aasql() if self.createtable: self.sql.createtable() self.ui() except aasql.AasqlError,error: self.setup(error) def setup(self, error): template = TemplateManager().prepare("setup.tmpl") tproc = TemplateProcessor() self.host = self.form.getvalue("host", "localhost") tproc.set("host", self.host) self.user = self.form.getvalue("user", "") tproc.set("user", self.user) self.passwd = self.form.getvalue("passwd", "") tproc.set("passwd", self.passwd) self.db = self.form.getvalue("db", "aat") tproc.set("db", self.db) tproc.set("error", str(error)) if os.path.isfile(settings): submit = """ Settings exist so will have to delete the file by hand, before submitting this. e.g. rm %s """ % os.path.abspath(settings) else: submit = "Save settings" tproc.set("submit", str(submit)) print tproc.process(template) def ui(self): self.query = self.form.getvalue("q", "") template = TemplateManager().prepare("basic.tmpl") tproc = TemplateProcessor() tproc.set("query", self.query) if not self.query: tproc.set("type", "Random") tproc.set("Abbr", [r for r in [self.sql.random()] if r]) elif self.VALID_URI(): # HTTP fetch contents of query if not self.fetch: self.geturi() # Find AAs if not self.aa: self.getaa() if self.fetch and not self.aa: m = re.search('(.*?)', self.fetch, re.S) expand = '' if m: expand = re.sub(r'\b([A-Z]{2,5})\b', self.lookup , m.group(1)) print expand sys.exit(0) for i in self.aa: # If not in the databaes if not self.sql.check(i["acronym"], i["definition"]): # Mark as new i["new"] = 1 # Set title to mode tproc.set("type", "URI Fetch") # Show AA tproc.set("Abbr", self.aa) # Show fetched tproc.set("fetch", self.fetch) self.sql.insert([i for i in self.aa if "new" in i]) else: tproc.set("type", "Search") results = self.sql.query(self.query) ids = [i["id"] for i in results] for r in self.sql.search(self.query): if r["id"] not in ids: results.append(r) tproc.set("Abbr", results) #tproc.set("Abbr", a.query(self.query) a.search(self.query)) print tproc.process(template) def lookup(self, m): expand = self.sql.query(m.group(1)) if expand: expand = list(expand)[0] expand = dict(expand) if 'definition' in expand: expand = expand['definition'] if not expand: expand = "Unknown Acronym :/" return '%s' % (expand, m.group(1)) def geturi(self): try: f = urllib2.urlopen(self.query) self.fetch = f.read() except: pass def getaa(self): for match in re.findall(r"""<(acronym|abbr) title="(.*?)">(.*?)""", self.fetch): match = {"acronym": match[2], "definition": match[1], "source": self.query, "poster": cgi.os.environ['REMOTE_ADDR']} if match not in self.aa: self.aa.append(match) def VALID_URI(self): if re.search("^http:\/\/", self.query, re.I): return 1 if __name__ == "__main__": main()