Permalink
Browse files

Added rudimentary server code (using werkzeug framework)

Created starbase python module
  • Loading branch information...
1 parent 0d4a23a commit 42165e4267282ec34dd1049777d5b1f7abe8a768 @sschaetz committed Aug 15, 2011
View
@@ -7,28 +7,11 @@
import sqlite3
import os
+from starbase.utils import file_exists
-def user_exists(folder, username):
- try:
- f = open(folder + username + '.sql', 'r')
- f.close
- except:
- return 0
- return 1
-
-
-class Usage(Exception):
- def __init__(self, msg):
- self.msg = msg
-
-
-def main():
- if len(sys.argv) != 3:
- print "usage: python newuser.py <username> <accesstoken>"
- return 2
- username = sys.argv[1]
- accesstoken = sys.argv[2]
- print "trying to create user", username
+def create_user(username, accesstoken, verbose=0):
+ if verbose:
+ print "trying to create user", username
# read configuration
config = ConfigParser.RawConfigParser()
@@ -39,8 +22,9 @@ def main():
user_url = config.get('general', 'domain') + username
# check if user already exists
- if user_exists(user_data_folder, username):
- print "error: user already exists"
+ if file_exists(user_database):
+ if verbose:
+ print "error: user already exists"
return 2
# here be dragons
@@ -50,21 +34,32 @@ def main():
# initialize database
db = sqlite3.connect(user_database)
- db.execute('insert into admin (authkey, name, url) values (?, ?, ?) ',
+ db.execute('INSERT INTO admin (authkey, name, url) VALUES (?, ?, ?) ',
(accesstoken, username, user_url))
db.commit()
db.close()
except Exception, e:
# delete the file if it was created
- if user_exists(user_data_folder, username):
+ if file_exists(user_database):
os.remove(user_database)
-
- print e
+ if verbose:
+ print e
return 2
else:
- print "success"
+ if verbose:
+ print "success"
+ return 0
+
+def main():
+ if len(sys.argv) != 3:
+ print "usage: python newuser.py <username> <accesstoken>"
+ return 2
+ username = sys.argv[1]
+ accesstoken = sys.argv[2]
+ return create_user(username, accesstoken, 1)
+
if __name__ == "__main__":
sys.exit(main())
-
+
View
View
@@ -0,0 +1,95 @@
+
+import ConfigParser
+
+from werkzeug.wrappers import Response, Request
+from werkzeug.routing import Map, Rule
+from werkzeug.exceptions import HTTPException, NotFound
+from werkzeug.wsgi import SharedDataMiddleware
+from werkzeug.wrappers import CommonRequestDescriptorsMixin, BaseRequest
+
+from starbase.utils import file_exists
+
+
+class starbase(object):
+
+ def __init__(self):
+ self.config = ConfigParser.RawConfigParser()
+ self.config.read('../config.cfg')
+ self.user_data_folder = self.config.get('general', 'user_data_folder')
+
+ # map urls to functions
+ self.url_map = Map([
+ Rule('/<user>/', endpoint='default'),
+ Rule('/<user>/load_data', endpoint='load_data'),
+ Rule('/<user>/store_data', endpoint='store_data'),
+ Rule('/<user>/load_messages', endpoint='load_messages'),
+ Rule('/<user>/inbox', endpoint='inbox'),
+ Rule('/<user>/add_accesstokens', endpoint='add_accesstokens'),
+ Rule('/<user>/remove_accesstokens', endpoint='remove_accesstokens')
+ ])
+
+ def dispatch_request(self, request):
+ adapter = self.url_map.bind_to_environ(request.environ)
+ try:
+ endpoint, values = adapter.match()
+ # only proceed if user exists
+ if not self.user_exists(values['user']):
+ raise NotFound()
+
+ return getattr(self, 'on_' + endpoint)(request, **values)
+ except HTTPException, e:
+ return e
+
+ def wsgi_app(self, environ, start_response):
+ request = Request(environ)
+ response = self.dispatch_request(request)
+ return response(environ, start_response)
+
+ def __call__(self, environ, start_response):
+ return self.wsgi_app(environ, start_response)
+
+
+ # here are the responders -----
+
+
+ def on_default(self, request, user):
+ return Response("on_default " + user)
+
+ def on_load_data(self, request, user):
+ return Response("load_data")
+
+ def on_store_data(self, request, user):
+ return Response("store_data")
+
+ def on_load_messages(self, request, user):
+ return Response("load_messages")
+
+ def on_inbox(self, request, user):
+ return Response("inbox")
+
+ def on_add_accesstokens(self, request, user):
+ return Response("add_accesstokens")
+
+ def on_remove_accesstokens(self, request, user):
+ return Response("remove_accesstokens")
+
+
+ # some utility functions -----
+
+ def user_exists(self, user):
+ user_database = self.user_data_folder + user + '.sql'
+ print user_database
+ return file_exists(user_database)
+
+
+def create_app():
+ app = starbase()
+ return app
+
+
+if __name__ == '__main__':
+ from werkzeug.serving import run_simple
+ app = create_app()
+ run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
+
+
No changes.
Binary file not shown.
View
@@ -0,0 +1,8 @@
+
+def file_exists(filename):
+ try:
+ f = open(filename, 'r')
+ f.close
+ except:
+ return 0
+ return 1
View
Binary file not shown.
View
No changes.

0 comments on commit 42165e4

Please sign in to comment.