123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- import io
- from PIL import Image
- from HTMLParser import HTMLParser
- import sqlite3 as lite
- import tornado.httpserver
- import tornado.ioloop
- import tornado.options
- import tornado.web
- from tornado.options import define, options
- define("port", default=8000, help="run on the given port", type=int)
- PATH=os.path.dirname(os.path.abspath(__file__))
- class MyHTMLParser(HTMLParser):
- def __init__(self):
- self.reset()
- self.ttype = "p"
- self.fed = []
-
- def handle_starttag(self, tag, attrs):
- self.ttype = str(tag)
- def handle_endtag(self, tag):
- self.ttype = "p"
- def handle_data(self, data):
- self.fed.append({"type": self.ttype,
- "text": data.strip()})
-
- def get_data(self):
- return self.fed
- class Application(tornado.web.Application):
- def __init__(self):
- handlers = [
- (r"/", MainHandler),
- (r"/img", ImageHandler),
- (r"/download", GenFileStreamerHandler),
- ]
- settings = dict(
- template_path=os.path.join(os.path.dirname(__file__), "templates"),
- static_path=os.path.join(os.path.dirname(__file__), "static"),
- debug=True,
- )
- tornado.web.Application.__init__(self, handlers, **settings)
- class MainHandler(tornado.web.RequestHandler):
-
- #def __init__(self):
- # con = lite.connect('fossil.db')
- #with con:
- # cur = con.cursor()
- def get(self):
-
- con = lite.connect('fossil.db')
- with con:
- cur = con.cursor()
- cur.execute("SELECT * FROM Fossil ORDER BY id")
- data = cur.fetchall()
-
- data_list = [ [x[0], x[1]] for x in data]
- current_index = 0
- parser = MyHTMLParser()
- print data[current_index][2]
- parser.feed(data[current_index][2])
- desc = parser.get_data()
-
- data_size = ("%.2f" % (data[current_index][3] / 1000000.0))
-
- self.render(
- "index.html",
- title = """Preservation of three-dimensional anatomy in phosphatized
- fossil arthropods enriches evolutionary inference""",
- author = """Achim H Schwermann, Tomy dos Santos Rolo, Michael S Caterino,
- Günter Bechly, Heiko Schmied, Tilo Baumbach, Thomas van de Kamp""",
- institute = """Steinmann Institute for Geology, Mineralogy and Paleontology,
- University of Bonn, Bonn, Germany; ANKA/Institute for Photon Science and
- Synchrotron Radiation, Karlsruhe Institute of Technology, Eggenstein-Leopoldshafen,
- Germany; Department of Agricultural and Environmental Sciences, Clemson University,
- Clemson, United States; State Museum of Natural History Stuttgart, Stuttgart, Germany;
- Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany;
- Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology,
- Karlsruhe, Germany""",
- data_list = data_list,
- desc = desc,
- data_size = data_size
- )
- class GenFileStreamerHandler(tornado.web.RequestHandler):
- CHUNK_SIZE = 512000
- DATA_PATH = "/home/ntj/fossils_for_nicholas"
-
- @tornado.web.asynchronous
- @tornado.gen.engine
- def get(self):
- cdid = self.get_arguments("did")
- con = lite.connect('fossil.db')
- with con:
- cur = con.cursor()
- cur.execute("SELECT name FROM Fossil WHERE name = ?", (cdid))
- data_key = cur.fetchall()
-
- if len(data_key) > 0:
- file_name = str(cdid[0]) + ".zip"
- self.path = self.DATA_PATH + str(cdid[0]) + "/" + file_name
- self.set_header('Content-Type',
- 'application/octet-stream')
- self.set_header('Content-Disposition',
- 'attachment; filename=' + file_name)
- self.set_header("Content-Length",
- os.path.getsize(self.path))
- self.flush()
- fd = open(self.path, "rb")
- data = fd.read(self.CHUNK_SIZE)
- while data:
- self.write(data)
- yield tornado.gen.Task(self.flush)
- data = fd.read(self.CHUNK_SIZE)
- fd.close()
- self.finish()
- class ImageHandler(tornado.web.RequestHandler):
- CHUNK_SIZE = 512000
- @tornado.web.asynchronous
- @tornado.gen.engine
- def get(self):
- #print self.get_arguments("did")
- self.path = PATH + '/static/data/1.png'
- self.set_header('Content-Type', 'image/png')
- self.set_header("Content-Length",
- os.path.getsize(self.path))
- self.flush()
- fd = open(self.path, "rb")
- data = fd.read(self.CHUNK_SIZE)
- while data:
- self.write(data)
- yield tornado.gen.Task(self.flush)
- data = fd.read(self.CHUNK_SIZE)
- fd.close()
- self.finish()
- if __name__ == "__main__":
- tornado.options.parse_command_line()
- http_server = tornado.httpserver.HTTPServer(Application())
- http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
|