def run_main(): """Function to be used as setuptools script entry point. Appcommands assumes that it always runs as __main__, but launching via a setuptools-generated entry_point breaks this rule. We do some trickery here to make sure that appcommands and flags find their state where they expect to by faking ourselves as __main__. """ # Put the flags for this module somewhere the flags module will look # for them. # pylint: disable=protected-access new_name = flags._GetMainModule() sys.modules[new_name] = sys.modules['__main__'] for flag in FLAGS.FlagsByModuleDict().get(__name__, []): FLAGS._RegisterFlagByModule(new_name, flag) for key_flag in FLAGS.KeyFlagsByModuleDict().get(__name__, []): FLAGS._RegisterKeyFlagForModule(new_name, key_flag) # pylint: enable=protected-access # Now set __main__ appropriately so that appcommands will be # happy. sys.modules['__main__'] = sys.modules[__name__] appcommands.Run() sys.modules['__main__'] = sys.modules.pop(new_name)
def run_main(): # pylint:disable=invalid-name """Function to be used as setuptools script entry point.""" # Put the flags for this module somewhere the flags module will look # for them. # pylint:disable=protected-access new_name = flags._GetMainModule() sys.modules[new_name] = sys.modules['__main__'] for flag in FLAGS.FlagsByModuleDict().get(__name__, []): FLAGS._RegisterFlagByModule(new_name, flag) for key_flag in FLAGS.KeyFlagsByModuleDict().get(__name__, []): FLAGS._RegisterKeyFlagForModule(new_name, key_flag) # pylint:enable=protected-access # Now set __main__ appropriately so that appcommands will be # happy. sys.modules['__main__'] = sys.modules[__name__] appcommands.Run() sys.modules['__main__'] = sys.modules.pop(new_name)
def run_main(): """Function to be used as setuptools script entry point.""" # Put the flags for this module somewhere the flags module will look # for them. # pylint:disable=protected-access new_name = flags._GetMainModule() sys.modules[new_name] = sys.modules["__main__"] for flag in FLAGS.FlagsByModuleDict().get(__name__, []): FLAGS._RegisterFlagByModule(new_name, flag) for key_flag in FLAGS.KeyFlagsByModuleDict().get(__name__, []): FLAGS._RegisterKeyFlagForModule(new_name, key_flag) # pylint:enable=protected-access # Now set __main__ appropriately so that appcommands will be # happy. sys.modules["__main__"] = sys.modules[__name__] appcommands.Run() sys.modules["__main__"] = sys.modules.pop(new_name)
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)
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)