-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruamelYamlDatasetConvertor.py
executable file
·159 lines (122 loc) · 4.38 KB
/
ruamelYamlDatasetConvertor.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
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
#!/usr/bin/env python3
import datetime
import math
import shutil
import typing
from ast import literal_eval
from datetime import date, timezone
from pathlib import Path
import cbor2
import pytz
import ruamel.yaml
from ruamel.yaml import YAML
from fsutilz import copytree, movetree
def loadYAML(t: typing.Union[str, Path]):
y = YAML(typ="safe")
try:
return False, y.load(t)
except ruamel.yaml.composer.ComposerError as ex:
return True, list(y.load_all(t))
def serializeYAML(res) -> str:
from io import StringIO
y = YAML(typ="unsafe")
y.indent(mapping=2, sequence=4, offset=2)
with StringIO() as f:
y.dump(res, f)
return f.getvalue()
def moveWithAux(targetDir, el, corrData):
if corrData:
for corrEl in corrData:
shutil.move(corrEl, targetDir / el.name)
el.rename(targetDir / el.name)
def convertAndMoveWithAuxDataAutodetectDir(el, dataDir):
corrData = set(el.parent.glob(el.stem + ".*")) - {el}
if len(corrData) == 1:
validDir = dataDir / next(iter(corrData)).suffix[1:]
else:
validDir = dataDir / "valid"
_convertAndMove(el, validDir, corrData)
def convertAndMoveWithAuxDataSameDir(el, validDir):
corrData = set(el.parent.glob(el.stem + ".*")) - {el}
_convertAndMove(el, el.parent, corrData)
def _convertAndMove(el, validDir, corrData):
validMultiDir = validDir / "multi"
unhashableDir = validDir / "unhashable"
ctorErrorDir = validDir / "noCtor"
invalidDir = validDir / "invalid"
invalidCBORDir = validDir / "cborError"
try:
isMulti, d = loadYAML(el.read_bytes().strip())
if isMulti:
validMultiDir.mkdir(parents=True, exist_ok=True)
cborRes = (validDir if not isMulti else validMultiDir) / (el.stem + ".cbor")
try:
cborRes.write_bytes(cbor2.dumps(d, timezone=timezone.utc))
except:
invalidCBORDir.mkdir(parents=True, exist_ok=True)
moveWithAux(invalidCBORDir, el, corrData)
return
except Exception as ex:
print(el)
print(repr(ex))
if "found unhashable key" in str(ex):
unhashableDir.mkdir(parents=True, exist_ok=True)
moveWithAux(unhashableDir, el, corrData)
return
if "could not determine a constructor for the tag" in str(ex):
ctorErrorDir.mkdir(parents=True, exist_ok=True)
moveWithAux(ctorErrorDir, el, corrData)
return
invalidDir.mkdir(parents=True, exist_ok=True)
moveWithAux(invalidDir, el, corrData)
else:
if isMulti:
moveWithAux(validMultiDir, el, corrData)
else:
moveWithAux(validDir, el, corrData)
RUAMEL_YAML_TESTS_DATA_DIR = Path("./ruamel.yaml/")
RUAMEL_YAML_DATA_DIR = Path("./ruamel.yaml.data/")
noDataNames = {"dumper-error", "emitter-error", "former-dumper-error", "former-loader-error", "loader-error", "loader-warning", "stream-error", "single-loader-error", "recursive"}
uselessFiles = {"structure", "unicode", "skip-ext", "roundtrip", "path", "events", "marks", "recursive"}
def convertRuamelTestsDirIntoFTS(dataDir):
for ext in noDataNames:
subDirName = dataDir / ext
subDirName.mkdir(parents=True, exist_ok=True)
for f in dataDir.glob("*." + ext):
shutil.move(f, subDirName / f.name)
for uselessExt in uselessFiles:
for f in dataDir.glob("*." + uselessExt):
f.unlink()
for el in sorted(set(dataDir.glob("*.data"))):
convertAndMoveWithAuxDataAutodetectDir(el, dataDir)
errorsDir = dataDir / "errors"
errorsDir.mkdir(parents=True, exist_ok=True)
for el in noDataNames:
if el.endswith("-error"):
errKind = el[: -len("-error")]
(dataDir / el).rename(errorsDir / errKind)
(dataDir / "error").rename(errorsDir / "error")
validDir = dataDir / "valid"
for f in dataDir.glob("*.data"):
convertAndMoveWithAuxDataSameDir(f, validDir)
for ext in noDataNames:
for f in (dataDir / ext).glob("*/*." + ext):
newName = f.parent / (f.stem + ".yaml")
f.rename(newName)
f = newName
convertAndMoveWithAuxDataSameDir(f, dataDir)
#for f in (dataDir / "recursive").glob("*.recursive"):
# res = {}
# exec(f.read_text(), res)
# d = res["value"]
# (f.parent / (f.stem + ".cbor")).write_bytes(cbor2.dumps(d, timezone=timezone.utc, value_sharing=True))
# (f.parent / (f.stem + ".yaml")).write_text(serializeYAML(d))
movetree(dataDir / "code", validDir)
for f in dataDir.glob("**/*.data"):
f.rename(f.parent / (f.stem + ".yaml"))
def convertRuamelYamlDataIntoFTS(dataDir):
for f in dataDir.glob("*.yaml"):
convertAndMoveWithAuxDataSameDir(f, f.parent)
if __name__ == "__main__":
convertRuamelTestsDirIntoFTS(RUAMEL_YAML_TESTS_DATA_DIR)
#convertRuamelYamlDataIntoFTS(RUAMEL_YAML_DATA_DIR)