| 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 |
|
| 15 |
from .errors import JenkinsJobsException |
| 16 |
from .loc_loader import LocDict |
| 17 |
from .expander import Expander |
| 18 |
from .root_base import RootBase, NonTemplateRootMixin, TemplateRootMixin, Group |
| 19 |
from .defaults import split_contents_params, view_contents_keys |
| 20 |
|
| 21 |
|
| 22 |
@dataclass |
| 23 |
class ViewBase(RootBase): |
| 24 |
view_type: str |
| 25 |
|
| 26 |
@classmethod |
| 27 |
def from_dict(cls, config, roots, data, pos): |
| 28 |
keep_descriptions = config.yamlparser["keep_descriptions"] |
| 29 |
d = data.copy() |
| 30 |
name = d.pop_required_loc_string("name") |
| 31 |
id = d.pop_loc_string("id", None) |
| 32 |
description = d.pop_loc_string("description", None) |
| 33 |
defaults = d.pop_loc_string("defaults", "global") |
| 34 |
view_type = d.pop_loc_string("view-type", "list") |
| 35 |
contents, params = split_contents_params(d, view_contents_keys) |
| 36 |
return cls( |
| 37 |
_defaults=roots.defaults, |
| 38 |
_expander=Expander(config), |
| 39 |
_keep_descriptions=keep_descriptions, |
| 40 |
_id=id, |
| 41 |
name=name, |
| 42 |
pos=pos, |
| 43 |
description=description, |
| 44 |
defaults_name=defaults, |
| 45 |
params=params, |
| 46 |
_contents=contents, |
| 47 |
view_type=view_type, |
| 48 |
) |
| 49 |
|
| 50 |
@property |
| 51 |
def contents(self): |
| 52 |
return LocDict.merge( |
| 53 |
{ |
| 54 |
"name": self.name, |
| 55 |
"view-type": self.view_type, |
| 56 |
}, |
| 57 |
super().contents, |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
class View(ViewBase, NonTemplateRootMixin): |
| 62 |
@classmethod |
| 63 |
def add(cls, config, roots, data, pos): |
| 64 |
view = cls.from_dict(config, roots, data, pos) |
| 65 |
roots.assign(roots.views, view.id, view, "view") |
| 66 |
|
| 67 |
def __str__(self): |
| 68 |
return f"view {self.name!r}" |
| 69 |
|
| 70 |
|
| 71 |
class ViewTemplate(ViewBase, TemplateRootMixin): |
| 72 |
@classmethod |
| 73 |
def add(cls, config, roots, data, pos): |
| 74 |
template = cls.from_dict(config, roots, data, pos) |
| 75 |
roots.assign(roots.view_templates, template.id, template, "view template") |
| 76 |
|
| 77 |
def __str__(self): |
| 78 |
return f"view template {self.name!r}" |
| 79 |
|
| 80 |
|
| 81 |
@dataclass |
| 82 |
class ViewGroup(Group): |
| 83 |
_views: dict |
| 84 |
_view_templates: dict |
| 85 |
|
| 86 |
@classmethod |
| 87 |
def add(cls, config, roots, data, pos): |
| 88 |
d = data.copy() |
| 89 |
name = d.pop_required_loc_string("name") |
| 90 |
try: |
| 91 |
view_specs = cls._specs_from_list(d.pop("views", None)) |
| 92 |
except JenkinsJobsException as x: |
| 93 |
raise x.with_context(f"In view {name!r}", pos=pos) |
| 94 |
group = cls( |
| 95 |
name, |
| 96 |
pos, |
| 97 |
view_specs, |
| 98 |
d, |
| 99 |
roots.views, |
| 100 |
roots.view_templates, |
| 101 |
) |
| 102 |
roots.assign(roots.view_groups, group.name, group, "view group") |
| 103 |
|
| 104 |
def __str__(self): |
| 105 |
return f"view group {self.name!r}" |
| 106 |
|
| 107 |
@property |
| 108 |
def _root_dicts(self): |
| 109 |
return [self._views, self._view_templates] |