Browse Source

refactor config

raylu 11 years ago
parent
commit
fd967c8cd3
10 changed files with 37 additions and 52 deletions
  1. 1 1
      .gitignore
  2. 3 1
      api/db.py
  3. 15 6
      api/reloader.py
  4. 2 1
      api/server.py
  5. 11 0
      config.py.example
  6. 0 28
      web/config.py
  7. 1 0
      web/config.py
  8. 0 11
      web/config.yaml.example
  9. 1 1
      web/db.py
  10. 3 3
      web/server.py

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
 *.pyc
-config.yaml
+config.py

+ 3 - 1
api/db.py

@@ -6,6 +6,8 @@ import psycopg2
 import psycopg2.extensions
 import psycopg2.extras
 
+import config
+
 def gevent_wait_callback(conn, timeout=None):
 	# https://github.com/zacharyvoase/gevent-psycopg2/blob/master/lib/gevent_psycopg2.py
 	while True:
@@ -23,7 +25,7 @@ psycopg2.extensions.set_wait_callback(gevent_wait_callback)
 
 pool = gevent.queue.Queue(maxsize=4)
 for _ in xrange(4):
-	pool.put(psycopg2.connect('dbname=%s user=%s' % ('sysvitals', 'sysvitals')))
+	pool.put(psycopg2.connect('dbname=%s user=%s' % (config.database, config.db_user)))
 
 @contextlib.contextmanager
 def cursor(): # https://code.google.com/p/gevent/source/browse/examples/psycopg2_pool.py?name=1.0b4#88

+ 15 - 6
api/reloader.py

@@ -4,25 +4,34 @@ import sys
 import thread
 
 def init(server):
-	thread.start_new_thread(_reload, (server,))
+	thread.start_new_thread(_reloader, (server,))
 
-def _reload(server):
+def _reloader(server):
 	import inotifyx
 
 	fd = inotifyx.init()
 	rootdir = path.dirname(path.abspath(__file__))
 	for dirpath, _, _ in os.walk(rootdir):
 		inotifyx.add_watch(fd, dirpath, inotifyx.IN_CLOSE_WRITE)
+	config_path = path.normpath(path.join(rootdir, '..', 'config.py'))
+	config_wd = inotifyx.add_watch(fd, config_path, inotifyx.IN_CLOSE_WRITE)
 
 	while True:
 		events = inotifyx.get_events(fd)
 		for event in events:
 			if event.name == '4913': # https://code.google.com/p/vim/source/browse/src/fileio.c?spec=svn94df797ed6b03d5387b5ed7526f8c65024112822&r=29eb4c2a33ac701bfcd4d2e2bed7864eba876e0e#3767
 				continue
+			elif event.name is None and event.wd == config_wd:
+				print 'config.py changed, reloading...'
+				do_reload(fd, server)
+				return
 			elif event.name.endswith('.py'):
 				print event.name, 'changed, reloading...'
-				os.close(fd)
-				server.stop()
-				os.closerange(sys.stderr.fileno()+1, os.sysconf('SC_OPEN_MAX')) # close keep-alive client sockets
-				os.execv(sys.argv[0], sys.argv)
+				do_reload(fd, server)
 				return
+
+def do_reload(fd, server):
+	os.close(fd)
+	server.stop()
+	os.closerange(sys.stderr.fileno()+1, os.sysconf('SC_OPEN_MAX')) # close keep-alive client sockets
+	os.execv(sys.argv[0], sys.argv)

+ 2 - 1
api/server.py

@@ -18,6 +18,7 @@ import urlparse
 
 import gevent.pywsgi
 
+import config
 import db
 import fileio
 import reloader
@@ -33,7 +34,7 @@ def main():
 		'stats': get_stats,
 		'register_server': register_server,
 	}
-	server = gevent.pywsgi.WSGIServer(('0.0.0.0', 8892), application)
+	server = gevent.pywsgi.WSGIServer(('0.0.0.0', config.api_port), application)
 	reloader.init(server)
 	server.serve_forever()
 

+ 11 - 0
config.py.example

@@ -0,0 +1,11 @@
+api_port = 8892
+
+web_port = 8888
+host = 'http://127.0.0.1:8888'
+cookie_secret = 'dis is super sekrit'
+debug = True
+
+db_user = 'sysvitals'
+database = 'sysvitals'
+
+# vim: set ft=python:

+ 0 - 28
web/config.py

@@ -1,28 +0,0 @@
-import yaml
-
-class Config:
-	def __init__(self, cdict):
-		attrs = set(self.attrs) # copy and "unfreeze"
-		for k, v in cdict.items():
-			attrs.remove(k) # check if the key is allowed, mark it as present
-			setattr(self, k, v)
-		if len(attrs) != 0:
-			raise KeyError('missing required bot config keys: %s' % attrs)
-
-class WebConfig(Config):
-	attrs = frozenset([
-		'port',
-		'host',
-		'cookie_secret',
-		'debug',
-	])
-
-class DBConfig(Config):
-	attrs = frozenset([
-		'user',
-		'database',
-	])
-
-__doc = yaml.load(open('config.yaml', 'r'))
-web = WebConfig(__doc['web'])
-db = DBConfig(__doc['db'])

+ 1 - 0
web/config.py

@@ -0,0 +1 @@
+../config.py

+ 0 - 11
web/config.yaml.example

@@ -1,11 +0,0 @@
-web:
-    port: 8888
-    host: 'http://127.0.0.1:8888'
-    cookie_secret: 'dis is super sekrit'
-    debug: true
-
-db:
-    user: 'sysvitals'
-    database: 'sysvitals'
-
-# vim: set et ft=yaml:

+ 1 - 1
web/db.py

@@ -19,7 +19,7 @@ def hash_pw(password, salt=None):
 	return hashed, salt_hex
 
 class MomokoDB:
-	db = momoko.Pool(dsn='dbname=%s user=%s' % (config.db.database, config.db.user), size=2)
+	db = momoko.Pool(dsn='dbname=%s user=%s' % (config.database, config.db_user), size=2)
 
 	@tornado.gen.coroutine
 	def execute(self, query, *args):

+ 3 - 3
web/server.py

@@ -10,7 +10,7 @@ import tornado.httpclient
 import tornado.ioloop
 import tornado.web
 
-from config import web as config
+import config
 import db
 
 class BaseHandler(tornado.web.RequestHandler):
@@ -119,7 +119,7 @@ if __name__ == '__main__':
 		cookie_secret=config.cookie_secret,
 		debug=config.debug,
 	)
-	app.listen(config.port)
+	app.listen(config.web_port)
 	app.db = db.MomokoDB()
-	print('listening on :%d' % config.port)
+	print('listening on :%d' % config.web_port)
 	tornado.ioloop.IOLoop.instance().start()