Debian Codegraph @

jenkins_jobs/modules/base.py

raw
1 # Copyright 2012 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 # Base class for a jenkins_jobs module
16
17 from jenkins_jobs.loc_loader import LocList
18
19
20 class Base(object):
21 """
22 A base class for a Jenkins Job Builder Module.
23
24 The module is initialized before any YAML is parsed.
25
26 :arg ModuleRegistry registry: the global module registry.
27 """
28
29 #: The sequence number for the module. Modules are invoked in the
30 #: order of their sequence number in order to produce consistently
31 #: ordered XML output.
32 sequence = 10
33
34 #: The component type for components of this module. This will be
35 #: used to look for macros (they are defined singularly, and should
36 #: not be plural).
37 #: Set both component_type and component_list_type to None if module
38 #: doesn't have components.
39 component_type = None
40
41 #: The component list type will be used to look up possible
42 #: implementations of the component type via entry points (entry
43 #: points provide a list of components, so it should be plural).
44 #: Set both component_type and component_list_type to None if module
45 #: doesn't have components.
46 component_list_type = None
47
48 def __init__(self, registry):
49 self.registry = registry
50
51 def amend_job_dict(self, job):
52 """This method is called before any XML is generated. By
53 overriding this method, a module may arbitrarily modify a job data
54 structure which will probably be the JJB Job intermediate data dict
55 representation. If it has changed the data structure at all, it must
56 return ``True``, otherwise, it must return ``False``.
57
58 :arg dict job: the intermediate representation of job data
59 loaded from JJB Yaml files after variables interpolation and other
60 yaml expansions.
61
62 :rtype: bool
63 """
64
65 return False
66
67 def gen_xml(self, xml_parent, data):
68 """Update the XML element tree based on YAML data. Override
69 this method to add elements to the XML output. Create new
70 Element objects and add them to the xml_parent. The YAML data
71 structure must not be modified.
72
73 :arg class:`xml.etree.ElementTree` xml_parent: the parent XML element
74 :arg dict data: the YAML data structure
75 """
76
77 pass
78
79 def dispatch_component_list(
80 self, component_type, component_list, xml_parent, job_data=None
81 ):
82 if not component_list:
83 return
84 for idx, component in enumerate(component_list):
85 if isinstance(component_list, LocList):
86 pos = component_list.value_pos[idx]
87 else:
88 pos = None
89 self.registry.dispatch(
90 component_type,
91 xml_parent,
92 component,
93 job_data=job_data,
94 component_pos=pos,
95 )