Skip to content

Commit dd04d81

Browse files
committed
Refactor function analysis
Moved analyzing a function to Command()'s __init__ code. The intended usage is introspection, i.e. class my_app(): def cmd_fubar(self,hello="Joe"): pass def init_manager(self,mgr): for fname in dir(self): if fname.startswith("cmd_"): cmd = Command(getattr(self,fname)) mgr.add_command(fname[4:],cmd)
1 parent 93b08af commit dd04d81

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

flask_script/__init__.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import re
66
import sys
77
import types
8-
import inspect
98
import warnings
109

1110
import argparse
1211

1312
from flask import Flask
1413

15-
from ._compat import text_type, iteritems, izip
14+
from ._compat import iteritems
1615
from .commands import Group, Option, Command, Server, Shell
1716
from .cli import prompt, prompt_pass, prompt_bool, prompt_choices
1817

@@ -264,46 +263,7 @@ def command(self, func):
264263
265264
"""
266265

267-
args, varargs, keywords, defaults = inspect.getargspec(func)
268-
if inspect.ismethod(func):
269-
args = args[1:]
270-
271-
options = []
272-
273-
# first arg is always "app" : ignore
274-
275-
defaults = defaults or []
276-
kwargs = dict(izip(*[reversed(l) for l in (args, defaults)]))
277-
278-
for arg in args:
279-
280-
if arg in kwargs:
281-
282-
default = kwargs[arg]
283-
284-
if isinstance(default, bool):
285-
options.append(Option('-%s' % arg[0],
286-
'--%s' % arg,
287-
action="store_true",
288-
dest=arg,
289-
required=False,
290-
default=default))
291-
else:
292-
options.append(Option('-%s' % arg[0],
293-
'--%s' % arg,
294-
dest=arg,
295-
type=text_type,
296-
required=False,
297-
default=default))
298-
299-
else:
300-
options.append(Option(arg, type=text_type))
301-
302-
command = Command()
303-
command.run = func
304-
command.__doc__ = func.__doc__
305-
command.option_list = options
306-
266+
command = Command(func)
307267
self.add_command(func.__name__, command)
308268

309269
return func

flask_script/commands.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import code
66
import warnings
77
import string
8+
import inspect
89

910
import argparse
1011

1112
from flask import _request_ctx_stack
1213

1314
from .cli import prompt, prompt_pass, prompt_bool, prompt_choices
15+
from ._compat import izip, text_type
1416

1517

1618
class InvalidCommand(Exception):
@@ -90,10 +92,55 @@ def __init__(self, *args, **kwargs):
9092
class Command(object):
9193
"""
9294
Base class for creating commands.
95+
96+
:param func: Initialize this command by introspecting the function.
9397
"""
9498

9599
option_list = []
96100

101+
def __init__(self, func=None):
102+
if func is None:
103+
return
104+
105+
args, varargs, keywords, defaults = inspect.getargspec(func)
106+
if inspect.ismethod(func):
107+
args = args[1:]
108+
109+
options = []
110+
111+
# first arg is always "app" : ignore
112+
113+
defaults = defaults or []
114+
kwargs = dict(izip(*[reversed(l) for l in (args, defaults)]))
115+
116+
for arg in args:
117+
118+
if arg in kwargs:
119+
120+
default = kwargs[arg]
121+
122+
if isinstance(default, bool):
123+
options.append(Option('-%s' % arg[0],
124+
'--%s' % arg,
125+
action="store_true",
126+
dest=arg,
127+
required=False,
128+
default=default))
129+
else:
130+
options.append(Option('-%s' % arg[0],
131+
'--%s' % arg,
132+
dest=arg,
133+
type=text_type,
134+
required=False,
135+
default=default))
136+
137+
else:
138+
options.append(Option(arg, type=text_type))
139+
140+
self.run = func
141+
self.__doc__ = func.__doc__
142+
self.option_list = options
143+
97144
@property
98145
def description(self):
99146
description = self.__doc__ or ''

0 commit comments

Comments
 (0)