1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
from strictyaml.ruamel.comments import CommentedSeq, CommentedMap
from strictyaml.exceptions import raise_type_error, YAMLSerializationError
from strictyaml.yamllocation import YAMLChunk
from strictyaml.dumper import StrictYAMLDumper
from strictyaml.ruamel import dump, scalarstring
from copy import copy
import decimal
import sys
if sys.version_info[0] == 3:
unicode = str
if sys.version_info[:2] < (3, 7):
from collections import OrderedDict as OrderedDictBase
class OrderedDict(OrderedDictBase):
def __repr__(self):
return (
"{"
+ ", ".join("{}: {}".format(repr(k), repr(v)) for k, v in self.items())
+ "}"
)
else:
OrderedDict = dict
class YAMLIterator(object):
def __init__(self, yaml_object):
self._yaml_object = yaml_object
self._index = 0
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
if self._index >= len(self._yaml_object):
raise StopIteration
else:
self._index = self._index + 1
return self._yaml_object[self._index - 1]
class YAML(object):
"""
A YAML object represents a block of YAML which can be:
* Used to extract parsed data from the YAML (.data).
* Used to render to a string of YAML, with comments (.as_yaml()).
* Revalidated with a stricter schema (.revalidate(schema)).
"""
def __init__(self, value, validator=None):
if isinstance(value, YAMLChunk):
self._chunk = value
self._validator = validator
if value.is_scalar():
self._value = validator.validate(value)
if isinstance(self._value, YAML):
self._value = self._value._value
self._text = value.contents
else:
self._value = (
value.strictparsed()._value
if isinstance(value.strictparsed(), YAML)
else value.strictparsed()
)
self._text = None
elif isinstance(value, YAML):
self._chunk = value._chunk
self._validator = validator if validator is not None else value.validator
self._value = value._value
self._text = value._text
else:
self._chunk = YAMLChunk(value)
self._validator = validator
self._value = value
self._text = unicode(value)
self._selected_validator = None
assert not isinstance(self._value, YAML)
def __int__(self):
# TODO: Raise more sensible exception if not int
return int(self._value)
def __str__(self):
if not self.is_scalar():
raise TypeError(
"Cannot cast mapping/sequence '{0}' to string".format(repr(self._value))
)
elif type(self._value) in (unicode, str, int, float, decimal.Decimal):
return unicode(self._value)
else:
raise_type_error(
repr(self), "str", "str(yamlobj.data) or str(yamlobj.text)"
)
def __unicode__(self):
return self.__str__()
def revalidate(self, schema):
if self.is_scalar():
self._value = schema(self._chunk)._value
else:
result = schema(self._chunk)
self._selected_validator = result._selected_validator
self._validator = schema
@property
def data(self):
"""
Returns raw data representation of the document or document segment.
Mappings are rendered as ordered dicts, sequences as lists and scalar values
as whatever the validator returns (int, string, etc.).
If no validators are used, scalar values are always returned as strings.
"""
if isinstance(self._value, CommentedMap):
mapping = OrderedDict()
for key, value in self._value.items():
"""
#if isinstance(key, YAML):
#mapping[key.data] = value.data if isinstance(value, YAML) else value
"""
mapping[key.data] = value.data
return mapping
elif isinstance(self._value, CommentedSeq):
return [item.data for item in self._value]
else:
if isinstance(self._value, scalarstring.ScalarString):
return str(self._value)
return self._value
def as_marked_up(self):
"""
Returns strictyaml.ruamel CommentedSeq/CommentedMap objects
with comments. This can be fed directly into a strictyaml.ruamel
dumper.
"""
return self._chunk.contents
@property
def start_line(self):
"""
Return line number that the element starts on (including preceding comments).
"""
return self._chunk.start_line()
@property
def end_line(self):
"""
Return line number that the element ends on (including trailing comments).
"""
return self._chunk.end_line()
def lines(self):
"""
Return a string of the lines which make up the selected line
including preceding and trailing comments.
"""
return self._chunk.lines()
def lines_before(self, how_many):
return self._chunk.lines_before(how_many)
def lines_after(self, how_many):
return self._chunk.lines_after(how_many)
def __float__(self):
return float(self._value)
def __repr__(self):
return "YAML({0})".format(self.data)
def __bool__(self):
if isinstance(self._value, bool):
return self._value
else:
raise_type_error(
repr(self), "bool", "bool(yamlobj.data) or bool(yamlobj.text)"
)
def _strictindex(self, index):
if isinstance(index, YAML):
index = index.data
if self.is_mapping():
key_validator = (
self._selected_validator.key_validator
if self._selected_validator is not None
else self._validator.key_validator
)
return key_validator(YAMLChunk(index)).data
else:
return index
def __nonzero__(self):
return self.__bool__()
def __getitem__(self, index):
return self._value[self._strictindex(index)]
def __setitem__(self, index, value):
strictindex = self._strictindex(index)
# Generate nice error messages - first, copy our whole node's data
# and use ``to_yaml()`` to determine if the resulting data would
# validate our schema. Must replace whole current node to support
# complex types, e.g. ``EmptyList() | Seq(Str())``.
if isinstance(value, YAML):
yaml_value = self._chunk.fork(strictindex, value)
new_value = self._validator(yaml_value)
else:
old_data = self.data
if isinstance(old_data, dict):
old_data[index] = value
elif isinstance(old_data, list):
if len(old_data) <= index:
raise YAMLSerializationError(
"cannot extend list via __setitem__. "
"Instead, replace whole list on parent "
"node."
)
old_data[index] = value
else:
raise NotImplementedError(repr(old_data))
yaml_value = YAMLChunk(self._validator.to_yaml(old_data))
yaml_value_repr = self._validator(yaml_value)
# Now that the new content is properly validated, create a valid
# chunk with the new information.
forked_chunk = self._chunk.fork(strictindex, yaml_value_repr[strictindex])
new_value = self._validator(forked_chunk)
# Now, overwrite our chunk and value with the new information.
old_chunk = self._chunk # Needed for reference to pre-fork ruamel
self._chunk = new_value._chunk
self._value = new_value._value
self._text = new_value._text
self._selected_validator = new_value._selected_validator
# Update any parent ruamel links to point to our new chunk.
self._chunk.pointer.set(old_chunk, "_ruamelparsed", new_value._chunk.contents)
self._chunk.pointer.set(old_chunk, "_strictparsed", self, strictdoc=True)
# forked chunk made a deep copy of the original document, but we just
# updated pointers in the original document. So, restore our chunk to
# pointing at the original document.
self._chunk._ruamelparsed = old_chunk._ruamelparsed
self._chunk._strictparsed = old_chunk._strictparsed
def __delitem__(self, index):
strictindex = self._strictindex(index)
del self._value[strictindex]
del self._chunk.contents[self._chunk.ruamelindex(strictindex)]
def __hash__(self):
return hash(self._value)
def __len__(self):
return len(self._value)
def as_yaml(self):
"""
Render the YAML node and subnodes as string.
"""
dumped = dump(self.as_marked_up(), Dumper=StrictYAMLDumper, allow_unicode=True)
return dumped if sys.version_info[0] == 3 else dumped.decode("utf8")
def items(self):
if not isinstance(self._value, CommentedMap):
raise TypeError("{0} not a mapping, cannot use .items()".format(repr(self)))
return [(key, self._value[key]) for key, value in self._value.items()]
def keys(self):
if not isinstance(self._value, CommentedMap):
raise TypeError("{0} not a mapping, cannot use .keys()".format(repr(self)))
return [key for key, _ in self._value.items()]
def values(self):
if not isinstance(self._value, CommentedMap):
raise TypeError(
"{0} not a mapping, cannot use .values()".format(repr(self))
)
return [self._value[key] for key, value in self._value.items()]
def get(self, index, default=None):
if not isinstance(self._value, CommentedMap):
raise TypeError("{0} not a mapping, cannot use .get()".format(repr(self)))
return self._value[index] if index in self._value.keys() else default
def __contains__(self, item):
if isinstance(self._value, CommentedSeq):
return item in self._value
elif isinstance(self._value, CommentedMap):
return item in self.keys()
else:
return item in self._value
def __iter__(self):
if self.is_sequence():
return YAMLIterator(self)
elif self.is_mapping():
return YAMLIterator(self.keys())
else:
raise TypeError("{0} is a scalar value, cannot iterate.".format(repr(self)))
@property
def validator(self):
return self._validator
@property
def text(self):
"""
Return string value of scalar, whatever value it was parsed as.
"""
if isinstance(self._value, CommentedMap):
raise TypeError("{0} is a mapping, has no text value.".format(repr(self)))
if isinstance(self._value, CommentedSeq):
raise TypeError("{0} is a sequence, has no text value.".format(repr(self)))
return self._text
def copy(self):
return copy(self)
def __gt__(self, val):
if isinstance(self._value, CommentedMap) or isinstance(
self._value, CommentedSeq
):
raise TypeError("{0} not an orderable type.".format(repr(self._value)))
return self._value > val
def __lt__(self, val):
if isinstance(self._value, CommentedMap) or isinstance(
self._value, CommentedSeq
):
raise TypeError("{0} not an orderable type.".format(repr(self._value)))
return self._value < val
@property
def value(self):
return self._value
def is_mapping(self):
return isinstance(self._value, CommentedMap)
def is_sequence(self):
return isinstance(self._value, CommentedSeq)
def is_scalar(self):
return not self.is_mapping() and not self.is_sequence()
@property
def scalar(self):
if isinstance(self._value, (CommentedMap, CommentedSeq)):
raise TypeError("{0} has no scalar value.".format(repr(self)))
return self._value
def whole_document(self):
return self._chunk.whole_document
def __eq__(self, value):
return self.data == value
def __ne__(self, value):
return self.data != value
|