Exemple #1
0
 def register_option(function):
   if Inspection.find_calling_module() == '__main__':
     added_option = self._get_option_from_args(args, kwargs)
     if not hasattr(function, Application.OPTIONS_ATTR):
       new_group = options.new_group('For command %s' % function.__name__)
       setattr(function, Application.OPTIONS_ATTR, new_group)
     getattr(function, Application.OPTIONS_ATTR).prepend_option(added_option)
   return function
Exemple #2
0
 def command(self, function):
   """
     Decorator to turn a function into an application command.
   """
   if Inspection.find_calling_module() == '__main__':
     func_name = function.__name__
     if func_name in self._commands:
       raise Application.Error('Found two definitions for command %s' % func_name)
     self._commands[func_name] = function
   return function
Exemple #3
0
 def default_command(self, function):
   """
     Decorator to make a command default.
   """
   if Inspection.find_calling_module() == '__main__':
     if None in self._commands:
       defaults = (self._commands[None].__name__, function.__name__)
       raise Application.Error('Found two default commands: %s and %s' % defaults)
     self._commands[None] = function
   return function
Exemple #4
0
  def add_option(self, *args, **kwargs):
    """
      Add an option to the application.

      You may pass either an Option object from the optparse/options module, or
      pass the *args/**kwargs necessary to construct an Option.
    """
    self._raise_if_initialized("Cannot call add_option() after main()!")
    calling_module = Inspection.find_calling_module()
    added_option = self._get_option_from_args(args, kwargs)
    self._add_option(calling_module, added_option)
Exemple #5
0
  def main(self):
    """
      If called from __main__ module, run script's main() method with arguments passed
      and global options parsed.
    """
    main_module = Inspection.find_calling_module()
    if main_module != '__main__':
      # only support if __name__ == '__main__'
      return

    # Pull in modules in twitter.common.app.modules
    if not self._import_module('twitter.common.app.modules'):
      print >> sys.stderr, 'Unable to import twitter app modules!'
      sys.exit(1)

    # defer init as long as possible.
    self.init()

    self._commands[None] = Inspection.find_main_from_caller()
    main_method = self._commands[self._command]
    if main_method is None:
      commands = sorted(self.get_commands())
      if commands:
        print >> sys.stderr, 'Must supply one of the following commands:', ', '.join(commands)
      else:
        print >> sys.stderr, 'No main() or command defined! Application must define one of these.'
      sys.exit(1)

    try:
      argspec = inspect.getargspec(main_method)
    except TypeError as e:
      print >> sys.stderr, 'Malformed main(): %s' % e
      sys.exit(1)

    if len(argspec.args) == 1:
      args = [self._argv]
    elif len(argspec.args) == 2:
      args = [self._argv, self._option_values]
    else:
      if len(self._argv) != 0:
        print >> sys.stderr, 'main() takes no arguments but got leftover arguments: %s!' % (
          ' '.join(self._argv))
        sys.exit(1)
      args = []
    rc = self._run_main(main_method, *args)
    self.quit(rc)