| 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 collections import UserString |
| 14 |
|
| 15 |
import yaml |
| 16 |
|
| 17 |
from .errors import JenkinsJobsException |
| 18 |
from .position import Pos |
| 19 |
|
| 20 |
|
| 21 |
class LocDict(dict): |
| 22 |
"""dict implementation with added source position information""" |
| 23 |
|
| 24 |
def __init__(self, value=None, pos=None, key_pos=None, value_pos=None): |
| 25 |
super().__init__(value or []) |
| 26 |
self.pos = pos |
| 27 |
self.key_pos = key_pos or {} # key -> key pos. |
| 28 |
self.value_pos = value_pos or {} # key -> value pos. |
| 29 |
|
| 30 |
def item_with_pos(self, key): |
| 31 |
value = self[key] # KeyError is propagated from here. |
| 32 |
key_pos = self.key_pos.get(key) |
| 33 |
value_pos = self.value_pos.get(key) |
| 34 |
return (value, key_pos, value_pos) |
| 35 |
|
| 36 |
def pop_loc_string(self, key, default_value): |
| 37 |
value = super().pop(key, default_value) |
| 38 |
if type(value) is str: |
| 39 |
return LocString(value, self.value_pos.get(key)) |
| 40 |
else: |
| 41 |
return value |
| 42 |
|
| 43 |
def pop_required_loc_string(self, name): |
| 44 |
try: |
| 45 |
value = self.pop(name) |
| 46 |
except KeyError: |
| 47 |
raise JenkinsJobsException( |
| 48 |
f"Missing required element: {name!r}", |
| 49 |
pos=self.pos, |
| 50 |
) |
| 51 |
return LocString(value, self.value_pos.get(name)) |
| 52 |
|
| 53 |
def pop_required_element(self, name): |
| 54 |
try: |
| 55 |
return self.pop(name) |
| 56 |
except KeyError: |
| 57 |
raise JenkinsJobsException( |
| 58 |
f"Missing required element: {name!r}", |
| 59 |
pos=self.pos, |
| 60 |
) |
| 61 |
|
| 62 |
def copy(self): |
| 63 |
return LocDict(self, self.pos, self.key_pos, self.value_pos) |
| 64 |
|
| 65 |
def copy_with(self, value): |
| 66 |
return LocDict(value, self.pos, self.key_pos, self.value_pos) |
| 67 |
|
| 68 |
def __setitem__(self, key, value): |
| 69 |
if type(value) is LocString: |
| 70 |
super().__setitem__(key, str(value)) |
| 71 |
self.value_pos[key] = value.pos |
| 72 |
else: |
| 73 |
super().__setitem__(key, value) |
| 74 |
|
| 75 |
def set_item(self, key, value, key_pos, value_pos): |
| 76 |
self[key] = value |
| 77 |
if key_pos: |
| 78 |
self.key_pos[key] = key_pos |
| 79 |
if value_pos: |
| 80 |
self.value_pos[key] = value_pos |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def merge(cls, *args, pos=None): |
| 84 |
result = LocDict(pos=pos) |
| 85 |
for d in args: |
| 86 |
result.update(d) |
| 87 |
if type(d) is cls: |
| 88 |
result.key_pos.update(d.key_pos) |
| 89 |
result.value_pos.update(d.value_pos) |
| 90 |
return result |
| 91 |
|
| 92 |
def update(self, d): |
| 93 |
super().update(d) |
| 94 |
if type(d) is LocDict: |
| 95 |
self.key_pos.update(d.key_pos) |
| 96 |
self.value_pos.update(d.value_pos) |
| 97 |
|
| 98 |
|
| 99 |
class LocList(list): |
| 100 |
"""list implementation with added source position information""" |
| 101 |
|
| 102 |
def __init__(self, value=None, pos=None, value_pos=None): |
| 103 |
if value is None: |
| 104 |
value = [] |
| 105 |
super().__init__(value) |
| 106 |
self.pos = pos |
| 107 |
self.value_pos = value_pos or [None for _ in value] # Value pos list. |
| 108 |
|
| 109 |
def copy(self): |
| 110 |
return LocList(self, self.pos, self.value_pos) |
| 111 |
|
| 112 |
|
| 113 |
class LocString(UserString): |
| 114 |
"""str implementation with added source position information""" |
| 115 |
|
| 116 |
def __init__(self, value="", pos=None): |
| 117 |
super().__init__(value) |
| 118 |
self.pos = pos |
| 119 |
|
| 120 |
|
| 121 |
class LocLoader(yaml.Loader): |
| 122 |
"""Load YAML and store source position information""" |
| 123 |
|
| 124 |
def __init__(self, stream, file_path, line_ofs=0, column_ofs=0): |
| 125 |
super().__init__(stream) |
| 126 |
if file_path: |
| 127 |
# Override one set by yaml Reader. Used to construct marks. |
| 128 |
self.name = file_path |
| 129 |
self._line_ofs = line_ofs |
| 130 |
self._column_ofs = column_ofs |
| 131 |
|
| 132 |
def pos_from_node(self, node): |
| 133 |
return Pos.from_node(node, self._line_ofs, self._column_ofs) |
| 134 |
|
| 135 |
def construct_yaml_map(self, node): |
| 136 |
data = LocDict(pos=self.pos_from_node(node)) |
| 137 |
yield data |
| 138 |
value = self.construct_mapping(node) |
| 139 |
data.update(value) |
| 140 |
data.key_pos.update( |
| 141 |
{ |
| 142 |
key_node.value: self.pos_from_node(key_node) |
| 143 |
for key_node, value_node in node.value |
| 144 |
} |
| 145 |
) |
| 146 |
data.value_pos.update( |
| 147 |
{ |
| 148 |
key_node.value: self.pos_from_node(value_node) |
| 149 |
for key_node, value_node in node.value |
| 150 |
} |
| 151 |
) |
| 152 |
|
| 153 |
def construct_yaml_seq(self, node): |
| 154 |
data = LocList(pos=self.pos_from_node(node)) |
| 155 |
yield data |
| 156 |
data.extend(self.construct_sequence(node)) |
| 157 |
data.value_pos.extend(self.pos_from_node(item_node) for item_node in node.value) |
| 158 |
|
| 159 |
|
| 160 |
LocLoader.add_constructor("tag:yaml.org,2002:map", LocLoader.construct_yaml_map) |
| 161 |
LocLoader.add_constructor("tag:yaml.org,2002:seq", LocLoader.construct_yaml_seq) |