Commits

Anonymous committed 084b773

runserver cmd

Comments (0)

Files changed (2)

examples/manage.py

 from flask import Flask, current_app
 from flaskext.script import Manager
 
+from optparse import make_option
+
 def create_app():
     return Flask(__name__)
 
 def dump_config():
     pprint.pprint(current_app.config)
 
+@manager.register('printme', 
+                  options=(make_option('-n', '--name'),))
+def printme(name):
+    print name
 
 if __name__ == "__main__":
     manager.run()

flaskext/script.py

-"""
-Basic idea:
-# manage.py
-
-from flaskext.script import Manager
-
-def create_app(config=None, *args, **options):
-    # *args and **kwargs are command-line options
-    app = Flask(__name__)
-    if config is not None:
-        app.config.from_object(config)
-    return app
-
-manager = Manager(create_app)
-
-@manager.register('do_something')
-def do_something(name, **options):
-    print name
-
->>> python manage.py do_something hello
-... "hello"
->>> python manage.py do_something --name=hello
-... "hello"
-
-
-def make_shell_ctx(app):
-    return dict(db=db, app=app)
-
-manager.configure_shell(ctx=make_shell_ctx, title="My App Context")
-
-if __name__ == "__main__":
-    manager.run()
-
-"""
 import sys
 import code
 
+from flask import current_app
+
 from optparse import OptionParser, make_option
 
+class Command(object):
+
+    def __init__(self, func, name, usage, options):
+        self.func = func
+        self.name = name
+        self.options = options or []
+        self.usage = usage
+
+    def create_parser(self, prog):
+
+        parser = OptionParser(prog=prog,
+                              usage=self.usage)
+
+
+        parser.add_options(self.options)
+
+        return parser
+
+    def __call__(self, *args, **kwargs):
+        return self.func(*args, **kwargs)
+
 class Manager(object):
 
     def __init__(self, app_factory):
         self.app_factory = app_factory
-        self._scripts = dict()
+        self._commands = dict()
+        
+        server_options = [make_option('-p', '--port', default=5000, type="int")]
+        @self.register('runserver', options=server_options)
+        def runserver(port):
+            current_app.run(port=port)
+
+
+    def configure_shell(self, make_context=None, title=''):
+
+        pass
     
-    def register(self, name=None):
+    def register(self, name=None, usage=None, options=None):
         def decorator(f):
-            self._scripts[name or f.__name__] = f
-            print self._scripts
-            f.option_list = getattr(f, 'option_list', [])
+            self._commands[name or f.__name__] = \
+                Command(f, name, usage, options)
         return decorator
 
-    def usage(self, script):
-        return "ok"
+    def usage(self):
 
-    def create_parser(self, name, script):
-        return OptionParser(prog=name,
-                            usage=self.usage(script),
-                            option_list=script.option_list)
+        return "\n\r".join(sorted(self._commands.keys()))
+
     def run(self):
-
+        
         try:
-            script = self._scripts[sys.argv[1]]
-        except KeyError:
-            print "Command %s not found" % script
-            return
+            command = self._commands[sys.argv[1]]
+        except (IndexError, KeyError):
+            print self.usage()
+            sys.exit(1)
 
         # get option list
-        parser = self.create_parser(sys.argv[0], script)
+        parser = command.create_parser(sys.argv[0])
         options, args = parser.parse_args(sys.argv[2:])
+
         app = self.app_factory()
         with app.test_request_context():
-            script(*args, **options.__dict__)
+            command(*args, **options.__dict__)
+
+        sys.exit(0)