def __setitem__(self, name, flag): """Registers a new flag variable.""" fl = self.FlagDict() if not isinstance(flag, _flag.Flag): raise exceptions.IllegalFlagValue(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.FlagsError('Flag name must be a string') if not name: raise exceptions.FlagsError('Flag name cannot be empty') if name in fl and not flag.allow_override and not fl[ name].allow_override: module, module_name = _helpers.GetCallingModuleObjectAndName() if (self.FindModuleDefiningFlag(name) == module_name and id(module) != self.FindModuleIdDefiningFlag(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(name, self) short_name = flag.short_name 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(short_name, self) fl[short_name] = flag if (name not in fl # new flag or fl[name].using_default_value or not flag.using_default_value): fl[name] = flag
def AppendFlagValues(self, flag_values): """Appends flags registered in another FlagValues instance. Args: flag_values: registry to copy from """ for flag_name, flag in flag_values.FlagDict().iteritems(): # Each flags with shortname appears here twice (once under its # normal name, and again with its short name). To prevent # problems (DuplicateFlagError) with double flag registration, we # perform a check to make sure that the entry we're looking at is # for its normal name. if flag_name == flag.name: try: self[flag_name] = flag except exceptions.DuplicateFlagError: raise exceptions.DuplicateFlagError( flag_name, self, other_flag_values=flag_values)