diff iwws.py @ 0:2d9ee2b3ae82

Initial commit of iWWS.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 15 Aug 2011 17:44:56 +0930
parents
children e5ba2e76d1b0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iwws.py	Mon Aug 15 17:44:56 2011 +0930
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+
+#
+# Copyright 2011 Daniel O'Connor <darius@dons.net.au>
+#
+#  Redistribution and use in source and binary forms, with or without modification, are
+# permitted provided that the following conditions are met:
+#
+#   1. Redistributions of source code must retain the above copyright notice, this list of
+#      conditions and the following disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above copyright notice, this list
+#      of conditions and the following disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and documentation are those of the
+# authors and should not be interpreted as representing official policies, either expressed
+# or implied, of Daniel O'Connor.
+#
+
+import sys
+
+sys.path.append('..')
+
+import calendar
+import datetime
+import DataStore
+import flask
+import logging
+
+DEBUG = True
+DATA_DIR = '/data/weather'
+
+# Create application object
+app = flask.Flask(__name__)
+app.debug_log_format = '%(asctime)s: %(message)s'
+app.config.from_object(__name__)
+app.config.from_envvar('IWWS_SETTINGS', silent=True)
+
+if DEBUG:
+    app.logger.setLevel(logging.DEBUG)
+
+@app.route('/')
+def index():
+    # Could be static really..
+    return flask.render_template('index.html')
+
+@app.route('/getdata.json')
+def getdata():
+    app.logger.debug("Getting data")
+    store = DataStore.hourly_store(DATA_DIR)
+    now = datetime.datetime.now()
+    then = now - datetime.timedelta(1)
+    data = store[then:now]
+    app.logger.debug("done")
+    r = {}
+    for k in store.key_list:
+        r[k] = []
+
+    for d in data:
+        for k in store.key_list:
+            if k == 'idx':
+                t = int(dt2epoch(d[k]))
+                r[k].append(t)
+            else:
+                r[k].append(d[k])
+    return flask.jsonify(r)
+
+def dt2epoch(dt):
+    return calendar.timegm(dt.utctimetuple()[0:6]) + dt.microsecond / 1e6
+
+
+if __name__ == "__main__":
+    app.run(host = '0.0.0.0', port = 5001, debug = DEBUG)