Commits

Michael Elsdörfer committed 9cfae23 Merge

Merged backout into default branch.

Comments (0)

Files changed (4)

examples/manage.py

 import pprint
 
 from flask import Flask, current_app
-from flaskext.script import Manager, prompt_choices
+from flaskext.script import Manager, prompt_choices, Server
 
 def create_app(config=None):
     app = Flask(__name__)
                    help="config file", 
                    required=False)
 
+manager.add_command("runservernoreload", Server(use_reloader=False))
+
 if __name__ == "__main__":
     manager.run()

flaskext/script.py

         
         return parser
 
+    def handle(self, app, *args, **kwargs):
+        """
+        Handles the command with given app. Default behaviour is to call within
+        a test request context.
+        """
+        with app.test_request_context():
+            self.run(*args, **kwargs)
+
     def run(self):
 
         """
                          This can be overriden in the command line by 
                          passing the **-r** flag.
                          
+    :param options: :func:`werkzeug.run_simple` options.
     """
 
     description = 'Runs the Flask development server i.e. app.run()'
 
     def __init__(self, host='127.0.0.1', port=5000, use_debugger=True,
-        use_reloader=True):
+        use_reloader=True, **options):
 
 
         self.port = port
         self.host = host
         self.use_debugger = use_debugger
         self.use_reloader = use_reloader
+        self.server_options = options
     
     def get_options(self):
 
-        # TBD : if use_debugger or use_reloader is true 
-
         options = (
 
                 Option('-t', '--host',
         else:
             options += (Option('-r', '--reload',
                                action='store_true',
-                               dest='use_debugger',
-                               default=self.use_debugger),)
+                               dest='use_reloader',
+                               default=self.use_reloader),)
 
         return options
 
-    def run(self, host, port, use_debugger, use_reloader):
-        app = _request_ctx_stack.top.app
+    def handle(self, app, host, port, use_debugger, use_reloader):
+        # we don't need to run the server in request context
+        # so just run it directly
+
         app.run(host=host,
                 port=port,
                 debug=use_debugger,
                 use_debugger=use_debugger,
-                use_reloader=use_reloader)
+                use_reloader=use_reloader,
+                **self.server_options)
 
 
 class InvalidCommand(Exception):
         
         app = self.create_app(**app_namespace.__dict__)
 
-        with app.test_request_context():
-            command.run(**command_namespace.__dict__)
+        command.handle(app, **command_namespace.__dict__)
 
-    def run(self, commands=None):
+    def run(self, commands=None, default_command=None):
         
         """
         Prepares manager to receive command line input. Usually run
 
         :param commands: optional dict of commands. Appended to any commands 
                          added using add_command().
+
+        :param default_command: name of default command to run if no 
+                                arguments passed.
         """
 
         if commands:
             self._commands.update(commands)
-        
+
         try:
+            if len(sys.argv) == 1 and default_command is not None:
+                command = default_command
+            else:
+                command = sys.argv[1]
+
+            if command is None:
+                raise InvalidCommand, "Please provide a command"
+
             self.handle(sys.argv[0],
-                        sys.argv[1],
+                        command,
                         sys.argv[2:])
             
             sys.exit(0)
 
 setup(
     name='Flask-Script',
-    version='0.3',
+    version='0.3.1',
     url='http://bitbucket.org/danjac/flask-script',
     license='BSD',
     author='Dan Jacob',
         manager = Manager(lambda: app)
         assert callable(manager.app)
 
+    def test_run_with_default_command(self):
+        manager = Manager(self.app)
+        manager.add_command('simple', SimpleCommand())
+        try:
+            manager.run(default_command='simple')
+        except SystemExit, e:
+            assert e.code==0
+        assert 'OK' in sys.stdout.getvalue()
+