Skip to content

Commit 2525235

Browse files
add reschema script to utils
1 parent cf42acb commit 2525235

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

utils/reschema/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.py[cod]
2+
__pycache__/
3+
.mypy_cache/
4+
.pytest_cache/
5+
.ruff_cache/
6+
.pdm-python

utils/reschema/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Reschema
2+
3+
Started by jan Kita in https://github.com/kulupu-lapo/poki/pull/36.
4+
5+
Useful for mass-editing the schema.

utils/reschema/main.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import datetime
2+
from pathlib import Path
3+
import re
4+
from ruamel.yaml import (
5+
YAML,
6+
CommentedMap,
7+
CommentedSeq,
8+
) # also requires `pip install ruamel.yaml.string`
9+
import traceback
10+
from tqdm.auto import tqdm
11+
12+
yaml = YAML(typ=["rt", "string"], pure=True)
13+
yaml.default_flow_style = False
14+
yaml.preserve_quotes = True
15+
yaml.sort_base_mapping_type_on_output = False
16+
# yaml.indent(mapping=2, sequence=2, offset=0) # slight majority, also default
17+
# yaml.indent(mapping=2, sequence=4, offset=2) # almost as common as above
18+
# yaml.width = 69420
19+
20+
yaml.representer.add_representer(
21+
type(None),
22+
lambda repr, _data: repr.represent_scalar("tag:yaml.org,2002:null", "null"),
23+
)
24+
25+
26+
def reschema(y):
27+
# # kinda cursed but it minimizes the null edit :3
28+
# # using regex because otherwise hyphens can occur in e.g. the description and make it non-idempotent
29+
if re.search(r"\n[a-z-]+/:\n - ", y):
30+
yaml.indent(mapping=2, sequence=4, offset=2)
31+
else:
32+
yaml.indent(mapping=2, sequence=2, offset=0)
33+
34+
data = yaml.load(y)
35+
36+
def rename(old_key, new_key):
37+
pos = list(data.keys()).index(old_key)
38+
to_rename = data.pop(old_key)
39+
data.insert(pos, new_key, to_rename)
40+
41+
def make_neighbors(anchor_key, move_key):
42+
to_move = data.pop(move_key)
43+
pos = list(data.keys()).index(anchor_key)
44+
data.insert(pos + 1, move_key, to_move)
45+
46+
# rescheming actions go here
47+
# comments might be fragile? use helper functions or refer to https://sourceforge.net/p/ruamel-yaml/code/ci/default/tree/comments.py
48+
49+
if "translators" in data:
50+
data.pop("translators")
51+
52+
# # 1. translator rework
53+
# if "translators" in data:
54+
# if "authors" in data:
55+
# rename("authors", "original-authors")
56+
# rename("translators", "authors")
57+
# if "original-authors" not in data:
58+
# data["original-authors"] = CommentedSeq(["unknown"])
59+
# data["original-authors"].yaml_add_eol_comment(
60+
# "added automatically during reschema", 0
61+
# )
62+
# make_neighbors("authors", "original-authors")
63+
#
64+
# # 2. date precision rework
65+
# if "date" in data and "date-precision" not in data:
66+
# # looking for date-precision comment
67+
# pos = list(data.keys()).index("date")
68+
# k = list(data.keys())[pos + 1]
69+
# if (
70+
# data.ca
71+
# and "date" in data.ca.items
72+
# and "date-precision:" in data.ca.items["date"][2].value
73+
# ):
74+
# data["date-precision"] = (
75+
# data.ca.items["date"][2].value.split(": ")[-1].strip()
76+
# )
77+
# data.ca.items["date"][2] = None
78+
# else:
79+
# match data["date"]:
80+
# case None:
81+
# data["date"] = datetime.date(2001, 1, 1)
82+
# data["date-precision"] = "none"
83+
# case int(): # seems to not be a thing
84+
# data["date"] = datetime.date(data["date"], 1, 1)
85+
# data["date-precision"] = "year"
86+
# case datetime.datetime():
87+
# # do we need to specify day precision?
88+
# pass
89+
# if "date-precision" in data:
90+
# make_neighbors("date", "date-precision")
91+
92+
out = yaml.dump_to_string(data)
93+
return out
94+
95+
96+
for i in tqdm(
97+
list(Path("../../plaintext").glob("**/*.md"))
98+
):
99+
try:
100+
with open(i) as f:
101+
text = f.read()
102+
front, contents = text.split("\n---\n", 1)
103+
front = front[4:]
104+
new_front = reschema(front)
105+
if new_front != front:
106+
with open(i, "w") as f:
107+
f.write(f"---\n{new_front}\n---\n{contents}")
108+
except:
109+
print(i)
110+
traceback.print_exc()

utils/reschema/pdm.lock

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/reschema/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[project]
2+
name = "reschema"
3+
version = "0.1.0"
4+
description = "Default template for PDM package"
5+
authors = [
6+
{name = "AcipenserSturio", email = "AcipenserSturio@users.noreply.github.com"},
7+
]
8+
dependencies = ["ruamel-yaml>=0.18.10", "ruamel-yaml-string>=0.1.1", "tqdm>=4.67.1"]
9+
requires-python = "==3.13.*"
10+
readme = "README.md"
11+
license = {text = "MIT"}
12+
13+
14+
[tool.pdm]
15+
distribution = false

0 commit comments

Comments
 (0)