Skip to content

Commit

Permalink
PyYAML is a shitty library.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Feb 25, 2021
1 parent b084f94 commit d2da8af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
17 changes: 1 addition & 16 deletions Resources/Changelog/Changelog.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
Entries:
- author: PJB
changes:
- {message: Added a changelog. You're reading it right now!, type: Add}
id: 1
time: 2021-02-25 08:50:45
- author: PJB
changes:
- {message: Testing whether changelog works..., type: Fix}
id: 2
time: '2021-02-25T09:10:40.172922+00:00'
- author: PJB
changes:
- {message: Added a changelog. You're reading it right now!, type: Add}
id: 3
time: 2021-02-25 08:50:45
Entries: []
20 changes: 18 additions & 2 deletions Tools/update_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
ENTRY_RE = r"^ *[*-]? *(\S[^\n\r]+)\r?$"


# From https://stackoverflow.com/a/37958106/4678631
class NoDatesSafeLoader(yaml.SafeLoader):
@classmethod
def remove_implicit_resolver(cls, tag_to_remove):
if not 'yaml_implicit_resolvers' in cls.__dict__:
cls.yaml_implicit_resolvers = cls.yaml_implicit_resolvers.copy()

for first_letter, mappings in cls.yaml_implicit_resolvers.items():
cls.yaml_implicit_resolvers[first_letter] = [(tag, regexp)
for tag, regexp in mappings
if tag != tag_to_remove]

# Hrm yes let's make the fucking default of our serialization library to PARSE ISO-8601
# but then output garbage when re-serializing.
NoDatesSafeLoader.remove_implicit_resolver('tag:yaml.org,2002:timestamp')

def main():
parser = argparse.ArgumentParser()
parser.add_argument("changelog_file")
Expand All @@ -21,7 +37,7 @@ def main():
args = parser.parse_args()

with open(args.changelog_file, "r", encoding="utf-8-sig") as f:
current_data = yaml.safe_load(f)
current_data = yaml.load(f, Loader=NoDatesSafeLoader)

entries_list: List[Any]
if current_data is None:
Expand All @@ -38,7 +54,7 @@ def main():
partpath = os.path.join(args.parts_dir, partname)
print(partpath)

partyaml = yaml.safe_load(open(partpath, "r", encoding="utf-8-sig"))
partyaml = yaml.load(open(partpath, "r", encoding="utf-8-sig"), Loader=NoDatesSafeLoader)

author = partyaml["author"]
time = partyaml.get(
Expand Down

0 comments on commit d2da8af

Please sign in to comment.