123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- #!/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"/update", UpdateHandler),
- (r"/img", ImageHandler),
- (r"/download", GenFileStreamerHandler),
- ]
- settings = dict(
- template_path=os.path.join(os.path.dirname(__file__), "templates"),
- static_path="/srv/www/katrin/static",
- debug=True,
- )
- tornado.web.Application.__init__(self, handlers, **settings)
- class DATA:
- def __init__(self, index=None):
- self.con = lite.connect('fossil.db')
- cur = self.con.cursor()
- with self.con:
- if index == None:
- cur.execute("SELECT * FROM Fossil ORDER BY Id LIMIT 1")
- else:
- cur.execute("SELECT * FROM Fossil WHERE Id = ?", [index])
- data = cur.fetchone()
-
- if data == None:
- raise Exception('Database: Data Id ' + str(index) + ' does not exist.')
-
- self.index = data[0]
- self.name = data[1]
- parser = MyHTMLParser()
- parser.feed(data[2])
- self.desc = parser.get_data()
- self.size = ("%.2f" % (data[3] / 1000000.0))
- self.left_count = data[4]
- self.top_count = data[5]
- self.front_count = data[6]
- def get_index(self):
- return self.index
-
- def get_name(self):
- return self.name
-
- def get_desc(self):
- return self.desc
-
- def get_size(self):
- return self.size
-
- def get_left_count(self):
- return self.left_count
-
- def get_top_count(self):
- return self.top_count
-
- def get_front_count(self):
- return self.front_count
-
- def slug(self, slug=None):
- if slug:
- self.slug = slug
- else:
- self.slug = "The Fossils Data"
- return self.slug
-
- def title(self, title=None):
- if title:
- self.title = title
- else:
- self.title = """Preservation of three-dimensional
- anatomy in phosphatized fossil arthropods enriches
- evolutionary inference"""
- return self.title
- def get_author(self):
- return [
- "Achim H Schwermann",
- "Tomy dos Santos Rolo",
- "Michael S Caterino",
- "Günter Bechly",
- "Heiko Schmied",
- "Tilo Baumbach",
- "Thomas van de Kamp"
- ]
- def get_institute(self):
- return [
- """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"""
- ]
- def get_list(self):
- with self.con:
- cur = self.con.cursor()
- cur.execute("SELECT * FROM Fossil ORDER BY Id")
- data = cur.fetchall()
- self.data_list = [ [x[0], x[1]] for x in data]
- return self.data_list
- class UpdateHandler(tornado.web.RequestHandler):
- def get(self):
- index = self.get_argument("index", None)
- name = self.get_argument("name", None)
- print index, name
- selected_data = DATA(index)
- self.write({
- "data_size": selected_data.get_size(),
- "desc": selected_data.get_desc(),
- "top_count": selected_data.get_top_count(),
- "left_count": selected_data.get_left_count(),
- "front_count": selected_data.get_front_count()
- })
- class MainHandler(tornado.web.RequestHandler):
- def get(self):
- index = self.get_argument("index", None)
- print index
- data = DATA(index)
-
- self.render(
- "index.html",
- slug = data.slug(),
- index = data.get_index(),
- name = data.get_name(),
- title = data.title(),
- authors = data.get_author(),
- institutes = data.get_institute(),
- data_list = data.get_list(),
- desc = data.get_desc(),
- data_size = data.get_size(),
- left_count = data.get_left_count(),
- top_count = data.get_top_count(),
- front_count = data.get_front_count()
- )
- 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_argument("did")
- print cdid
- 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) + ".zip"
- self.path = os.path.join(
- self.DATA_PATH,
- str(cdid),
- 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
- #DATA_PATH = "/srv/www/katrin/static/fossils_for_nicholas"
- DATA_PATH = "/home/ntj/fossils_for_nicholas"
- @tornado.web.asynchronous
- @tornado.gen.engine
- def get(self):
- did = self.get_argument("id")
- dname = self.get_argument("name")
- dtype = self.get_argument("type")
- dcounter = self.get_argument("counter").zfill(4)
-
- if dtype == "left" or dtype == "left-resized":
- prefix = "left"
- elif dtype == "top" or dtype == "top-resized":
- prefix = "top"
- elif dtype == "front" or dtype == "front-resized":
- prefix = "front"
-
- if dtype == "preview":
- self.path = os.path.join(
- self.DATA_PATH,
- dname,
- str(dname)+".png")
- else:
- self.path = os.path.join(
- self.DATA_PATH,
- dname,
- dtype,
- prefix+str(dcounter)+".png")
- #print did, dname, dtype, dcounter
- #print self.path
- 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()
|