Browse Source

added a DATA object.

Signed-off-by: nicholas <nicholas.jerome@kit.edu>
root 6 years ago
parent
commit
039e23e04f
2 changed files with 230 additions and 3 deletions
  1. 219 0
      main.py
  2. 11 3
      templates/index.html

+ 219 - 0
main.py

@@ -0,0 +1,219 @@
+#!/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="/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))
+
+    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 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 MainHandler(tornado.web.RequestHandler):
+
+    def get(self):
+        data = DATA()
+        
+        self.render(
+            "index.html",
+            slug = data.slug(),
+            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()
+        )
+
+
+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("id")
+        print self.get_arguments("name")
+        print self.get_arguments("type")
+        return
+        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()

+ 11 - 3
templates/index.html

@@ -4,7 +4,7 @@
 <head>
     <meta charset="utf-8">
 
-    <title>The Fossils Data</title>
+    <title>{{ slug }}</title>
     <meta name="description" content="Institute for Data Processing and Electronics">
     <meta name="author" content="Nicholas Tan Jerome">
     <link rel="stylesheet" href="{{ static_url("css/jquery-ui.min.css") }}">
@@ -42,11 +42,19 @@
     </div>
     
     <div id="authors">
-        <p>{{ author }}</p>
+        <p>
+            {% for author in authors %}
+            {{ author }}, 
+            {% end %}
+        </p>
     </div>
 
     <div id="institute">
-        <p>{{ institute }}</p>
+        <p>
+            {% for institute in institutes %}
+            {{ institute }}, 
+            {% end %}
+        </p>
     </div>
     
     <div id="subheader">