@@ -62,7 +62,7 @@ def run(self):
62
62
python manage.py print
63
63
> hello
64
64
65
- :param app: Flask instance or callable returning a Flask instance.
65
+ :param app: Flask instance, or callable returning a Flask instance.
66
66
:param with_default_commands: load commands **runserver** and **shell**
67
67
by default.
68
68
:param disable_argcomplete: disable automatic loading of argcomplete.
@@ -87,7 +87,7 @@ def __init__(self, app=None, with_default_commands=None, usage=None,
87
87
88
88
def add_default_commands (self ):
89
89
"""
90
- Adds the shell and runserver default commands. To override these
90
+ Adds the shell and runserver default commands. To override these,
91
91
simply add your own equivalents using add_command or decorators.
92
92
"""
93
93
@@ -98,62 +98,70 @@ def add_default_commands(self):
98
98
99
99
def add_option (self , * args , ** kwargs ):
100
100
"""
101
- Adds an application-wide option. This is useful if you want to set
102
- variables applying to the application setup, rather than individual
103
- commands.
101
+ Adds a global option. This is useful if you want to set variables
102
+ applying to the application setup, rather than individual commands.
104
103
105
104
For this to work, the manager must be initialized with a factory
106
- function rather than an instance. Otherwise any options you set will
107
- be ignored.
105
+ function rather than a Flask instance. Otherwise any options you set
106
+ will be ignored.
108
107
109
108
The arguments are then passed to your function, e.g.::
110
109
111
- def create_app (config=None):
110
+ def create_my_app (config=None):
112
111
app = Flask(__name__)
113
112
if config:
114
113
app.config.from_pyfile(config)
115
114
116
115
return app
117
116
118
- manager = Manager(create_app )
117
+ manager = Manager(create_my_app )
119
118
manager.add_option("-c", "--config", dest="config", required=False)
119
+ @manager.command
120
+ def mycommand(app):
121
+ app.do_something()
120
122
121
- and are evoked like this::
123
+ and are invoked like this::
122
124
123
125
> python manage.py -c dev.cfg mycommand
124
126
125
- Any manager options passed in the command line will not be passed to
127
+ Any manager options passed on the command line will not be passed to
126
128
the command.
127
129
128
130
Arguments for this function are the same as for the Option class.
129
131
"""
130
132
131
133
self ._options .append (Option (* args , ** kwargs ))
132
134
133
- def create_app (self , app = None , ** kwargs ):
134
- if self .app is None :
135
- # defer to parent Manager
136
- return self .parent .create_app (** kwargs )
137
-
138
- if isinstance (self .app , Flask ):
139
- return self .app
140
-
141
- return self .app (** kwargs ) or app
142
-
143
- def __call__ (self , app = None , * args , ** kwargs ):
135
+ def __call__ (self , app = None , ** kwargs ):
144
136
"""
145
- Call self.app()
137
+ This procedure is called with the App instance (if this is a
138
+ sub-Manager) and any options.
139
+
140
+ If your sub-Manager does not override this, any values for options will get lost.
146
141
"""
147
- res = self .create_app (app , * args , ** kwargs )
148
- if res is None :
149
- res = app
150
- return res
142
+ if app is None :
143
+ app = self .app
144
+ if app is None :
145
+ raise Exception ("There is no app here. This is unlikely to work." )
146
+
147
+ if isinstance (app , Flask ):
148
+ if kwargs :
149
+ import warnings
150
+ warnings .warn ("Options will be ignored." )
151
+ return app
152
+
153
+ app = app (** kwargs )
154
+ self .app = app
155
+ return app
151
156
157
+ def create_app (self , * args , ** kwargs ):
158
+ warnings .warn ("create_app() is deprecated; use __call__()." , warnings .DeprecationWarning )
159
+ return self (* args ,** kwargs )
152
160
153
- def create_parser (self , prog , func_stack = (), parents = None ):
161
+ def create_parser (self , prog , func_stack = (), parent = None ):
154
162
"""
155
163
Creates an ArgumentParser instance from options returned
156
- by get_options(), and a subparser for the given command .
164
+ by get_options(), and subparser for the given commands .
157
165
"""
158
166
prog = os .path .basename (prog )
159
167
func_stack = func_stack + (self ,)
@@ -162,9 +170,6 @@ def create_parser(self, prog, func_stack=(), parents=None):
162
170
for option in self .get_options ():
163
171
options_parser .add_argument (* option .args , ** option .kwargs )
164
172
165
- # parser_parents = parents if parents else [option_parser]
166
- # parser_parents = [options_parser]
167
-
168
173
parser = argparse .ArgumentParser (prog = prog , usage = self .usage ,
169
174
description = self .description ,
170
175
parents = [options_parser ],
@@ -182,7 +187,7 @@ def create_parser(self, prog, func_stack=(), parents=None):
182
187
description = getattr (command , 'description' , None )
183
188
if description is None : description = command .__doc__
184
189
185
- command_parser = command .create_parser (name , func_stack = func_stack )
190
+ command_parser = command .create_parser (name , func_stack = func_stack , parent = self )
186
191
187
192
subparser = subparsers .add_parser (name , usage = usage , help = help ,
188
193
description = description ,
@@ -194,7 +199,7 @@ def create_parser(self, prog, func_stack=(), parents=None):
194
199
195
200
## enable autocomplete only for parent parser when argcomplete is
196
201
## imported and it is NOT disabled in constructor
197
- if parents is None and ARGCOMPLETE_IMPORTED \
202
+ if parent is None and ARGCOMPLETE_IMPORTED \
198
203
and not self .disable_argcomplete :
199
204
argcomplete .autocomplete (parser , always_complete_options = True )
200
205
@@ -355,8 +360,6 @@ def handle(self, prog, args=None):
355
360
app_parser .error ('too many arguments' )
356
361
357
362
args = []
358
- if self .app is not None :
359
- args .append (self .app )
360
363
for handle in func_stack :
361
364
362
365
# get only safe config options
0 commit comments