Debian Codegraph @

jenkins_jobs/macro.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from dataclasses import dataclass
14 from functools import partial
15
16 from .root_base import ElementBase
17 from .expander import Expander, StringsOnlyExpander
18 from .yaml_objects import BaseYamlObject
19 from .loc_loader import LocDict
20 from .errors import JenkinsJobsException
21 from .position import Pos
22
23
24 macro_specs = [
25 # type_name, elements_name (aka component_type, component_list_type for Registry).
26 ("parameter", "parameters"),
27 ("property", "properties"),
28 ("builder", "builders"),
29 ("wrapper", "wrappers"),
30 ("trigger", "triggers"),
31 ("publisher", "publishers"),
32 ("scm", "scm"),
33 ("pipeline-scm", "pipeline-scm"),
34 ("reporter", "reporters"),
35 ("notification", "notifications"),
36 ]
37
38
39 @dataclass
40 class Macro(ElementBase):
41 _expander: Expander
42 _str_expander: StringsOnlyExpander
43 _type_name: str
44 name: str
45 defaults_name: str
46 pos: Pos
47 elements: list
48
49 @classmethod
50 def add(
51 cls,
52 type_name,
53 elements_name,
54 config,
55 roots,
56 data,
57 pos,
58 ):
59 d = data.copy()
60 name = d.pop_required_loc_string("name")
61 defaults = d.pop_loc_string("defaults", "global")
62 elements = d.pop_required_element(elements_name)
63 params = d
64 expander = Expander(config)
65 str_expander = StringsOnlyExpander(config)
66 macro = cls(
67 _defaults=roots.defaults,
68 _expander=expander,
69 _str_expander=str_expander,
70 _type_name=type_name,
71 name=name,
72 defaults_name=defaults,
73 pos=pos,
74 params=params,
75 elements=elements or [],
76 )
77 roots.assign(roots.macros[type_name], name, macro, "macro")
78
79 def __str__(self):
80 return f"{self._type_name} macro {self.name!r}"
81
82 def dispatch_elements(self, registry, xml_parent, component_data, job_data, params):
83 defaults = self._pick_defaults(self.defaults_name)
84 full_params = LocDict.merge(
85 defaults.params,
86 self.params,
87 params,
88 )
89 element_list = self.elements
90 if isinstance(element_list, BaseYamlObject):
91 # Expand !j2-yaml tag if it is right below macro body.
92 # But do not expand yaml tags inside it - they will be expanded later.
93 element_list = element_list.expand(self._str_expander, full_params)
94 for element in element_list:
95 try:
96 expanded_element = self._expander.expand(element, full_params)
97 except JenkinsJobsException as x:
98 raise x.with_context(
99 f"While expanding {self}",
100 pos=self.pos,
101 )
102 # Pass component_data in as template data to this function
103 # so that if the macro is invoked with arguments,
104 # the arguments are interpolated into the real defn.
105 registry.dispatch(
106 self._type_name,
107 xml_parent,
108 expanded_element,
109 component_data,
110 job_data=job_data,
111 )
112
113
114 macro_adders = {
115 macro_type: partial(Macro.add, macro_type, elements_name)
116 for macro_type, elements_name in macro_specs
117 }