|
5 | 5 |
|
6 | 6 |
|
7 | 7 | class ArtifactDefinition: |
8 | | - """Artifact definition interface. |
| 8 | + """Artifact definition interface. |
9 | 9 |
|
10 | | - Attributes: |
11 | | - aliases (list[str]): aliases that identify the artifact definition. |
12 | | - description (str): description. |
13 | | - name (str): name that uniquely identifiers the artifact definition. |
14 | | - sources (list[SourceType]): sources. |
15 | | - supported_os (list[str]): supported operating systems. |
16 | | - urls (list[str]): URLs with more information about the artifact definition. |
17 | | - """ |
18 | | - |
19 | | - def __init__(self, name, aliases=None, description=None): |
20 | | - """Initializes an artifact definition. |
21 | | -
|
22 | | - Args: |
| 10 | + Attributes: |
| 11 | + aliases (list[str]): aliases that identify the artifact definition. |
| 12 | + description (str): description. |
23 | 13 | name (str): name that uniquely identifiers the artifact definition. |
24 | | - aliases (Optional[str]): aliases that identify the artifact definition. |
25 | | - description (Optional[str]): description of the artifact definition. |
| 14 | + sources (list[SourceType]): sources. |
| 15 | + supported_os (list[str]): supported operating systems. |
| 16 | + urls (list[str]): URLs with more information about the artifact definition. |
26 | 17 | """ |
27 | | - super().__init__() |
28 | | - self.aliases = aliases or [] |
29 | | - self.description = description |
30 | | - self.name = name |
31 | | - self.sources = [] |
32 | | - self.supported_os = [] |
33 | | - self.urls = [] |
34 | | - |
35 | | - def AppendSource(self, type_indicator, attributes): |
36 | | - """Appends a source. |
37 | | -
|
38 | | - If you want to implement your own source type you should create a subclass |
39 | | - in source_type.py and change the AppendSource method to handle the new |
40 | | - subclass. This function raises FormatError if an unsupported source type |
41 | | - indicator is encountered. |
42 | | -
|
43 | | - Args: |
44 | | - type_indicator (str): source type indicator. |
45 | | - attributes (dict[str, object]): source attributes. |
46 | | -
|
47 | | - Returns: |
48 | | - SourceType: a source type. |
49 | | -
|
50 | | - Raises: |
51 | | - FormatError: if the type indicator is not set or unsupported, |
52 | | - or if required attributes are missing. |
53 | | - """ |
54 | | - if not type_indicator: |
55 | | - raise errors.FormatError('Missing type indicator.') |
56 | | - |
57 | | - try: |
58 | | - source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( |
59 | | - type_indicator, attributes) |
60 | | - except (AttributeError, TypeError) as exception: |
61 | | - raise errors.FormatError(( |
62 | | - f'Unable to create source type: {type_indicator:s} for artifact ' |
63 | | - f'definition: {self.name:s} with error: {exception!s}')) |
64 | 18 |
|
65 | | - self.sources.append(source_object) |
66 | | - return source_object |
67 | | - |
68 | | - def AsDict(self): |
69 | | - """Represents an artifact as a dictionary. |
70 | | -
|
71 | | - Returns: |
72 | | - dict[str, object]: artifact attributes. |
73 | | - """ |
74 | | - sources = [] |
75 | | - for source in self.sources: |
76 | | - source_definition = { |
77 | | - 'type': source.type_indicator, |
78 | | - 'attributes': source.AsDict() |
79 | | - } |
80 | | - if source.supported_os: |
81 | | - source_definition['supported_os'] = source.supported_os |
82 | | - sources.append(source_definition) |
83 | | - |
84 | | - artifact_definition = { |
85 | | - 'name': self.name, |
86 | | - 'doc': self.description, |
87 | | - 'sources': sources, |
88 | | - } |
89 | | - if self.aliases: |
90 | | - artifact_definition['aliases'] = self.aliases |
91 | | - if self.supported_os: |
92 | | - artifact_definition['supported_os'] = self.supported_os |
93 | | - if self.urls: |
94 | | - artifact_definition['urls'] = self.urls |
95 | | - return artifact_definition |
| 19 | + def __init__(self, name, aliases=None, description=None): |
| 20 | + """Initializes an artifact definition. |
| 21 | +
|
| 22 | + Args: |
| 23 | + name (str): name that uniquely identifiers the artifact definition. |
| 24 | + aliases (Optional[str]): aliases that identify the artifact definition. |
| 25 | + description (Optional[str]): description of the artifact definition. |
| 26 | + """ |
| 27 | + super().__init__() |
| 28 | + self.aliases = aliases or [] |
| 29 | + self.description = description |
| 30 | + self.name = name |
| 31 | + self.sources = [] |
| 32 | + self.supported_os = [] |
| 33 | + self.urls = [] |
| 34 | + |
| 35 | + def AppendSource(self, type_indicator, attributes): |
| 36 | + """Appends a source. |
| 37 | +
|
| 38 | + If you want to implement your own source type you should create a subclass |
| 39 | + in source_type.py and change the AppendSource method to handle the new |
| 40 | + subclass. This function raises FormatError if an unsupported source type |
| 41 | + indicator is encountered. |
| 42 | +
|
| 43 | + Args: |
| 44 | + type_indicator (str): source type indicator. |
| 45 | + attributes (dict[str, object]): source attributes. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + SourceType: a source type. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + FormatError: if the type indicator is not set or unsupported, |
| 52 | + or if required attributes are missing. |
| 53 | + """ |
| 54 | + if not type_indicator: |
| 55 | + raise errors.FormatError("Missing type indicator.") |
| 56 | + |
| 57 | + try: |
| 58 | + source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( |
| 59 | + type_indicator, attributes |
| 60 | + ) |
| 61 | + except (AttributeError, TypeError) as exception: |
| 62 | + raise errors.FormatError( |
| 63 | + ( |
| 64 | + f"Unable to create source type: {type_indicator:s} for artifact " |
| 65 | + f"definition: {self.name:s} with error: {exception!s}" |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + self.sources.append(source_object) |
| 70 | + return source_object |
| 71 | + |
| 72 | + def AsDict(self): |
| 73 | + """Represents an artifact as a dictionary. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + dict[str, object]: artifact attributes. |
| 77 | + """ |
| 78 | + sources = [] |
| 79 | + for source in self.sources: |
| 80 | + source_definition = { |
| 81 | + "type": source.type_indicator, |
| 82 | + "attributes": source.AsDict(), |
| 83 | + } |
| 84 | + if source.supported_os: |
| 85 | + source_definition["supported_os"] = source.supported_os |
| 86 | + sources.append(source_definition) |
| 87 | + |
| 88 | + artifact_definition = { |
| 89 | + "name": self.name, |
| 90 | + "doc": self.description, |
| 91 | + "sources": sources, |
| 92 | + } |
| 93 | + if self.aliases: |
| 94 | + artifact_definition["aliases"] = self.aliases |
| 95 | + if self.supported_os: |
| 96 | + artifact_definition["supported_os"] = self.supported_os |
| 97 | + if self.urls: |
| 98 | + artifact_definition["urls"] = self.urls |
| 99 | + return artifact_definition |
0 commit comments