Skip to content

Commit

Permalink
improve tests, style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hlef committed Oct 1, 2018
1 parent 980d10d commit e97917b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
17 changes: 6 additions & 11 deletions juliet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 """

Expand Down Expand Up @@ -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):
Expand Down
29 changes: 20 additions & 9 deletions juliet/pageprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -222,17 +233,17 @@ def process(self, raw_file, filename):
would be returned as {"key": "value", "body": "<p>bodyContent</p>", "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

Expand Down
32 changes: 16 additions & 16 deletions juliet/tests/test_new_article_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e97917b

Please sign in to comment.