Commits

Jason R. Coombs committed 259dc68

Added serve-excuses as a console entry point. Use argparse to parse command-line arguments

Comments (0)

Files changed (2)

 import os
 import random
 import sys
+import argparse
 
 import cherrypy
 
 
     def get(self):
         return random.choice(self.excuses)
-    
+
     def find(self, word, index=None):
         candidates = []
         for e in self.excuses:
         if index > imax:
             index = imax
         return candidates[index]
-        
+
 class ExcusesApp(object):
     def __init__(self, base):
         self.base_path = base
                 index = None
         return self.excuses.find(word, index)
 
-def setup():
-    base = os.path.abspath(sys.argv[1])
+def setup(base):
     jqpath = os.path.join(base, "jquery-latest.pack.js")
     app_conf = {
         '/static/jquery.js':{
 
     cherrypy.tree.mount(ExcusesApp(base), '/', app_conf)
 
-if __name__ == '__main__':
-    cherrypy.config.update({'server.environment':'production', 
+def get_args():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('excuses_base', help="The directory where "
+        "excuses.txt and excuses.html can be found.", default=".")
+    return parser.parse_args()
+
+def main():
+    args = get_args()
+    cherrypy.config.update({'server.environment':'production',
                             'server.socket_port':8082,
                             'server.log_to_screen':False,})
-    setup()
+    setup(args.excuses_base)
     cherrypy.engine.signal_handler.subscribe()
     cherrypy.engine.start()
     cherrypy.engine.block()
 
+if __name__ == '__main__':
+    main()
 	setup_requires=[
 		'hgtools',
 	],
+	entry_points = dict(
+		console_scripts = [
+			'serve-excuses = excuses:main',
+		],
+	),
 )
 if __name__ == '__main__':
 	from setuptools import setup