Debian Codegraph @

artifacts/artifact.py

raw
1 # -*- coding: utf-8 -*-
2 """The artifact definition."""
3
4 from artifacts import errors
5 from artifacts import registry
6
7
8 class ArtifactDefinition(object):
9 """Artifact definition interface.
10
11 Attributes:
12 aliases (list[str]): aliases that identify the artifact definition.
13 description (str): description.
14 name (str): name that uniquely identifiers the artifact definition.
15 provides (list[str]): hints to what information the artifact definition
16 provides.
17 sources (list[str]): sources.
18 supported_os (list[str]): supported operating systems.
19 urls (list[str]): URLs with more information about the artifact definition.
20 """
21
22 def __init__(self, name, aliases=None, description=None):
23 """Initializes an artifact definition.
24
25 Args:
26 name (str): name that uniquely identifiers the artifact definition.
27 aliases (Optional[str]): aliases that identify the artifact definition.
28 description (Optional[str]): description of the artifact definition.
29 """
30 super(ArtifactDefinition, self).__init__()
31 self.aliases = aliases or []
32 self.description = description
33 self.name = name
34 self.provides = []
35 self.sources = []
36 self.supported_os = []
37 self.urls = []
38
39 def AppendSource(self, type_indicator, attributes):
40 """Appends a source.
41
42 If you want to implement your own source type you should create a subclass
43 in source_type.py and change the AppendSource method to handle the new
44 subclass. This function raises FormatError if an unsupported source type
45 indicator is encountered.
46
47 Args:
48 type_indicator (str): source type indicator.
49 attributes (dict[str, object]): source attributes.
50
51 Returns:
52 SourceType: a source type.
53
54 Raises:
55 FormatError: if the type indicator is not set or unsupported,
56 or if required attributes are missing.
57 """
58 if not type_indicator:
59 raise errors.FormatError('Missing type indicator.')
60
61 try:
62 source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType(
63 type_indicator, attributes)
64 except (AttributeError, TypeError) as exception:
65 raise errors.FormatError((
66 f'Unable to create source type: {type_indicator:s} for artifact '
67 f'definition: {self.name:s} with error: {exception!s}'))
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.provides:
98 artifact_definition['provides'] = self.provides
99 if self.urls:
100 artifact_definition['urls'] = self.urls
101 return artifact_definition