def Usage(shorthelp=0,
           writeto_stdout=0,
           detailed_error=None,
           exitcode=None,
           show_cmd=None,
           show_global_flags=False):
     """A replacement for app.usage."""
     printer('%s: Incorrect usage; details below.' % show_cmd)
     printer('Correct usage is as follows:')
     printer('')
     for line in ('  ' + cmd.__doc__.rstrip()).splitlines():
         printer(line)
     # Print out str(FLAGS) for just the UICmd-specific flags.
     tmp_flags = flags.FlagValues()
     type(cmd)(show_cmd, tmp_flags)
     prefix = _UICMD_MODULE_NAME + ':\n'
     flag_str = tmp_flags.ModuleHelp(
         str(_UICMD_MODULE_NAME) if six.PY2 else _UICMD_MODULE_NAME)
     flag_str = flag_str.lstrip()
     if flag_str.startswith(prefix):
         flag_str = flag_str[len(prefix):]
     if flag_str:
         printer('')
         printer('flags:')
         for line in flag_str.splitlines():
             printer(line)
     if detailed_error is not None:
         printer('')
         printer('The incorrect usage is as follows:')
         printer('')
         for line in six.text_type(detailed_error).splitlines():
             printer('  ' + line)
示例#2
0
 def test_importer_main_no_args(self, unused_pymongo):
     """Test the importer_main without args."""
     self.assertRaises(gflags.IllegalFlagValue,
                       mongo.importer_main,
                       _my_importer_func,
                       'my-collection', ['foo'],
                       flag_values=gflags.FlagValues())
示例#3
0
    def _GetCachedFlagValues(flag_values, cache_string):
        """Get the values for cacheable flags from the given cache string.

    Any flags in the parsed_flags argument will be omitted regardless of
    whether or not they are in the cache_string. This allows cached and
    parsed flags to be combined without conflict.

    Args:
      flag_values: A FlagValues instance defining the cacheable flags.
      cache_string: The contents of the cache as a newline delimited string.
    Returns:
      A FlagValues instance containing the flags present in the cache.
    """
        # We don't want to inadvertantly overwrite the parsed members of the
        # cacheable_flags, so we make a deep copy of it before appending.
        flag_values_copy = copy.deepcopy(flag_values)

        # Clear flags to load cache into clean state.
        for _, flag in flag_values_copy.FlagDict().iteritems():
            flag.present = 0
            flag.value = None

        cached_flag_values = flags.FlagValues()
        cached_flag_values.AppendFlagValues(flag_values_copy)
        undefok = []
        cache_entries = cache_string.split('\n')
        while cache_entries:
            try:
                argv = (
                    ['dummy_command',
                     '--undefok=\'%s\'' % ','.join(undefok)] + cache_entries)
                cache_entries = cached_flag_values(argv)[2:]
            except flags.UnrecognizedFlagError, err:
                undefok.append(err.flagname)
示例#4
0
    def _parse(self):
        if self._extra is not None:
            return

        args = gflags.FlagValues().ReadFlagsFromFiles(self._args)

        extra = None

        #
        # This horrendous hack allows us to stop optparse
        # exiting when it encounters an unknown option
        #
        error_catcher = self.ErrorCatcher(self._conf._oparser.error)
        self._conf._oparser.error = error_catcher.catch
        try:
            while True:
                error_catcher.reset()

                extra = self._conf(args)

                unknown = error_catcher.get_unknown_arg(args)
                if not unknown:
                    break

                args.remove(unknown)
        finally:
            self._conf._oparser.error = error_catcher.orig_error

        self._extra = extra
示例#5
0
    def _ApplyCachedFlags(cacheable, cached):
        """Applies cached values to the flags in cacheable flag set.

    Only flags that are not themselves present are updated with the
    cached value.  Returns flags whose value is present, either by
    user specifying them at command line (cacheable) or by having been
    loaded from cache (cached).

    Args:
      cacheable: flags whose value can be cached (and whose values will be
          updated from cache if value not present)
      cache: flags loaded from the cache.

    Returns:
      FlagValues object which contains all present flags
    """
        canonical = flags.FlagValues()
        for name, flag in cacheable.FlagDict().iteritems():
            if flag.name == name:
                if not flag.present:
                    cached_flag = cached[name]
                    if cached_flag.present:
                        LOGGER.debug('Flag override from cache file: %s -> %s',
                                     repr(name), repr(cached_flag.value))
                        flag.present = cached_flag.present
                        flag.value = cached_flag.value

                if flag.present:
                    canonical[name] = flag
        return canonical
示例#6
0
    def test_importer_filter_ids(self, mongo_mock):
        """Test of the filter_ids flag."""
        def richer_importer_func():
            """An importer with many outputs."""

            return list({
                '_id': 'foo-{:02d}'.format(i),
                'value': i
            } for i in range(20))

        mongo_mock.return_value = mock.MagicMock()
        mongo.importer_main(richer_importer_func,
                            'my-collection', ['foo', '--filter_ids', 'foo-.2'],
                            flag_values=gflags.FlagValues())

        import_in_collection = mongo_mock.return_value.import_in_collection
        self.assertTrue(import_in_collection.called)
        call_args = import_in_collection.call_args[0]
        self.assertEqual([{
            '_id': 'foo-02',
            'value': 2
        }, {
            '_id': 'foo-12',
            'value': 12
        }], call_args[0])
        self.assertEqual('my-collection', call_args[1])
示例#7
0
 def setUp(self):
     """Set up for each test: prepare the importer."""
     super(ImporterTestCase, self).setUp()
     self.flag_values = gflags.FlagValues()
     self.importer = mongo.Importer(self.flag_values)
     self.patcher = mock.patch(
         pymongo.__name__ + '.MongoClient', autospec=mongomock.MongoClient)
     self.mock_client = self.patcher.start()
     self.db_client = mongomock.MongoClient('mongodb://my-db-client-url/test')
     self.mock_client.return_value = self.db_client
示例#8
0
    def test_importer_main(self, mongo_mock):
        """Test of basic usage of the importer_main function."""
        mongo_mock.return_value = mock.MagicMock()
        mongo.importer_main(_my_importer_func,
                            'my-collection',
                            ['foo', '--arg1', 'Value of arg1'],
                            flag_values=gflags.FlagValues())

        import_in_collection = mongo_mock.return_value.import_in_collection
        self.assertTrue(import_in_collection.called)
        call_args = import_in_collection.call_args[0]
        self.assertEqual([{'arg1': 'Value of arg1', 'dummy': 2}], call_args[0])
        self.assertEqual('my-collection', call_args[1])
 def _DefineFlagAndValidators(self, first_validator, second_validator):
   local_flags = gflags.FlagValues()
   gflags.DEFINE_integer('test_flag', 2, 'test flag', flag_values=local_flags)
   gflags.RegisterValidator('test_flag',
                           first_validator,
                           message='',
                           flag_values=local_flags)
   gflags.RegisterValidator('test_flag',
                           second_validator,
                           message='',
                           flag_values=local_flags)
   argv = ('./program')
   local_flags(argv)
示例#10
0
    def test_importer_main_no_args_but_default(self, mongo_mock):
        """Test the importer_main without args but with default value."""
        def import_func(arg1='default value'):
            """Foo."""
            return [{'dummy': 2, 'arg1': arg1}]

        mongo_mock.return_value = mock.MagicMock()
        mongo.importer_main(import_func,
                            'my-collection', ['foo'],
                            flag_values=gflags.FlagValues())
        import_in_collection = mongo_mock.return_value.import_in_collection
        self.assertTrue(import_in_collection.called)
        call_args = import_in_collection.call_args[0]
        self.assertEqual([{'arg1': 'default value', 'dummy': 2}], call_args[0])
    def AddCmd(self, command_name, cmd_factory, **kargs):
        """See appcommands.AddCmd.

    Raises:
      Error
    """
        try:
            assert command_name not in self._flag_values_by_cmd, command_name
            self._flag_values_by_cmd[command_name] = flags.FlagValues()
            cmd = cmd_factory(command_name,
                              self._flag_values_by_cmd[command_name], **kargs)
            self._AddCmdInstance(command_name, cmd, **kargs)
        except appcommands.AppCommandsError as e:
            raise Error(e)
示例#12
0
 def test_importer_main_with_output_file(self, mongo_mock):
     """Test that data gets written to file instead of DB when file given."""
     out_path = tempfile.mktemp()
     mongo.importer_main(
         _my_importer_func, 'my-collection',
         ['', '--to_json', out_path, '--arg1', 'arg1 test value'],
         flag_values=gflags.FlagValues())
     import_in_collection = mongo_mock.return_value.import_in_collection
     self.assertFalse(import_in_collection.called)
     with open(out_path) as json_file:
         json_content = json_file.read()
         self.assertEqual(
             [{'arg1': 'arg1 test value', 'dummy': 2}],
             json.loads(json_content))
         self.assertTrue(json_content.endswith('\n'))
示例#13
0
    def testIsAnyScopeFlagSpecified(self):
        flag_values = flags.FlagValues()
        flags.DEFINE_string('zone', None, 'zone', flag_values)
        flags.DEFINE_string('region', None, 'region', flag_values)
        flags.DEFINE_bool('global', False, 'global', flag_values)

        def Set(name, value):
            flag_values[name].present = 1
            flag_values[name].value = value

        def Clear(name):
            flag_values[name].present = 0
            flag_values[name].value = flag_values[name].default

        #
        # The order of Set/Clear below is important to test all combinations.
        #

        # Nothing is set
        self.assertFalse(utils.IsAnyScopeFlagSpecified(flag_values))

        # Zone
        Set('zone', 'my-test-zone')
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Zone + Global
        Set('global', True)
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Global
        Clear('zone')
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Region + Global
        Set('region', 'my-test-region')
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Region
        Clear('global')
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Region + Zone
        Set('zone', 'my-test-zone')
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))

        # Zone + Region + Global
        Set('global', True)
        self.assertTrue(utils.IsAnyScopeFlagSpecified(flag_values))
示例#14
0
def DuplicateFlags(flagnames=None):
  """Returns a new FlagValues object with the requested flagnames.

  Used to test DuplicateFlagError detection.

  Args:
    flagnames: str, A list of flag names to create.

  Returns:
    A FlagValues object with one boolean flag for each name in flagnames.
  """
  flag_values = gflags.FlagValues()
  for name in flagnames:
    gflags.DEFINE_boolean(name, False, 'Flag named %s' % (name,),
                         flag_values=flag_values)
  return flag_values
示例#15
0
    def test_importer_main_with_input_file(self, pymongo_mock):
        """Test that the import_func doesn't get called with an input file."""
        mock_importer_func = mock.MagicMock(spec=_my_importer_func)

        def importer_func():
            """Foo."""
            mock_importer_func()

        client = mongomock.MongoClient('mongodb://mongo-url/test')
        pymongo_mock.MongoClient.return_value = client
        testdata_dir = path.join(path.dirname(__file__), 'testdata')
        json_path = path.join(testdata_dir, 'import_dummy_data.json')
        mongo.importer_main(importer_func,
                            'my_collection', ['', '--from_json', json_path],
                            flag_values=gflags.FlagValues())
        self.assertFalse(mock_importer_func.called)
        self.assertEqual(1, len(list(client.test.my_collection.find())))
示例#16
0
def AddCmd(command_name, cmd_factory, **kargs):
  """Add a command from a Cmd subclass or factory.

  Args:
    command_name:    name of the command which will be used in argument parsing
    cmd_factory:     A callable whose arguments match those of Cmd.__init__ and
                     returns a Cmd. In the simplest case this is just a subclass
                     of Cmd.
    command_aliases: A list of command aliases that the command can be run as.

  Raises:
    AppCommandsError: if calling cmd_factory does not return an instance of Cmd.
  """
  cmd = cmd_factory(command_name, flags.FlagValues(), **kargs)

  if not isinstance(cmd, Cmd):
    raise AppCommandsError('Command must be an instance of commands.Cmd')

  _AddCmdInstance(command_name, cmd, **kargs)
示例#17
0
def AddCmdFunc(command_name, cmd_func, command_aliases=None,
               all_commands_help=None):
  """Add a new command to the list of registered commands.

  Args:
    command_name:      name of the command which will be used in argument
                       parsing
    cmd_func:          command function, this function received the remaining
                       arguments as its only parameter. It is supposed to do the
                       command work and then return with the command result that
                       is being used as the shell exit code.
    command_aliases:   A list of command aliases that the command can be run as.
    all_commands_help: Help message to be displayed in place of func.__doc__
                       when all commands are displayed.
  """
  _AddCmdInstance(command_name,
                  _FunctionalCmd(command_name, flags.FlagValues(), cmd_func,
                                 command_aliases=command_aliases,
                                 all_commands_help=all_commands_help),
                  command_aliases)
def AddCmd(command_name, cmd_factory, **kwargs):
    """Add a command from a Cmd subclass or factory.

  Args:
    command_name:    name of the command which will be used in argument parsing
    cmd_factory:     A callable whose arguments match those of Cmd.__init__ and
                     returns a Cmd. In the simplest case this is just a subclass
                     of Cmd.
    **kwargs:        Additional keyword arguments to be passed to the
                     cmd_factory at initialization. Also passed to
                     _AddCmdInstance to catch command_aliases.

  Raises:
    AppCommandsError: if calling cmd_factory does not return an instance of Cmd.
  """
    cmd = cmd_factory(command_name, flags.FlagValues(), **kwargs)

    if not isinstance(cmd, Cmd):
        raise AppCommandsError('Command must be an instance of commands.Cmd')

    _AddCmdInstance(command_name, cmd, **kwargs)
示例#19
0
    def _GetCacheableFlagValues(flag_values):
        """Get the subset of flags that are appropriate to cache.

    All non-duplicate flags excluding --cache_flag_values and
    --cached_flags_file are eligible for caching.

    Args:
      flag_values: The superset FlagValues object.
    Returns:
      A subset FlagValues object that contains only the cacheable flags.
    """
        cacheable_flag_values = flags.FlagValues()
        for name, flag in flag_values.FlagDict().iteritems():
            # We only process the flag if seen by its full name (not short_name).
            if (name == flag.name
                    and not name in ['cache_flag_values', 'cached_flags_file']
                    and not name in cacheable_flag_values
                    and not flag.short_name in cacheable_flag_values):
                cacheable_flag_values[name] = flag

        return cacheable_flag_values
示例#20
0
    def _parse(self):
        if not self._values is None:
            return

        args = gflags.FlagValues().ReadFlagsFromFiles(self._args)

        values = extra = None

        #
        # This horrendous hack is needed because optparse only
        # shallow copies its defaults dict before parsing
        #
        defaults = copy.deepcopy(self._parser.defaults)

        #
        # This horrendous hack allows us to stop optparse
        # exiting when it encounters an unknown option
        #
        error_catcher = self.ErrorCatcher(self._parser.error)
        self._parser.error = error_catcher.catch
        try:
            while True:
                error_catcher.reset()

                (values, extra) = self._parser.parse_args(args)

                unknown = error_catcher.get_unknown_arg(args)
                if not unknown:
                    break

                args.remove(unknown)
                self._parser.defaults = defaults
                defaults = copy.deepcopy(defaults)
        finally:
            self._parser.error = error_catcher.orig_error
            self._parser.defaults = defaults

        values = self._apply_multistring_defaults(values)

        (self._values, self._extra) = (values, extra)
示例#21
0
 def setUp(self):
     super(SimpleValidatorTest, self).setUp()
     self.flag_values = gflags.FlagValues()
     self.call_args = []
示例#22
0
    3. An index file is maintained to track all the information.

    4. only deal with .jpg .png .mov 
"""

import sys
import gflags
import os

if __name__ != "__main__":
    sys.exit(0)

#==============================================================================
# Option handling
#==============================================================================
GFLAGS = gflags.FlagValues()
gflags.DEFINE_boolean("help", False, "print help information", GFLAGS)

#option parsing
try:
    GARGV = GFLAGS(sys.argv)
except gflags.FlagsError, e:
    print '%s\n%s\nOPTIONS:\n%s' % (e, __doc__, GFLAGS.MainModuleHelp())
    sys.exit(1)

#option and argument check
if GFLAGS.help:
    print '%s\nOPTIONS:\n%s' % (__doc__, GFLAGS.MainModuleHelp())
    sys.exit(0)

try:
示例#23
0
 def setUp(self):
     self.flags = gflags.FlagValues()
示例#24
0
    def testWriteHelpInXMLFormat(self):
        fv = gflags.FlagValues()
        # Since these flags are defined by the top module, they are all key.
        gflags.DEFINE_integer('index', 17, 'An integer flag', flag_values=fv)
        gflags.DEFINE_integer('nb_iters',
                              17,
                              'An integer flag',
                              lower_bound=5,
                              upper_bound=27,
                              flag_values=fv)
        gflags.DEFINE_string('file_path',
                             '/path/to/my/dir',
                             'A test string flag.',
                             flag_values=fv)
        gflags.DEFINE_boolean('use_hack',
                              False,
                              'Use performance hack',
                              flag_values=fv)
        gflags.DEFINE_enum('cc_version',
                           'stable', ['stable', 'experimental'],
                           'Compiler version to use.',
                           flag_values=fv)
        gflags.DEFINE_list('files',
                           'a.cc,a.h,archive/old.zip',
                           'Files to process.',
                           flag_values=fv)
        gflags.DEFINE_list('allow_users', ['alice', 'bob'],
                           'Users with access.',
                           flag_values=fv)
        gflags.DEFINE_spaceseplist('dirs',
                                   'src libs bins',
                                   'Directories to create.',
                                   flag_values=fv)
        gflags.DEFINE_multistring('to_delete', ['a.cc', 'b.h'],
                                  'Files to delete',
                                  flag_values=fv)
        gflags.DEFINE_multi_int('cols', [5, 7, 23],
                                'Columns to select',
                                flag_values=fv)
        # Define a few flags in a different module.
        module_bar.DefineFlags(flag_values=fv)
        # And declare only a few of them to be key.  This way, we have
        # different kinds of flags, defined in different modules, and not
        # all of them are key flags.
        gflags.DECLARE_key_flag('tmod_bar_z', flag_values=fv)
        gflags.DECLARE_key_flag('tmod_bar_u', flag_values=fv)

        # Generate flag help in XML format in the StringIO sio.
        sio = StringIO.StringIO()
        fv.WriteHelpInXMLFormat(sio)

        # Check that we got the expected result.
        expected_output_template = EXPECTED_HELP_XML_START
        main_module_name = gflags._GetMainModule()
        module_bar_name = module_bar.__name__

        if main_module_name < module_bar_name:
            expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MAIN_MODULE
            expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MODULE_BAR
        else:
            expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MODULE_BAR
            expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MAIN_MODULE

        expected_output_template += EXPECTED_HELP_XML_END

        # XML representation of the whitespace list separators.
        whitespace_separators = _ListSeparatorsInXMLFormat(string.whitespace,
                                                           indent='    ')
        expected_output = (expected_output_template % {
            'usage_doc': sys.modules['__main__'].__doc__,
            'main_module_name': main_module_name,
            'module_bar_name': module_bar_name,
            'whitespace_separators': whitespace_separators
        })

        actual_output = sio.getvalue()
        self.assertMultiLineEqual(actual_output, expected_output)

        # Also check that our result is valid XML.  minidom.parseString
        # throws an xml.parsers.expat.ExpatError in case of an error.
        xml.dom.minidom.parseString(actual_output)
示例#25
0
 def setUp(self):
     # self.fv is a FlagValues object, just like gflags.FLAGS.  Each
     # test registers one flag with this FlagValues.
     self.fv = gflags.FlagValues()