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 --- src/pyssg/page.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src') 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