From 8e01bea95d3d94cf85055958b710c8b1a04c13c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 24 Jul 2024 10:21:57 +0200 Subject: [PATCH] refactor metadata line parsing --- jssg/models.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/jssg/models.py b/jssg/models.py index d958da9..33f58d5 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -28,6 +28,11 @@ from django.core.management.commands.runserver import Command as runserver +class EmptyLine(Exception) : + pass +class CommentLine(Exception) : + pass + class Document: """A document. @@ -125,8 +130,14 @@ def load(cls, path: Path) -> "Document": with settings.JFME_DEFAULT_METADATA_PATH.open() as f: for line in f : - key, value = map(str.strip, re.split("[\s]", line, maxsplit=1)) - metadata[key] = value + try : + # Parse a metadata key value pair + key, value = cls.parse_metadata_line(line) + metadata[key] = value + except EmptyLine : # ignore empty lines + continue + except CommentLine : # ignore comment lines + continue with path.open() as f: # States: @@ -150,16 +161,14 @@ def load(cls, path: Path) -> "Document": # Metadata end block found state = 2 else: - if line.strip() == "": # ignore empty lines + try : + # Parse a metadata key value pair + key, value = cls.parse_metadata_line(line) + metadata[key] = value + except EmptyLine : # ignore empty lines continue - if line.startswith("#"): # ignore comment lines + except CommentLine : # ignore comment lines continue - - # Parse a metadata key value pair - # key, value = map(str.strip, line.split("", maxsplit=1)) - key, value = map(str.strip, re.split("[\s]", line, maxsplit=1)) - # FIXME print("KEY {} : {} (line is: {})".format(key, value, line)) - metadata[key] = value elif state == 2: if line.rstrip().startswith("---"): # data end block found @@ -222,7 +231,15 @@ def load_glob( files += (p / dir).glob(glob) # print(files) return map(cls.load, files) - + + @classmethod + def parse_metadata_line(cls, line) : + if line.strip() == "": # ignore empty lines + raise EmptyLine() + if line.startswith("#"): # ignore comment lines + raise CommentLine(line) + # key, value = map(str.strip, line.split("", maxsplit=1)) + return map(str.strip, re.split("[\s]", line, maxsplit=1)) class Page(Document): """A webpage, with a title and some content."""