def testTypeDescriptorSet(self): type_infos = [ type_info.String( name="output", default="analysis/{p}/{u}-{t}"), type_info.String( description="Profile to use.", name="profile", default=""), type_info.String( description="A comma separated list of plugins.", name="plugins", default=""), ] info = type_info.TypeDescriptorSet( type_infos[0], type_infos[1], type_infos[2], ) new_info = type_info.TypeDescriptorSet( type_infos[0], ) updated_info = new_info + type_info.TypeDescriptorSet( type_infos[1], ) updated_info += type_info.TypeDescriptorSet( type_infos[2], ) self.assertEqual(info.descriptor_map, updated_info.descriptor_map) self.assertEqual(sorted(info.descriptors), sorted(updated_info.descriptors)) self.assertTrue(type_infos[1] in updated_info.descriptors) self.assertTrue("plugins" in updated_info) removed_info = updated_info.Remove("plugins") self.assertTrue(type_infos[1] in updated_info.descriptors) self.assertTrue("plugins" in updated_info) self.assertFalse(type_infos[2] in removed_info.descriptors) self.assertFalse("plugins" in removed_info)
def DEFINE_list(name, default, help): """A helper for defining lists of strings options.""" CONFIG.AddOption( type_info.List(name=name, default=default, description=help, validator=type_info.String()))
def InterpolateValue(self, value, type_info_obj=type_info.String(), default_section=None, context=None): """Interpolate the value and parse it with the appropriate type.""" # It is only possible to interpolate strings... if isinstance(value, basestring): value = StringInterpolator(value, self, default_section=default_section, parameter=type_info_obj.name, context=context).Parse() # Parse the data from the string. value = type_info_obj.FromString(value) # ... and lists of strings. if isinstance(value, list): value = [ self.InterpolateValue(v, default_section=default_section, context=context) for v in value ] return value
def FindTypeInfo(self, name): """Search for a type_info instance which describes this key.""" result = self.type_infos.get(name) if result is None: # Not found, assume string. result = type_info.String(name=name, default="") return result
def testTypeInfoStringObjects(self): """Test the type info objects behave as expected.""" a = type_info.String() self.assertRaises(type_info.TypeValueError, a.Validate, 1) self.assertRaises(type_info.TypeValueError, a.Validate, None) a.Validate("test") a.Validate(u"test") a.Validate(u"/test-Îñ铁网åţî[öñåļ(îžåţîờñ")
def testStructDefinition(self): """Ensure that errors in struct definitions are raised.""" # A descriptor without a field number should raise. self.assertRaises(type_info.TypeValueError, type_info.ProtoNested, name="name") # Adding a duplicate field number should raise. self.assertRaises( type_info.TypeValueError, TestStruct.AddDescriptor, type_info.ProtoUnsignedInteger(name="int", field_number=2)) # Adding a descriptor which is not a Proto* descriptor is not allowed for # Struct fields: self.assertRaises( type_info.TypeValueError, TestStruct.AddDescriptor, type_info.String(name="int"))
def ExpandArg(self, **_): """Expand the args as a section.parameter from the config.""" # This function is called when we see close ) and the stack depth has to # exactly match the number of (. if len(self.stack) <= 1: raise lexer.ParseError( "Unbalanced parenthesis: Can not expand '%s'" % self.processed_buffer) # This is the full parameter name: e.g. Logging.path parameter_name = self.stack.pop(-1) if "." not in parameter_name: parameter_name = "%s.%s" % (self.default_section, parameter_name) final_value = self.config.Get(parameter_name, context=self.context) if final_value is None: final_value = "" type_info_obj = (self.config.FindTypeInfo(parameter_name) or type_info.String()) # Encode the interpolated string according to its type. self.stack[-1] += type_info_obj.ToString(final_value)
def DEFINE_constant_string(name, default, help): """A helper for defining constant strings.""" CONFIG.AddOption(type_info.String(name=name, default=default or "", description=help), constant=True)
def DEFINE_string(name, default, help): """A helper for defining string options.""" CONFIG.AddOption( type_info.String(name=name, default=default or "", description=help))