From 9bfb9dd2e8fa187a36c4ef98a491b9c2944bbd11 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Mon, 18 Apr 2022 22:39:11 -0600 Subject: add meaningful error messages when missing mandatory metadata --- README.md | 2 +- src/pyssg/page.py | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 722ada4..8a56246 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Inspired (initially) by Roman Zolotarev's [`ssg5`](https://rgz.ee/bin/ssg5) and - [x] Use [`configparser`](https://docs.python.org/3/library/configparser.html) instead of custom config handler. - [ ] Migrate to YAML instead of INI, as it is way more flexible. - [x] Avoid the program to freak out when there are directories created in advance. -- [ ] Provide more meaningful error messages when you are missing mandatory tags in your `.md` files. +- [x] Provide more meaningful error messages when you are missing mandatory metadata in your `*.md` files. - [ ] More complex directory structure to support multiple subdomains and different types of pages. - [ ] Add option/change to using an SQL database instead of the custom solution. diff --git a/src/pyssg/page.py b/src/pyssg/page.py index dd3881c..82a0431 100644 --- a/src/pyssg/page.py +++ b/src/pyssg/page.py @@ -1,3 +1,4 @@ +import os import sys from datetime import datetime, timezone import logging @@ -61,15 +62,24 @@ class Page: return self.ctimestamp < other.ctimestamp + def __get_mandatory_meta(self, meta: str) -> str: + try: + log.debug('parsing required metadata "%s"', meta) + return self.meta[meta][0] + except KeyError: + log.error('failed to parse mandatory metadata "%s" from file "%s"', + meta, os.path.join(self.config.get('path', 'src'), self.name)) + sys.exit(1) + + # parses meta from self.meta, for og, it prioritizes, # the actual og meta def parse_metadata(self): log.debug('parsing metadata for file "%s"', self.name) - log.debug('parsing required metadata') - self.title = self.meta['title'][0] - self.author = self.meta['author'][0] - self.summary = self.meta['summary'][0] - self.lang = self.meta['lang'][0] + self.title = self.__get_mandatory_meta('title') + self.author = self.__get_mandatory_meta('author') + self.summary = self.__get_mandatory_meta('summary') + self.lang = self.__get_mandatory_meta('lang') log.debug('parsing timestamp') self.cdatetime = datetime.fromtimestamp(self.ctimestamp, -- cgit v1.2.3-70-g09d2