From cf4970a57fe25fc29bbdfef62ef18e7b7ce37efd Mon Sep 17 00:00:00 2001 From: Jonathan Marini Date: Thu, 13 Mar 2014 19:39:14 -0700 Subject: [PATCH 1/3] Added YAML as an option for context files. Using PyYAML as the parser for both JSON and YAML files. --- complexity/generate.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/complexity/generate.py b/complexity/generate.py index 08c00ce..bbffec6 100755 --- a/complexity/generate.py +++ b/complexity/generate.py @@ -8,10 +8,10 @@ Functions for static site generation. """ -import json import logging import os import shutil +import yaml from binaryornot.check import is_binary from jinja2 import FileSystemLoader @@ -139,14 +139,14 @@ def generate_context(context_dir): """ Generates the context for all Complexity pages. - :param context_dir: Directory containing `.json` file(s) to be turned into - context variables for Jinja2. + :param context_dir: Directory containing `.json` or '.yml'/'.yaml' file(s) + to be turned into context variables for Jinja2. Description: - Iterates through the contents of `context_dir` and finds all JSON - files. Loads the JSON file as a Python object with the key being the - JSON file name. + Iterates through the contents of `context_dir` and finds all JSON/YAML + files. Loads the JSON/YAML file as a Python object with the key being + the file name. Example: @@ -167,19 +167,19 @@ def generate_context(context_dir): """ context = {} - json_files = os.listdir(context_dir) + context_files = os.listdir(context_dir) - for file_name in json_files: + for file_name in context_files: - if file_name.endswith('json'): + if os.path.splitext(file_name)[1][1:] in ['json', 'yml', 'yaml']: - # Open the JSON file and convert to Python object - json_file = os.path.join(context_dir, file_name) - with unicode_open(json_file) as f: - obj = json.load(f) + # Open the JSON/YAML file and convert to Python object + context_file = os.path.join(context_dir, file_name) + with unicode_open(context_file) as f: + obj = yaml.load(f) # Add the Python object to the context dictionary - context[file_name[:-5]] = obj + context[os.path.splitext(file_name)[0]] = obj return context From 4135775c7e9faebe9af3a4383b2e97aba450da29 Mon Sep 17 00:00:00 2001 From: Jonathan Marini Date: Mon, 21 Apr 2014 11:35:11 -0400 Subject: [PATCH 2/3] Changed json files back to being parsed with builtin json module. --- complexity/generate.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/complexity/generate.py b/complexity/generate.py index bbffec6..21737be 100755 --- a/complexity/generate.py +++ b/complexity/generate.py @@ -8,6 +8,7 @@ Functions for static site generation. """ +import json import logging import os import shutil @@ -171,9 +172,9 @@ def generate_context(context_dir): for file_name in context_files: - if os.path.splitext(file_name)[1][1:] in ['json', 'yml', 'yaml']: + if os.path.splitext(file_name)[1][1:] in ['yml', 'yaml']: - # Open the JSON/YAML file and convert to Python object + # Open the YAML file and convert to Python object context_file = os.path.join(context_dir, file_name) with unicode_open(context_file) as f: obj = yaml.load(f) @@ -181,6 +182,16 @@ def generate_context(context_dir): # Add the Python object to the context dictionary context[os.path.splitext(file_name)[0]] = obj + elif os.path.splitext(file_name)[1][1:] in ['json']: + + # Open the JSON file and convert to Python object + context_file = os.path.join(context_dir, file_name) + with unicode_open(context_file) as f: + obj = json.load(f) + + # Add the Python object to the context dictionary + context[os.path.splitext(file_name)[0]] = obj + return context From eeeb2e9615d0c8086bfb20a5d41cc681a03ec7c7 Mon Sep 17 00:00:00 2001 From: Jonathan Marini Date: Mon, 21 Apr 2014 11:41:26 -0400 Subject: [PATCH 3/3] Added tests for yaml context files. Testing for yaml file with proper information as well as empty yaml file. --- tests/project/context-empty/test.yaml | 0 tests/project/context-yaml/test.yaml | 6 ++++++ tests/test_generate.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/project/context-empty/test.yaml create mode 100644 tests/project/context-yaml/test.yaml diff --git a/tests/project/context-empty/test.yaml b/tests/project/context-empty/test.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/project/context-yaml/test.yaml b/tests/project/context-yaml/test.yaml new file mode 100644 index 0000000..678d538 --- /dev/null +++ b/tests/project/context-yaml/test.yaml @@ -0,0 +1,6 @@ +{ + # comment + "1": 2, + a: "b", + multiple: ["c", "d", "e",], +} diff --git a/tests/test_generate.py b/tests/test_generate.py index 14973a2..b890434 100755 --- a/tests/test_generate.py +++ b/tests/test_generate.py @@ -213,12 +213,25 @@ def tearDown(self): class TestGenerateContext(unittest.TestCase): - def test_generate_context(self): + def test_generate_context_json(self): context = generate.generate_context( context_dir='tests/project/context/' ) self.assertEqual(context, {"test": {"1": 2}}) + def test_generate_context_yaml(self): + context = generate.generate_context( + context_dir='tests/project/context-yaml/' + ) + self.assertEqual( + context, {"test": {"1": 2, "a": "b", "multiple": ["c", "d", "e"]}}) + + def test_generate_empty_context_yaml(self): + context = generate.generate_context( + context_dir='tests/project/context-empty/' + ) + self.assertEqual(context, {"test": None}) + class TestCopyAssets(unittest.TestCase): def test_copy_assets(self):