Debian Codegraph @

jenkins_jobs/modules/view_sectioned.py

raw
1 # Copyright 2019 Openstack Foundation
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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 The view sectioned module handles creating Jenkins Sectioned views.
17
18 To create a sectioned view specify ``sectioned`` in the ``view-type`` attribute
19 to the :ref:`view_sectioned` definition.
20
21 :View Parameters:
22 * **name** (`str`): The name of the view.
23 * **view-type** (`str`): The type of view.
24 * **description** (`str`): A description of the view. (default '')
25 * **filter-executors** (`bool`): Show only executors that can
26 execute the included views. (default false)
27 * **filter-queue** (`bool`): Show only included jobs in builder
28 queue. (default false)
29 * **sections** (`list`): The views to show in sections.
30
31 Example:
32
33 .. literalinclude::
34 /../../tests/views/fixtures/view_sectioned.yaml
35
36 """
37
38 import xml.etree.ElementTree as XML
39 import jenkins_jobs.modules.base
40 import jenkins_jobs.modules.helpers as helpers
41 from jenkins_jobs.modules import view_list
42
43
44 class Sectioned(jenkins_jobs.modules.base.Base):
45 def root_xml(self, data):
46 root = XML.Element("hudson.plugins.sectioned__view.SectionedView")
47
48 mapping = [
49 ("name", "name", None),
50 ("description", "description", ""),
51 ("filter-executors", "filterExecutors", False),
52 ("filter-queue", "filterQueue", False),
53 ]
54 helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True)
55
56 XML.SubElement(root, "properties", {"class": "hudson.model.View$PropertyList"})
57
58 s_xml = XML.SubElement(root, "sections")
59 sections = data.get("sections", [])
60
61 for section in sections:
62 if "view-type" not in section:
63 raise KeyError("This sectioned view is missing a view-type.", section)
64 if section["view-type"] == "list":
65 project = view_list.List(self.registry)
66 elif section["view-type"] == "text":
67 project = SectionedText(self.registry)
68 else:
69 raise ValueError(
70 "Nested view-type %s is not known. Supported types: list, text."
71 % section["view-type"]
72 )
73 xml_project = project.root_xml(section, sectioned=True)
74 s_xml.append(xml_project)
75
76 return root
77
78
79 class SectionedText(jenkins_jobs.modules.base.Base):
80 def root_xml(self, data, sectioned=False):
81 assert sectioned
82
83 root = XML.Element("hudson.plugins.sectioned__view.TextSection")
84
85 # these are in the XML and look like from view_list,
86 # but the UI has no controls for them
87 jn_xml = XML.SubElement(root, "jobNames")
88 XML.SubElement(
89 jn_xml, "comparator", {"class": "hudson.util.CaseInsensitiveComparator"}
90 )
91 XML.SubElement(root, "jobFilters")
92
93 mapping = [
94 ("name", "name", ""),
95 ("width", "width", "FULL", ["FULL", "HALF", "THIRD", "TWO_THIRDS"]),
96 ("alignment", "alignment", "CENTER", ["CENTER", "LEFT", "RIGHT"]),
97 ("text", "text", None),
98 ("style", "style", "NONE", ["NONE", "NOTE", "INFO", "WARNING", "TIP"]),
99 ]
100 helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True)
101
102 return root