Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switch from web.py to tornado

Better the devil you know (and are working with every day anyway)
  • Loading branch information...
commit a8c513b30a0fe5ac965996be4e0b638ff62b29d1 1 parent b6849e9
@danohuiginn authored
View
13 .gitignore
@@ -0,0 +1,13 @@
+.installed.cfg
+bin
+develop-eggs
+dist
+downloads
+eggs
+parts
+*.egg-info
+*pyc
+lib
+doh/private/*py
+doh/private.py
+lib64
View
0  panama/__init__.py
No changes.
View
22 panama/test_regression.py
@@ -0,0 +1,22 @@
+
+import os
+import urllib2
+
+old_hostname = 'http://ohuiginn.net/panama'
+new_hostname = 'http://localhost:8888'
+
+def make_urls(basename):
+ '''return test urls attached to a given server'''
+ datafile = os.path.join(os.path.dirname(__file__), 'testdata/urls_noprefix.txt')
+ urls = open(datafile).readlines()
+ return ['%s%s' % (basename, url) for url in urls if not url.startswith('#')]
+
+def test_urls():
+ urls = zip(*map(make_urls, (old_hostname, new_hostname)))
+ for (old, new) in urls:
+ print('testing %s vs %s' % (old, new))
+ oldpage = urllib2.urlopen(old).read()
+ newpage = urllib2.urlopen(new).read()
+ assert oldpage == newpage
+ print('%s == %s' % (old, new))
+
View
27 panama/testdata/urls.txt
@@ -0,0 +1,27 @@
+/panama
+/panama/
+/panama/personsearch?name=hama
+/panama/person/ELIECER%20TU%C4%82%E2%80%98ON%20JAEN
+/panama/person/JUAN%20CARLOS%20ORTEGA%20CA%C3%B6IZALES
+/panama/company/id/220236
+/panama/company/id/43269
+/panama/person/LIC.%20JUAN%20CARLOS%20MU%C3%83%E2%80%98OZ%20FRANCESCHI%204-272-217
+/panama/company/id/148587
+/panama/company/id/404528
+/panama/person/JUAN%20ANTONIO%20CEDE%C3%83%E2%80%98O%20GONZALEZ
+/panama/person/ELVIN%20EDWIN%20SALDA%C3%83%E2%80%98A%20GONZALEZ
+/panama/company/id/53510
+/panama/person/JEAN%20PIERRE%20RATTON
+/panama/person/EDGARDO%20ELOY%20DIAZ
+/panama/person/LUZMILA%20DOMINGUEZ%20SALDA%C3%83%C2%B6A
+/panama/person/CLAUDIO%20PE%C3%83%C4%B7A
+/panama/person/LIC.JUAN%20CARLOS%20MU%C3%83%E2%80%98OZ%20FRANCESCHI%20CED-4-272-217
+/panama/person/LORENZO%20ACOSTA%20ARAUZ
+/panama/company/id/470503
+/panama/person/LUZ%20MARIA%20GONZALEZ%20SANTOS
+/panama/person/JOSE%20DEL%20CARMEN%20ESPINO
+/panama/person/MANUEL%20HINKE%20JR.
+/panama/person/RAFAEL%20ALBERTO%20SALDA%C3%83%E2%80%98A%20RIVAS
+/panama/person/MARIANO%20PARDO%20DE%20IRIONDO%20BARRIOS
+/panama/person/JUAN%20CARLOS%20PE%C4%82%C5%9BALOZA
+/panama/company/id/721278
View
26 panama/testdata/urls_noprefix.txt
@@ -0,0 +1,26 @@
+/
+/personsearch?name=hama
+/person/JEAN%20PIERRE%20RATTON
+/company/id/148587
+/company/id/220236
+/company/id/404528
+/company/id/43269
+/company/id/470503
+/company/id/53510
+/company/id/721278
+#/person/CLAUDIO%20PE%C3%83%C4%B7A
+#/person/EDGARDO%20ELOY%20DIAZ
+#/person/ELIECER%20TU%C4%82%E2%80%98ON%20JAEN
+#/person/ELVIN%20EDWIN%20SALDA%C3%83%E2%80%98A%20GONZALEZ
+/person/JOSE%20DEL%20CARMEN%20ESPINO
+#/person/JUAN%20ANTONIO%20CEDE%C3%83%E2%80%98O%20GONZALEZ
+/person/JUAN%20CARLOS%20ORTEGA%20CA%C3%B6IZALES
+#/person/JUAN%20CARLOS%20PE%C4%82%C5%9BALOZA
+#/person/LIC.%20JUAN%20CARLOS%20MU%C3%83%E2%80%98OZ%20FRANCESCHI%204-272-217
+#/person/LIC.JUAN%20CARLOS%20MU%C3%83%E2%80%98OZ%20FRANCESCHI%20CED-4-272-217
+/person/LORENZO%20ACOSTA%20ARAUZ
+/person/LUZ%20MARIA%20GONZALEZ%20SANTOS
+#/person/LUZMILA%20DOMINGUEZ%20SALDA%C3%83%C2%B6A
+/person/MANUEL%20HINKE%20JR.
+/person/MARIANO%20PARDO%20DE%20IRIONDO%20BARRIOS
+#/person/RAFAEL%20ALBERTO%20SALDA%C3%83%E2%80%98A%20RIVAS
View
17 panama/testweb.py
@@ -0,0 +1,17 @@
+#!/usr/bin/python
+
+import web
+
+urls = ("/?.*", "hello")
+
+class hello:
+ def GET(self):
+ #web.header('Content-Type', 'text/html')
+ html = '<html><h2>Temporarily Unavailable</h2> '
+ html += '</html>'
+ return html
+
+application = web.application(urls, globals()).wsgifunc()
+#web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
+#if __name__ == "__main__":
+# app.run()
View
154 panama/webtorn.py
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+import tornado.web
+import tornado.ioloop
+
+
+import urllib
+from urlparse import parse_qs
+from panama.settings import basedir
+BADDATA = 'DATA COULD NOT BE SCRAPED'
+import traceback
+from datamodel import Person, Company, SQLObjectNotFound
+#database schema
+
+#render = web.templaterender('templates/')
+
+HTMLHEAD = '<html><body>'
+HTMLTAIL = '</body></html>'
+BASEURL = '/panama'
+ABOUTTEXT = """
+<h3>What is this?</h3>
+
+Panama corporate records have recently been made available <a href="https://www.registro-publico.gob.pa/scripts/nwwisapi.dll/conweb/prinpage">online</a>. Unfortunately the official site won't let you search by the name of company directors - a gap which we're filling.
+
+Once you have found a company, you can use the official site to find the complete entry in the Companies Register, and <a href="https://www.registro-publico.gob.pa/RediWeb/default.asp">scans</a> of documents they have submitted.
+"""
+
+def escape_name(name):
+ try:
+ return urllib.quote(name)
+ except Exception:
+ return name
+
+class indexPage(tornado.web.RequestHandler):
+
+ def get(self):
+ html = '<html><h2>Search Panama company records</h2>\
+ <form action="%s/personsearch" method="get">\
+ <b>Name of person</b><input type = "text" name = "name"><br>\
+ <input type="submit" value="Search">\
+ ' % BASEURL
+ html += ABOUTTEXT
+ html += '</html>'
+ self.write(html)
+
+def google(text):
+ return '(<a href="http://www.google.com/search?q=%%22%s%%22">google</a>)' % text
+
+def getarg(fromqs, name):
+ if fromqs:
+ return fromqs
+ else:
+ arguments = parse_qs(self.request.query)
+ return arguments.get(name)[0]
+
+class personPage(tornado.web.RequestHandler):
+
+ def get(self, rawname = u''):
+ html = ''
+ rawname = getarg(rawname, 'name')
+ name = urllib.unquote(rawname).upper()
+ try:
+ record = Person.byName(name)
+ except SQLObjectNotFound:
+ html += 'could not find %s' % name
+ self.write(html)
+ return
+ subscriberships = record.subscriberships
+ directorships = record.directorships
+ agencies = record.agencys
+ html += '<h1>%s</h1>' % name
+ html += '<h2>Director</h2><ul>'
+ for company in directorships:
+ html += '<li><a href="%s/company/id/%s">%s</a></li>' % (BASEURL, company.recordid, company.name)
+ html += '</ul>'
+ html += '<h2>Subscriber</h2><ul>'
+ for company in subscriberships:
+ html += '<li><a href="%s/company/id/%s">%s</a></li>' % (BASEURL, company.recordid, company.name)
+ html += '</ul>'
+ html += '<h2>Agent</h2><ul>'
+ for company in agencies:
+ html += '<li><a href="%s/company/id/%s">%s</a></li>' % (BASEURL, company.recordid, company.name)
+ html += '</ul>'
+ self.write(html)
+
+class companyByNumberPage(tornado.web.RequestHandler):
+
+ def get(self, companyid=u''):
+
+ companyid = getarg(companyid, 'id')
+ try:
+ companyid = int(companyid)
+ record = Company.byRecordid(companyid)
+ except ValueError, SQLObjectNotFound:
+ print 'No company with ID %s' % companyid
+ return
+ subscribers = record.subscribers
+ directors = record.directors
+ html = '<h1>%s</h1>' % record.name
+ companyurl = 'https://www.registro-publico.gob.pa/scripts/nwwisapi.dll/conweb/MESAMENU?TODO=SHOW&ID=%s' % record.recordid
+ html += '<h3><a href="%s">Full Details</a></h3>' % companyurl
+ html += '<h2>Directors</h2><ul>'
+ for director in directors:
+ html += '<li><a href="%s/person/%s">%s</a> %s </li>' % (BASEURL, director.name, director.name, google(director.name))
+ html += '</ul>'
+ html += '<h2>Subscribers</h2><ul>'
+ for subscriber in subscribers:
+ html += '<li><a href="%s/person/%s">%s</a></li>' % (BASEURL, subscriber.name, subscriber.name)
+ html += '</ul>'
+ agentlist = record.agent
+ if len(agentlist):
+ agent = agentlist[0]
+ html += '<h2>Agent</h2>'
+ html += '<ul><li><a href="%s/person/%s">%s</a></li></ul>' % (BASEURL, agent.name, agent.name)
+ if record.registerdate:
+ html += '<h3>Date Registered</h3>'
+ html += record.registerdate.strftime('%F')
+ html += '<h3><a href="https://www.registro-publico.gob.pa/RediWeb/default.asp">Look up complete file</a></h3> </br>'
+ html += '(search for %s as "Numero de Ficha")' % record.recordid
+ self.write(html)
+
+class searchPersonPage(tornado.web.RequestHandler):
+
+ def get(self, rawSearchterm = u''):
+ if rawSearchterm is u'':
+ arguments = parse_qs(self.request.query)
+ rawSearchterm = arguments.get('name')[0]
+ html = HTMLHEAD
+ unquotedTerm = urllib.unquote(rawSearchterm).upper()
+ liketerm = Person.sqlrepr('%%%s%%' % unquotedTerm)
+ sqlquery = "person.name LIKE %s" % liketerm
+ html += '<h2>Search results</h2><h3>searching for %s</h3><ul>' % sqlquery
+ people = Person.select(sqlquery)
+ for thisone in people:
+ html += "<li><a href='%s/person/%s'>%s</a></li>" %(BASEURL, escape_name(thisone.name), thisone.name)
+ html += '</ul>'
+ html += HTMLTAIL
+ self.write(html)
+
+urls = [
+ ('/?', indexPage,),
+ ('/person/(.*)', personPage,),
+ ('/person$', personPage,),
+ ('/company/id/?(.*)', companyByNumberPage),
+ ('/personsearch/?(.*)', searchPersonPage),
+ ('/search/person/?(.*)', searchPersonPage),
+ ]
+for url in urls[:]:
+ newurl = ('/panama' + url[0], url[1])
+ urls.append(newurl)
+
+if __name__ == '__main__':
+ app = tornado.web.Application(urls)
+ app.listen(8090)
+ tornado.ioloop.IOLoop.instance().start()
Please sign in to comment.
Something went wrong with that request. Please try again.