def test_Uri(self): self.assertEqual( StringSerialization.DeserializeItem(UriTypeInfo(), "https://foo.bar.baz"), Uri.FromString("https://foo.bar.baz")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( UriTypeInfo(), "not a valid uri"))
def test_Date(self): self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "2018-04-28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04-28-2018"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "18-04-28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04-28-18"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "2018.04.28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04.28.2018"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "18.04.28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04.28.18"), datetime.date(year=2018, month=4, day=28)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DateTimeTypeInfo(), "2018-04.28"))
def test_Filename(self): self.assertEqual( StringSerialization.DeserializeItem( FilenameTypeInfo(ensure_exists=False), "foo/bar"), os.path.realpath(os.path.normpath(os.path.join("foo", "bar")))) self.assertEqual( StringSerialization.DeserializeItem( FilenameTypeInfo(ensure_exists=False), "foo/bar", normalize=False), os.path.join("foo", "bar")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DirectoryTypeInfo(), ''))
def test_Int(self): self.assertEqual( StringSerialization.DeserializeItem(IntTypeInfo(), "10"), 10) self.assertEqual( StringSerialization.DeserializeItem(IntTypeInfo(), "-10"), -10) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(), "not a valid int")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(min=0), "-10")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(max=10), "100"))
def test_String(self): self.assertEqual( StringSerialization.DeserializeItem(StringTypeInfo(), "foo"), "foo") self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem(StringTypeInfo(), "")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(min_length=2), "1")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(max_length=2), "123")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(validation_expression="test"), "not test"))
def test_Time(self): self.assertEqual( StringSerialization.DeserializeItem(TimeTypeInfo(), "10:15:01"), datetime.time(hour=10, minute=15, second=1)) self.assertEqual( StringSerialization.DeserializeItem(TimeTypeInfo(), "10:15:01.678"), datetime.time(hour=10, minute=15, second=1, microsecond=678000)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "not a valid time")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "10:15:01+10:30")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "10:15:01-03:13"))
def test_Enum(self): self.assertEqual( StringSerialization.DeserializeItem(EnumTypeInfo([ "one", "two", ]), "one"), "one") self.assertEqual( StringSerialization.DeserializeItem(EnumTypeInfo([ "one", "two", ]), "two"), "two") self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( EnumTypeInfo([ "one", "two", ]), "three"))
def _CreateContext(cls, metadata, status_stream): # Ensure that all plugin settings are present and that they are the # expected type. custom_settings = OrderedDict([ (k, v) for k, v in plugin.GenerateCustomMetadataSettingsAndDefaults() ]) assert "plugin_settings" in metadata plugin_settings = metadata["plugin_settings"] for k, v in six.iteritems(plugin_settings): if k not in custom_settings: raise Exception( "'{}' is not a valid plugin setting".format(k)) desired_type = type(custom_settings[k]) if type( v ) != desired_type: # <prefer isinstance> pylint: disable = C0123 assert isinstance(v, six.string_types), (v, type(v)) plugin_settings[k] = StringSerialization.DeserializeItem( CreateFromPythonType(desired_type), v) for k, v in six.iteritems(custom_settings): if k not in plugin_settings: plugin_settings[k] = v # Ensure that the required metadata is present for required in plugin.GetRequiredMetadataNames(): if required not in plugin_settings: raise Exception( "'{}' is required but was not found".format(required)) metadata["plugin_settings"] = plugin.PreprocessMetadata( plugin_settings) context = plugin.PreprocessContext(metadata) # Create data based on the input files context["plugin_context"] = ExtractContent( context["inputs"], status_stream, ) context["output_filenames"] = [ os.path.join(context["output_dir"], filename) for filename in plugin.GenerateOutputFilenames(context) ] context = plugin.PostprocessContext(context) del context["include_regexes"] del context["exclude_regexes"] return super(CodeGenerator, cls)._CreateContext(context, status_stream)
def _filter_Item(cls, item, always_include_optional, process_additional_data): attributes = OrderedDict() # continue_processing try: cls._ApplyOptionalAttribute( item, "continue_processing", attributes, cls.filter_continue_processing, always_include_optional, default_value_func=lambda: StringSerialization.DeserializeItem( _filter_continue_processing_TypeInfo, "true")) except: _DecorateActiveException("continue_processing") result = _CreatePythonObject(attributes=attributes, ) # includes try: cls._ApplyOptionalChildren(item, "includes", result, cls.filter_includes, always_include_optional) except: _DecorateActiveException("includes") # excludes try: cls._ApplyOptionalChildren(item, "excludes", result, cls.filter_excludes, always_include_optional) except: _DecorateActiveException("excludes") # Additional data if process_additional_data: cls._ApplyAdditionalData( item, result, exclude_names={"includes", "excludes", "continue_processing"}, ) else: cls._RejectAdditionalData( item, exclude_names=["includes", "excludes", "continue_processing"], ) _filter_TypeInfo.ValidateItem( result, recurse=False, require_exact_match=not process_additional_data, ) return result
def _ValidateMetadata(filter_unsupported_metadata, item): for item in item.Enumerate(): # Ensure that required values are present for md in item.metadata.RequiredItems: if md.Name not in item.metadata.Values: raise Exceptions.ValidateMissingAttributeException( item.Source, item.Line, item.Column, name=md.Name, ) # Verify / eliminate / Convert extra metadata md_lookup = { md.Name: md for md in itertools.chain(item.metadata.RequiredItems, item.metadata.OptionalItems) } md_keys = list(six.iterkeys(item.metadata.Values)) for k in md_keys: if k not in md_lookup: if filter_unsupported_metadata: del item.metadata.Values[k] continue raise Exceptions.ValidateExtraneousAttributeException( item.Source, item.Line, item.Column, name=k, ) md = md_lookup[k] value = item.metadata.Values[k].Value try: if isinstance(value, six.string_types): value = StringSerialization.DeserializeItem( md.TypeInfo, value) else: md.TypeInfo.Validate(value) except ValidationException as ex: raise Exceptions.ValidateInvalidAttributeException( item.Source, item.Line, item.Column, name=k, reason=str(ex), ) item.metadata.Values[k] = item.metadata.Values[k]._replace( Value=value, )
def test_Guid(self): self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "{9A3A5A9A-0755-4FAC-9019-176C609665C5}"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "9A3A5A9A-0755-4FAC-9019-176C609665C5"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "{9A3A5A9A07554FAC9019176C609665C5}"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "9A3A5A9A07554FAC9019176C609665C5"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( GuidTypeInfo(), "not a valid guid"))
def _CreateContext(cls, metadata, status_stream): if metadata["plugin_name"] not in plugin_map: raise CommandLine.UsageException( "'{}' is not a valid plugin".format( metadata["plugin_name"])) plugin = plugin_map[metadata["plugin_name"]].Plugin # Ensure that all plugin settings are present and that they # are the expected type. custom_settings = OrderedDict([ (k, v) for k, v in plugin.GenerateCustomSettingsAndDefaults() ]) plugin_settings = metadata["plugin_settings"] for k, v in six.iteritems(plugin_settings): if k not in custom_settings: raise CommandLine.UsageException( "'{}' is not a valid plugin setting".format(k)) desired_type = type(custom_settings[k]) if type(v) != desired_type: assert isinstance(v, (str, UnicodeDecodeError)), (v, type(v)) plugin_settings[k] = StringSerialization.DeserializeItem( CreateFromPythonType(desired_type), v) for k, v in six.iteritems(custom_settings): if k not in plugin_settings: plugin_settings[k] = v metadata["plugin_settings"] = plugin.PreprocessMetadata( plugin_settings) # Invoke custom functionality context = create_context_func(metadata, plugin) context = plugin.PreprocessContext(context) context["output_filenames"] = [ os.path.join(context["output_dir"], filename) for filename in plugin.GenerateOutputFilenames(context) ] context = plugin.PostprocessContext(context) if postprocess_context_func: context = postprocess_context_func(context, plugin) return super(CodeGenerator, cls)._CreateContext(context, status_stream)
def IsList(): # This item is a list if it meets the following conditions: # # 1) An arity was explicitly provided # 2) The arity is a collection # 3) The referenced item (or one of its descendants) has an arity # 4) The referenced item's arity (or one of its descendants) is a collection. # 5) 'refines_arity' is not provided or set to False # 1 and 2 if item.arity is None or not item.arity.IsCollection: return False # 3 and 4 ref = reference while ref.element_type == Elements.ReferenceElement and ref.arity is None: assert len(ref.references) == 1, ref.references ref = ref.references[0] if ref.arity is None or not ref.arity.IsCollection: return False # 5 if Attributes.COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME in item.metadata.Values: refines_arity = item.metadata.Values[ Attributes. COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME].Value del item.metadata.Values[ Attributes.COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME] try: refines_arity = StringSerialization.DeserializeItem( BoolTypeInfo(), refines_arity) except ValidationException as ex: raise Exceptions.InvalidAttributeException( item.Source, item.Line, item.Column, desc="'{}' is not valid: {}".fromat( Attributes. COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME, str(ex)), ) if refines_arity: return False return True
def _Collectionize(element, schema): arity = element.TypeInfo.Arity if arity.Max == 1: if arity.Min == 0 and hasattr(element, "default"): schema["default"] = StringSerialization.DeserializeItem( element.TypeInfo, element.default) return schema schema = {"type": "array", "items": schema} if arity.Min != 0: schema["minItems"] = arity.Min if arity.Max is not None: schema["maxItems"] = arity.Max return schema
def test_DateTime(self): self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "2018-04-28 10:05:00"), datetime.datetime(year=2018, month=4, day=28, hour=10, minute=5, second=0)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "2018-04-28T10:05:00"), datetime.datetime(year=2018, month=4, day=28, hour=10, minute=5, second=0)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "1528171793"), datetime.datetime(2018, 6, 5, 4, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 -0700"), datetime.datetime(2018, 6, 5, 11, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 +0700"), datetime.datetime(2018, 6, 4, 21, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 0700"), datetime.datetime(2018, 6, 4, 21, 9, 53)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DateTimeTypeInfo(), "not a valid datetime"))
def test_Duration(self): self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1.02:03:04"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1:02:03:04"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "02:03:04"), datetime.timedelta(hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1.02:03:04.5"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1:02:03:04.5"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "02:03:04.5"), datetime.timedelta(hours=2, minutes=3, seconds=4, microseconds=5)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DurationTypeInfo(), "not a valid duration"))
def test_Bool(self): self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "true")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "t")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "yes")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "y")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "1")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "false")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "f")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "no")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "n")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "0")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( BoolTypeInfo(), "not_valid"))
def ApplyImpl(parameter, arg): if isinstance(parameter.TypeInfo, DictTypeInfo): # Add the dictionary values; we will validate later. match = self._dict_regex.match(arg) if not match: return "'{}' is not a valid dictionary entry".format(arg) tag = match.group("tag") tag = tag.replace( "\\{}".format(self.CommandLineDictTagValueSeparator), self.CommandLineDictTagValueSeparator) value = match.group("value") argument_values.setdefault(parameter.Name, OrderedDict()) if tag not in argument_values[parameter.Name]: if parameter.AllowDuplicates: argument_values[parameter.Name][tag] = [ value, ] else: argument_values[parameter.Name][tag] = value else: if not parameter.AllowDuplicates: return "A value for '{}'s tag '{}' has already been provided ({})".format( parameter.Name, tag, argument_values[parameter.Name][tag], ) argument_values[parameter.Name][tag].append(value) return None if parameter.IsSwitch: # Preserve the value as a string so it can be deserialized below arg = StringSerialization.SerializeItem( parameter.TypeInfo, not parameter.DefaultValue) try: if isinstance(parameter.TypeInfo, tuple(ALL_FUNDAMENTAL_TYPES)): value = StringSerialization.DeserializeItem( parameter.TypeInfo, arg) elif isinstance(parameter.TypeInfo, AnyOfTypeInfo): found = False for eti in parameter.TypeInfo.ElementTypeInfos: try: value = StringSerialization.DeserializeItem( eti, arg) found = True break except ValidationException: pass if not found: value = arg else: value = arg if parameter.PostprocessFunc: value = parameter.PostprocessFunc(value) except ValidationException as ex: return str(ex) if parameter.DisplayArity in [ '?', '1', ]: if parameter.Name in argument_values: return "A value for '{}' has already been provided ({})".format( parameter.Name, argument_values[parameter.Name], ) argument_values[parameter.Name] = value elif parameter.DisplayArity in [ '*', '+', ]: argument_values.setdefault(parameter.Name, []).append(value) else: assert False return None