def adopt_module_key_flags(module, flag_values=_flagvalues.FLAGS): """Declares that all flags key to a module are key to the current module. Args: module: module, the module object from which all key flags will be declared as key flags to the current module. flag_values: FlagValues, the FlagValues instance in which the flags will be declared as key flags. This should almost never need to be overridden. Raises: Error: Raised when given an argument that is a module name (a string), instead of a module object. """ if not isinstance(module, types.ModuleType): raise _exceptions.Error('Expected a module object, not %r.' % (module, )) _internal_declare_key_flags([ f.name for f in flag_values.get_key_flags_for_module(module.__name__) ], flag_values=flag_values) # If module is this flag module, take _helpers.SPECIAL_FLAGS into account. if module == _helpers.FLAGS_MODULE: _internal_declare_key_flags( # As we associate flags with get_calling_module_object_and_name(), the # special flags defined in this module are incorrectly registered with # a different module. So, we can't use get_key_flags_for_module. # Instead, we take all flags from _helpers.SPECIAL_FLAGS (a private # FlagValues, where no other module should register flags). [ _helpers.SPECIAL_FLAGS[name].name for name in _helpers.SPECIAL_FLAGS ], flag_values=_helpers.SPECIAL_FLAGS, key_flag_values=flag_values)
def _extract_filename(self, flagfile_str): """Returns filename from a flagfile_str of form -[-]flagfile=filename. The cases of --flagfile foo and -flagfile foo shouldn't be hitting this function, as they are dealt with in the level above this function. Args: flagfile_str: str, the flagfile string. Returns: str, the filename from a flagfile_str of form -[-]flagfile=filename. Raises: Error: Raised when illegal --flagfile is provided. """ if flagfile_str.startswith('--flagfile='): return os.path.expanduser( (flagfile_str[(len('--flagfile=')):]).strip()) elif flagfile_str.startswith('-flagfile='): return os.path.expanduser( (flagfile_str[(len('-flagfile=')):]).strip()) else: raise _exceptions.Error('Hit illegal --flagfile type: %s' % flagfile_str)
def __init__(self, parser, serializer, name, default, help_string, short_name=None, boolean=False, allow_override=False, allow_override_cpp=False, allow_hide_cpp=False, allow_overwrite=True, allow_using_method_names=False): self.name = name if not help_string: help_string = '(no help available)' self.help = help_string self.short_name = short_name self.boolean = boolean self.present = 0 self.parser = parser self.serializer = serializer self.allow_override = allow_override self.allow_override_cpp = allow_override_cpp self.allow_hide_cpp = allow_hide_cpp self.allow_overwrite = allow_overwrite self.allow_using_method_names = allow_using_method_names self.using_default_value = True self._value = None self.validators = [] if self.allow_hide_cpp and self.allow_override_cpp: raise _exceptions.Error( "Can't have both allow_hide_cpp (means use Python flag) and " 'allow_override_cpp (means use C++ flag after InitGoogle)') self._set_default(default)
def __setitem__(self, name, flag): """Registers a new flag variable.""" fl = self._flags() if not isinstance(flag, _flag.Flag): raise _exceptions.IllegalFlagValueError(flag) if str is bytes and isinstance(name, unicode): # When using Python 2 with unicode_literals, allow it but encode it # into the bytes type we require. name = name.encode('utf-8') if not isinstance(name, type('')): raise _exceptions.Error('Flag name must be a string') if not name: raise _exceptions.Error('Flag name cannot be empty') if ' ' in name: raise _exceptions.Error('Flag name cannot contain a space') self._check_method_name_conflicts(name, flag) if name in fl and not flag.allow_override and not fl[ name].allow_override: module, module_name = _helpers.get_calling_module_object_and_name() if (self.find_module_defining_flag(name) == module_name and id(module) != self.find_module_id_defining_flag(name)): # If the flag has already been defined by a module with the same name, # but a different ID, we can stop here because it indicates that the # module is simply being imported a subsequent time. return raise _exceptions.DuplicateFlagError.from_flag(name, self) short_name = flag.short_name # If a new flag overrides an old one, we need to cleanup the old flag's # modules if it's not registered. flags_to_cleanup = set() if short_name is not None: if (short_name in fl and not flag.allow_override and not fl[short_name].allow_override): raise _exceptions.DuplicateFlagError.from_flag( short_name, self) if short_name in fl and fl[short_name] != flag: flags_to_cleanup.add(fl[short_name]) fl[short_name] = flag if (name not in fl # new flag or fl[name].using_default_value or not flag.using_default_value): if name in fl and fl[name] != flag: flags_to_cleanup.add(fl[name]) fl[name] = flag for f in flags_to_cleanup: self._cleanup_unregistered_flag_from_module_dicts(f)
def _serialize(self, value): """See base class.""" if not self.serializer: raise _exceptions.Error( 'Serializer not present for flag %s' % self.name) if value is None: return '' serialized_items = [ super(MultiFlag, self)._serialize(value_item) for value_item in value ] return '\n'.join(serialized_items)
def serialize(self): if self.value is None: return '' if self.boolean: if self.value: return '--%s' % self.name else: return '--no%s' % self.name else: if not self.serializer: raise _exceptions.Error( 'Serializer not present for flag %s' % self.name) return '--%s=%s' % (self.name, self.serializer.serialize(self.value))
def _serialize(self, value): """Internal serialize function.""" if value is None: return '' if self.boolean: if value: return '--%s' % self.name else: return '--no%s' % self.name else: if not self.serializer: raise _exceptions.Error('Serializer not present for flag %s' % self.name) return '--%s=%s' % (self.name, self.serializer.serialize(value))
def serialize(self): """See base class.""" if not self.serializer: raise _exceptions.Error('Serializer not present for flag %s' % self.name) if self.value is None: return '' s = '' multi_value = self.value for self.value in multi_value: if s: s += ' ' s += Flag.serialize(self) self.value = multi_value return s
def get_value(): # pylint: disable=cell-var-from-loop try: return next(args) if value is None else value except StopIteration: raise _exceptions.Error('Missing value for flag ' + arg) # pylint: disable=undefined-loop-variable