Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.support.werkzeug.script Namespace Reference

Functions

def run
def fail
def find_actions
def print_usage
def analyse_action
def make_shell
def make_runserver

Variables

dictionary argument_types
dictionary converters

Function Documentation

Analyse a function.

Definition at line 213 of file script.py.

00213 
00214 def analyse_action(func):
00215     """Analyse a function."""
00216     description = inspect.getdoc(func) or 'undocumented action'
00217     arguments = []
00218     args, varargs, kwargs, defaults = inspect.getargspec(func)
00219     if varargs or kwargs:
00220         raise TypeError('variable length arguments for action not allowed.')
00221     if len(args) != len(defaults or ()):
00222         raise TypeError('not all arguments have proper definitions')
00223 
00224     for idx, (arg, definition) in enumerate(zip(args, defaults or ())):
00225         if arg.startswith('_'):
00226             raise TypeError('arguments may not start with an underscore')
00227         if not isinstance(definition, tuple):
00228             shortcut = None
00229             default = definition
00230         else:
00231             shortcut, default = definition
00232         argument_type = argument_types[type(default)]
00233         if isinstance(default, bool) and default is True:
00234             arg = 'no-' + arg
00235         arguments.append((arg.replace('_', '-'), shortcut,
00236                           default, argument_type))
00237     return func, description, arguments
00238 

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.script.fail (   message,
  code = -1 
)
Fail with an error.

Definition at line 171 of file script.py.

00171 
00172 def fail(message, code=-1):
00173     """Fail with an error."""
00174     print >> sys.stderr, 'Error:', message
00175     sys.exit(code)
00176 

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.script.find_actions (   namespace,
  action_prefix 
)
Find all the actions in the namespace.

Definition at line 177 of file script.py.

00177 
00178 def find_actions(namespace, action_prefix):
00179     """Find all the actions in the namespace."""
00180     actions = {}
00181     for key, value in namespace.iteritems():
00182         if key.startswith(action_prefix):
00183             actions[key[len(action_prefix):]] = analyse_action(value)
00184     return actions
00185 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.script.make_runserver (   app_factory,
  hostname = 'localhost',
  port = 5000,
  use_reloader = False,
  use_debugger = False,
  use_evalex = True,
  threaded = False,
  processes = 1,
  static_files = None,
  extra_files = None 
)
Returns an action callback that spawns a new development server.

.. versionadded:: 0.5
   `static_files` and `extra_files` was added.

:param app_factory: a function that returns a new WSGI application.
:param hostname: the default hostname the server should listen on.
:param port: the default port of the server.
:param use_reloader: the default setting for the reloader.
:param use_evalex: the default setting for the evalex flag of the debugger.
:param threaded: the default threading setting.
:param processes: the default number of processes to start.
:param static_files: optionally a dict of static files.
:param extra_files: optionally a list of extra files to track for reloading.

Definition at line 274 of file script.py.

00274 
00275                    extra_files=None):
00276     """Returns an action callback that spawns a new development server.
00277 
00278     .. versionadded:: 0.5
00279        `static_files` and `extra_files` was added.
00280 
00281     :param app_factory: a function that returns a new WSGI application.
00282     :param hostname: the default hostname the server should listen on.
00283     :param port: the default port of the server.
00284     :param use_reloader: the default setting for the reloader.
00285     :param use_evalex: the default setting for the evalex flag of the debugger.
00286     :param threaded: the default threading setting.
00287     :param processes: the default number of processes to start.
00288     :param static_files: optionally a dict of static files.
00289     :param extra_files: optionally a list of extra files to track for reloading.
00290     """
00291     def action(hostname=('h', hostname), port=('p', port),
00292                reloader=use_reloader, debugger=use_debugger,
00293                evalex=use_evalex, threaded=threaded, processes=processes):
00294         """Start a new development server."""
00295         from werkzeug.serving import run_simple
00296         app = app_factory()
00297         run_simple(hostname, port, app, reloader, debugger, evalex,
00298                    extra_files, 1, threaded, processes,
00299                    static_files=static_files)
00300     return action

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.script.make_shell (   init_func = None,
  banner = None,
  use_ipython = True 
)
Returns an action callback that spawns a new interactive
python shell.

:param init_func: an optional initialization function that is
                  called before the shell is started.  The return
                  value of this function is the initial namespace.
:param banner: the banner that is displayed before the shell.  If
               not specified a generic banner is used instead.
:param use_ipython: if set to `True` ipython is used if available.

Definition at line 239 of file script.py.

00239 
00240 def make_shell(init_func=None, banner=None, use_ipython=True):
00241     """Returns an action callback that spawns a new interactive
00242     python shell.
00243 
00244     :param init_func: an optional initialization function that is
00245                       called before the shell is started.  The return
00246                       value of this function is the initial namespace.
00247     :param banner: the banner that is displayed before the shell.  If
00248                    not specified a generic banner is used instead.
00249     :param use_ipython: if set to `True` ipython is used if available.
00250     """
00251     if banner is None:
00252         banner = 'Interactive Werkzeug Shell'
00253     if init_func is None:
00254         init_func = dict
00255     def action(ipython=use_ipython):
00256         """Start a new interactive python session."""
00257         namespace = init_func()
00258         if ipython:
00259             try:
00260                 import IPython
00261             except ImportError:
00262                 pass
00263             else:
00264                 sh = IPython.Shell.IPShellEmbed(banner=banner)
00265                 sh(global_ns={}, local_ns=namespace)
00266                 return
00267         from code import interact
00268         interact(banner, local=namespace)
00269     return action
00270 

Here is the call graph for this function:

Print the usage information.  (Help screen)

Definition at line 186 of file script.py.

00186 
00187 def print_usage(actions):
00188     """Print the usage information.  (Help screen)"""
00189     actions = actions.items()
00190     actions.sort()
00191     print 'usage: %s <action> [<options>]' % basename(sys.argv[0])
00192     print '       %s --help' % basename(sys.argv[0])
00193     print
00194     print 'actions:'
00195     for name, (func, doc, arguments) in actions:
00196         print '  %s:' % name
00197         for line in doc.splitlines():
00198             print '    %s' % line
00199         if arguments:
00200             print
00201         for arg, shortcut, default, argtype in arguments:
00202             if isinstance(default, bool):
00203                 print '    %s' % (
00204                     (shortcut and '-%s, ' % shortcut or '') + '--' + arg
00205                 )
00206             else:
00207                 print '    %-30s%-10s%s' % (
00208                     (shortcut and '-%s, ' % shortcut or '') + '--' + arg,
00209                     argtype, default
00210                 )
00211         print
00212 

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.script.run (   namespace = None,
  action_prefix = 'action_',
  args = None 
)
Run the script.  Participating actions are looked up in the callers
namespace if no namespace is given, otherwise in the dict provided.
Only items that start with action_prefix are processed as actions.  If
you want to use all items in the namespace provided as actions set
action_prefix to an empty string.

:param namespace: An optional dict where the functions are looked up in.
                  By default the local namespace of the caller is used.
:param action_prefix: The prefix for the functions.  Everything else
                      is ignored.
:param args: the arguments for the function.  If not specified
             :data:`sys.argv` without the first argument is used.

Definition at line 89 of file script.py.

00089 
00090 def run(namespace=None, action_prefix='action_', args=None):
00091     """Run the script.  Participating actions are looked up in the callers
00092     namespace if no namespace is given, otherwise in the dict provided.
00093     Only items that start with action_prefix are processed as actions.  If
00094     you want to use all items in the namespace provided as actions set
00095     action_prefix to an empty string.
00096 
00097     :param namespace: An optional dict where the functions are looked up in.
00098                       By default the local namespace of the caller is used.
00099     :param action_prefix: The prefix for the functions.  Everything else
00100                           is ignored.
00101     :param args: the arguments for the function.  If not specified
00102                  :data:`sys.argv` without the first argument is used.
00103     """
00104     if namespace is None:
00105         namespace = sys._getframe(1).f_locals
00106     actions = find_actions(namespace, action_prefix)
00107 
00108     if args is None:
00109         args = sys.argv[1:]
00110     if not args or args[0] in ('-h', '--help'):
00111         return print_usage(actions)
00112     elif args[0] not in actions:
00113         fail('Unknown action \'%s\'' % args[0])
00114 
00115     arguments = {}
00116     conv = {}
00117     key_to_arg = {}
00118     long_options = []
00119     formatstring = ''
00120     func, doc, arg_def = actions[args.pop(0)]
00121     for idx, (arg, shortcut, default, option_type) in enumerate(arg_def):
00122         real_arg = arg.replace('-', '_')
00123         converter = converters[option_type]
00124         if shortcut:
00125             formatstring += shortcut
00126             if not isinstance(default, bool):
00127                 formatstring += ':'
00128             key_to_arg['-' + shortcut] = real_arg
00129         long_options.append(isinstance(default, bool) and arg or arg + '=')
00130         key_to_arg['--' + arg] = real_arg
00131         key_to_arg[idx] = real_arg
00132         conv[real_arg] = converter
00133         arguments[real_arg] = default
00134 
00135     try:
00136         optlist, posargs = getopt.gnu_getopt(args, formatstring, long_options)
00137     except getopt.GetoptError, e:
00138         fail(str(e))
00139 
00140     specified_arguments = set()
00141     for key, value in enumerate(posargs):
00142         try:
00143             arg = key_to_arg[key]
00144         except IndexError:
00145             fail('Too many parameters')
00146         specified_arguments.add(arg)
00147         try:
00148             arguments[arg] = conv[arg](value)
00149         except ValueError:
00150             fail('Invalid value for argument %s (%s): %s' % (key, arg, value))
00151 
00152     for key, value in optlist:
00153         arg = key_to_arg[key]
00154         if arg in specified_arguments:
00155             fail('Argument \'%s\' is specified twice' % arg)
00156         if arg.startswith('no_'):
00157             value = 'no'
00158         elif not value:
00159             value = 'yes'
00160         try:
00161             arguments[arg] = conv[arg](value)
00162         except ValueError:
00163             fail('Invalid value for \'%s\': %s' % (key, value))
00164 
00165     newargs = {}
00166     for k, v in arguments.iteritems():
00167         newargs[k.startswith('no_') and k[3:] or k] = v
00168     arguments = newargs
00169     return func(**arguments)
00170 

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 {
00002     bool:       'boolean',
00003     str:        'string',
00004     int:        'integer',
00005     float:      'float'
00006 }

Definition at line 73 of file script.py.

Initial value:
00001 {
00002     'boolean':  lambda x: x.lower() in ('1', 'true', 'yes', 'on'),
00003     'string':   str,
00004     'integer':  int,
00005     'float':    float
00006 }

Definition at line 81 of file script.py.