From e97917ba7794fff78fbd999bc1d4da8e901ddfe5 Mon Sep 17 00:00:00 2001 From: Hugo Lefeuvre Date: Mon, 1 Oct 2018 15:16:17 -0400 Subject: [PATCH] improve tests, style updates --- juliet/__init__.py | 17 +++++-------- juliet/pageprocessor.py | 29 ++++++++++++++------- juliet/tests/test_new_article_feature.py | 32 ++++++++++++------------ 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/juliet/__init__.py b/juliet/__init__.py index 6cd95ea..ceda36e 100644 --- a/juliet/__init__.py +++ b/juliet/__init__.py @@ -118,21 +118,19 @@ def __check_key(key): return True result = {} - if (len(header_entries) < 1): return result # Remove leading '--' header_entries = header_entries[1:] + if (not len(header_entries) % 2 == 0): + raise ValueError("last key does not have a value") while (len(header_entries)): # Retrieve raw key word = header_entries[0] header_entries = header_entries[1:] - if (not len(header_entries)): - raise ValueError("last key does not have a value") - # Try to trim equal if (word[-1] == ':'): word = word[:-1] @@ -158,12 +156,6 @@ def _get_article_path(args, user_config, processed_entries): return os.path.join(args.src, paths.POSTS_BUILDDIR, article_filename) -def _get_new_article(final_header): - """ Return default article matching passed args. """ - - default_article = "---\n" + yaml.dump(final_header, default_flow_style=False) + "---" - return default_article - def _process_header_dict(theme_config, parsed_entries): """ TODO """ @@ -193,12 +185,15 @@ def init_new_article(args): def _apply_theme_configuration(theme_config, processed_entries): result = {} - for key, value in theme_config.items(): result[key] = processed_entries[value[0]] return result + def _get_new_article(final_header): + default_article = "---\n" + yaml.dump(final_header, default_flow_style=False) + "---" + return default_article + # Get configs user_config = configurator.get_config(os.path.join(args.src, paths.CFG_FILE)) if (not user_config): diff --git a/juliet/pageprocessor.py b/juliet/pageprocessor.py index b9011e4..47c4176 100644 --- a/juliet/pageprocessor.py +++ b/juliet/pageprocessor.py @@ -181,7 +181,18 @@ def _get_header_limit(splitted_file): raise ValueError("Failed to parse header: Header never closed") - def process(self, raw_file, filename): + @staticmethod + def _get_parsed_header(raw_file, filename, file_naming_var): + """ TODO """ + + splitted_file = raw_file.splitlines() + + # Find header, check and process it + header_limit = PageProcessor._get_header_limit(splitted_file) + return PageProcessor._process_header("\n" + .join(splitted_file[1:header_limit]), filename, file_naming_var) + + def process(self, rawfile, filename): """ Return a parsed form of passed file. ### 1) Format @@ -222,17 +233,17 @@ def process(self, raw_file, filename): would be returned as {"key": "value", "body": "

bodyContent

", "slug": filename}. """ - result = {} - splitted_file = raw_file.splitlines() + def _get_body(raw_file, filename, file_naming_var): + # Find header, check and process it + splitted_file = rawfile.splitlines() + header_limit = PageProcessor._get_header_limit(splitted_file) + return splitted_file[header_limit+1:] - # Find header, check and process it - header_limit = PageProcessor._get_header_limit(splitted_file) - parsed_header = PageProcessor._process_header("\n".join(splitted_file[1:header_limit]), filename, self.file_naming_var) - result.update(parsed_header) + result = PageProcessor._get_parsed_header(rawfile, filename, self.file_naming_var) # Find body, process it - splitted_body = splitted_file[header_limit+1:] - result["body"] = PageProcessor._process_body(splitted_body, self.baseurl) + body = _get_body(rawfile, filename, self.file_naming_var) + result["body"] = PageProcessor._process_body(body, self.baseurl) result["file-name"] = filename diff --git a/juliet/tests/test_new_article_feature.py b/juliet/tests/test_new_article_feature.py index 65c7fb1..45dcd89 100644 --- a/juliet/tests/test_new_article_feature.py +++ b/juliet/tests/test_new_article_feature.py @@ -36,13 +36,17 @@ def test_parse_broken_raw_header_entries(self): args_missing_value = ["--", "title"] self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_missing_value) - # Missing key - args_missing_key = ["--", ":", "title"] - self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_missing_key) + # Invalid key #1 + args_invalid_key = ["--", "::", "hello"] + self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_invalid_key) - # Missing key and value - args_missing_value_and_key = ["--", ":"] - self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_missing_value_and_key) + # Invalid key #2 + args_invalid_key = ["--", "title_of_the_death:", "hello"] + self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_invalid_key) + + # Invalid key #3 (empty key) + args_empty_key = ["--", ":", "title"] + self.assertRaises(ValueError, juliet._parse_raw_header_entries, args_empty_key) # Shifted colon (last key missing value!) args_shifted_colon = ["--", "title", ":", "a title"] @@ -75,11 +79,9 @@ def test_generate_default(self): raw_file = "" with open(path) as f: raw_file = f.read() - splitted_file = raw_file.splitlines() - header_limit = PageProcessor._get_header_limit(splitted_file) - parsed_header = PageProcessor._process_header("\n".join(splitted_file[1:header_limit]), - filename, - defaults.DEFAULT_FILE_NAMING_VARIABLE) + + parsed_header = PageProcessor._get_parsed_header(raw_file, filename, + defaults.DEFAULT_FILE_NAMING_VARIABLE) for key, value in defaults.DEFAULT_THEME_CFG.items(): if (value[1] != None): @@ -118,11 +120,9 @@ def test_generate_with_remainder(self): raw_file = "" with open(path) as f: raw_file = f.read() - splitted_file = raw_file.splitlines() - header_limit = PageProcessor._get_header_limit(splitted_file) - parsed_header = PageProcessor._process_header("\n".join(splitted_file[1:header_limit]), - filename, - defaults.DEFAULT_FILE_NAMING_VARIABLE) + + parsed_header = PageProcessor._get_parsed_header(raw_file, filename, + defaults.DEFAULT_FILE_NAMING_VARIABLE) for key, value in parsed_header_entries.items(): self.assertTrue(parsed_header[key] == value)