Esempio n. 1
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('Unable to import twitter app modules!', file=sys.stderr)
            sys.exit(1)

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

        try:
            caller_main = Inspection.find_main_from_caller()
        except Inspection.InternalError:
            caller_main = None
        if None in self._commands:
            assert caller_main is None, "Error: Cannot define both main and a default command."
        else:
            self._commands[None] = caller_main
        main_method = self._commands[self._command]
        if main_method is None:
            commands = sorted(self.get_commands())
            if commands:
                print('Must supply one of the following commands:',
                      ', '.join(commands),
                      file=sys.stderr)
            else:
                print(
                    'No main() or command defined! Application must define one of these.',
                    file=sys.stderr)
            sys.exit(1)

        try:
            argspec = inspect.getargspec(main_method)
        except TypeError as e:
            print('Malformed main(): %s' % e, file=sys.stderr)
            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(
                    'main() takes no arguments but got leftover arguments: %s!'
                    % ' '.join(self._argv),
                    file=sys.stderr)
                sys.exit(1)
            args = []
        rc = self._run_main(main_method, *args)
        self.quit(rc)
Esempio n. 2
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('Unable to import twitter app modules!', file=sys.stderr)
      sys.exit(1)

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

    try:
        caller_main = Inspection.find_main_from_caller()
    except Inspection.InternalError:
        caller_main = None
    if None in self._commands:
      assert caller_main is None, "Error: Cannot define both main and a default command."
    else:
      self._commands[None] = caller_main
    main_method = self._commands[self._command]
    if main_method is None:
      commands = sorted(self.get_commands())
      if commands:
        print('Must supply one of the following commands:', ', '.join(commands), file=sys.stderr)
      else:
        print('No main() or command defined! Application must define one of these.', file=sys.stderr)
      sys.exit(1)

    try:
      argspec = inspect.getargspec(main_method)
    except TypeError as e:
      print('Malformed main(): %s' % e, file=sys.stderr)
      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('main() takes no arguments but got leftover arguments: %s!' %
          ' '.join(self._argv), file=sys.stderr)
        sys.exit(1)
      args = []
    rc = self._run_main(main_method, *args)
    self.quit(rc)
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
0
 def name(self):
   """
     Return the name of the application.  If set_name was never explicitly called,
     the application framework will attempt to autodetect the name of the application
     based upon the location of __main__.
   """
   if self._name is not None:
     return self._name
   else:
     return Inspection.find_application_name()
Esempio n. 8
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
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
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
Esempio n. 12
0
 def _register_command(self, function, command_name=None):
   """
     Registers function as the handler for command_name. Uses function.__name__ if command_name
     is None.
   """
   if Inspection.find_calling_module() == '__main__':
     if command_name is None:
       command_name = function.__name__
     if command_name in self._commands:
       raise Application.Error('Found two definitions for command %s' % command_name)
     self._commands[command_name] = function
   return function
Esempio n. 13
0
 def name(self):
     """
   Return the name of the application.  If set_name was never explicitly called,
   the application framework will attempt to autodetect the name of the application
   based upon the location of __main__.
 """
     if self._name is not None:
         return self._name
     else:
         try:
             return Inspection.find_application_name()
         except:
             return 'unknown'
Esempio n. 14
0
 def _register_command(self, function, command_name=None):
     """
   Registers function as the handler for command_name. Uses function.__name__ if command_name
   is None.
 """
     if Inspection.find_calling_module() == '__main__':
         if command_name is None:
             command_name = function.__name__
         if command_name in self._commands:
             raise Application.Error(
                 'Found two definitions for command %s' % command_name)
         self._commands[command_name] = function
     return function