forked from pycontribs/ruamel-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocinfo.py
67 lines (53 loc) · 1.7 KB
/
docinfo.py
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
from __future__ import annotations
"""
DocInfo
Although it was possible to read tag directives before this, all handle/prefix
pairs for all documents in all streams were stored in one dictionary per
YAML instance, making it impossible to distinguish where such a pair came
from without sublassing the scanner.
ToDo:
DocInfo can be used by a yaml dumper to dump a class
- if connected to the root of a data structure
- if provided to the dumper?
"""
from typing import Optional, Tuple
from dataclasses import dataclass, field, MISSING # NOQA
@dataclass(order=True, frozen=True)
class Version:
major: int
minor: int
# def __repr__(self):
# return f'Version("{self.major}.{self.minor}")'
def version(
major: int | str | Tuple[int, int] | None,
minor: Optional[int] = None,
) -> Optional[Version]:
if major is None:
assert minor is None
return None
if isinstance(major, str):
assert minor is None
parts = major.split('.')
assert len(parts) == 2
return Version(int(parts[0]), int(parts[1]))
elif isinstance(major, tuple):
assert minor is None
assert len(major) == 2
major, minor = major
assert minor is not None
return Version(major, minor)
@dataclass(frozen=True)
class Tag:
handle: str
prefix: str
@dataclass
class DocInfo:
"""
Store document information, can be used for analysis of a loaded YAML document
requested_version: if explicitly set before load
doc_version: from %YAML directive
tags: from %TAG directives in scanned order
"""
requested_version: Optional[Version] = None
doc_version: Optional[Version] = None
tags: list[Tag] = field(default_factory=list)