From 4672bfb4d351657791dd7bb4e216437509db587d Mon Sep 17 00:00:00 2001 From: mDuo13 Date: Fri, 16 Feb 2018 17:26:09 -0800 Subject: [PATCH] Dactyl v0.7.1: ElasticSearch index formula is configurable separate from PDF filename formula --- dactyl/dactyl_build.py | 17 +++-- dactyl/default-config.yml | 4 + dactyl/version.py | 2 +- tests/test-config.yml | 7 +- tests/testdactylbuild.py | 151 ++++++++++++++++++++------------------ 5 files changed, 98 insertions(+), 83 deletions(-) mode change 100644 => 100755 tests/testdactylbuild.py diff --git a/dactyl/dactyl_build.py b/dactyl/dactyl_build.py index 7632a04..def2e6f 100755 --- a/dactyl/dactyl_build.py +++ b/dactyl/dactyl_build.py @@ -41,23 +41,26 @@ ADHOC_TARGET = "__ADHOC__" ES_EVAL_KEY = "__dactyl_eval__" -def target_slug_name(target): - """Make a name for the target that's safe for use in URLs, filenames, and - similar places (ElasticSearch index names too) from human-readable fields""" +def target_slug_name(target, fields_to_use=[], separator="-"): + """Make a name for the target that's safe for use in URLs & filenames, + from human-readable fields""" target = get_target(target) filename_segments = [] - for fieldname in config["pdf_filename_fields"]: + for fieldname in fields_to_use: if fieldname in target.keys(): filename_segments.append(slugify(target[fieldname])) if filename_segments: - return config["pdf_filename_separator"].join(filename_segments) + return separator.join(filename_segments) else: return slugify(target["name"]) +def es_index_name(target): + return target_slug_name(target, config["es_index_fields"], config["es_index_separator"]) + def default_pdf_name(target): """Choose a reasonable name for a PDF file in case one isn't specified.""" - return target_slug_name(target)+".pdf" + return target_slug_name(target, config["pdf_filename_fields"], config["pdf_filename_separator"])+".pdf" # Generate a unique nonce per-run to be used for tempdir folder names @@ -706,7 +709,7 @@ def render_pages(target=None, mode="html", bypass_errors=False, if es_upload != NO_ES_UP: es_base_url = get_es_instance(es_upload) - es_index = target_slug_name(target) + es_index = es_index_name(target) # Note: this doesn't delete the old index if mode == "pdf" or mode == "html": diff --git a/dactyl/default-config.yml b/dactyl/default-config.yml index df8f1a4..9b0506f 100644 --- a/dactyl/default-config.yml +++ b/dactyl/default-config.yml @@ -34,6 +34,10 @@ pdf_filename_fields: - display_name pdf_filename_separator: '-' +es_index_fields: + - name +es_index_separator: '-' + prince_executable: prince ## If this is true, parses the files as Markdown without Jinja syntax diff --git a/dactyl/version.py b/dactyl/version.py index a71c5c7..f0788a8 100644 --- a/dactyl/version.py +++ b/dactyl/version.py @@ -1 +1 @@ -__version__ = '0.7.0' +__version__ = '0.7.1' diff --git a/tests/test-config.yml b/tests/test-config.yml index a788fba..35d5545 100644 --- a/tests/test-config.yml +++ b/tests/test-config.yml @@ -1,5 +1,5 @@ content_path: test_files -pdf_filename_fields: +pdf_filename_fields: - display_name pdf_filename_separator: '-' skip_preprocessor: false @@ -11,6 +11,9 @@ targets: - name: conditionals display_name: Conditional Text Target - name: markdown + - name: es_index_test_target + foo1: "Foo Value #1" + foo2: Foo Value TWOOOO pages: - name: filters_page @@ -31,4 +34,4 @@ pages: targets: - filters_target filters: - - mock_filter \ No newline at end of file + - mock_filter diff --git a/tests/testdactylbuild.py b/tests/testdactylbuild.py old mode 100644 new mode 100755 index cdb8c22..6dbf261 --- a/tests/testdactylbuild.py +++ b/tests/testdactylbuild.py @@ -1,73 +1,78 @@ -#!/usr/bin/env python3 - -import argparse -import sys -import unittest - -from dactyl import dactyl_build - -class MockCliArgs: - version=None - bypass_errors=False - config="test-config.yml" - debug=False - quiet=False - out_dir=None - skip_preprocessor=False - -from dactyl.config import DactylConfig - -class MockDactylConfig(DactylConfig): - def load_filters(self): - pass - -try: - dc = MockDactylConfig(MockCliArgs) - dactyl_build.config = dc -except ModuleNotFoundError: - print("Oh no! A module wasn't found, and this statement is extremely unhelpful!") - -from jinja2 import Template -mocktemplate = Template("This page is {{ currentpage.name }}.") - -class TestDactylBuild(unittest.TestCase): - #IMPORTANT: Please run these tests from the "tests" directory, to ensure test config files and mocks are correctly loaded. - - #P1 tests listed below - - def test_default_pdf_name(self): - test_result = dactyl_build.default_pdf_name("conditionals") - assert test_result == "Conditional_Text_Target.pdf" - - def test_get_target(self): - assert dactyl_build.get_target(None) == {"name": "test_target"} - assert dactyl_build.get_target("conditionals") == {"name": "conditionals", "display_name": "Conditional Text Target"} - assert dactyl_build.get_target({"name": "runtime_target"}) == {"name": "runtime_target"} - - def test_get_pages(self): - assert dactyl_build.get_pages(dactyl_build.get_target("test_target"), False) == [{'name': 'filters_page', 'category': 'Filters', 'filters': ['mock_filter'], 'targets': ['test_target'], 'html': 'filters_page.html'}, {'name': 'test_page', 'category': 'Tests', 'html': 'test.html', 'targets': ['test_target']}] - - def test_get_filters_for_page(self): - # Please note: due to the mock setup for unit testing, this function will always return an empty set. Refactoring is recommended to verify the remaining functionality for this method. - assert dactyl_build.get_filters_for_page(dactyl_build.config["pages"][0], dactyl_build.get_target("filters_target")) == set() - - def test_parse_markdown(self): - output = dactyl_build.parse_markdown(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], [], "html", "", False) - - def test_render_page(self): - output = dactyl_build.render_page(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], "html", "", [], mocktemplate, False) - assert output == "This page is md_test." - - #P2 tests listed below - - def test_target_slug_name(self): - assert dactyl_build.target_slug_name("conditionals") == "Conditional_Text_Target" - - def test_make_adhoc_target(self): - assert dactyl_build.make_adhoc_target(["gfm-compat.md"]) == {'name': '__ADHOC__', 'display_name': 'GitHub Markdown Compatibility'} - - def test_get_categories(self): - assert dactyl_build.get_categories(dactyl_build.config["pages"]) == ['Filters', 'Tests', 'Markdown'] - -if __name__ == '__main__': - unittest.main() \ No newline at end of file +#!/usr/bin/env python3 + +import argparse +import sys +import unittest + +from dactyl import dactyl_build + +class MockCliArgs: + version=None + bypass_errors=False + config="test-config.yml" + debug=False + quiet=False + out_dir=None + skip_preprocessor=False + +from dactyl.config import DactylConfig + +class MockDactylConfig(DactylConfig): + def load_filters(self): + pass + +try: + dc = MockDactylConfig(MockCliArgs) + dactyl_build.config = dc +except ModuleNotFoundError: + print("Oh no! A module wasn't found, and this statement is extremely unhelpful!") + +from jinja2 import Template +mocktemplate = Template("This page is {{ currentpage.name }}.") + +class TestDactylBuild(unittest.TestCase): + #IMPORTANT: Please run these tests from the "tests" directory, to ensure test config files and mocks are correctly loaded. + + def test_default_pdf_name(self): + test_result = dactyl_build.default_pdf_name("conditionals") + assert test_result == "Conditional_Text_Target.pdf" + + def test_get_target(self): + assert dactyl_build.get_target(None) == {"name": "test_target"} + assert dactyl_build.get_target("conditionals") == {"name": "conditionals", "display_name": "Conditional Text Target"} + assert dactyl_build.get_target({"name": "runtime_target"}) == {"name": "runtime_target"} + + def test_get_pages(self): + assert dactyl_build.get_pages(dactyl_build.get_target("test_target"), False) == [{'name': 'filters_page', 'category': 'Filters', 'filters': ['mock_filter'], 'targets': ['test_target'], 'html': 'filters_page.html'}, {'name': 'test_page', 'category': 'Tests', 'html': 'test.html', 'targets': ['test_target']}] + + def test_get_filters_for_page(self): + # Please note: due to the mock setup for unit testing, this function will always return an empty set. Refactoring is recommended to verify the remaining functionality for this method. + assert dactyl_build.get_filters_for_page(dactyl_build.config["pages"][0], dactyl_build.get_target("filters_target")) == set() + + def test_parse_markdown(self): + output = dactyl_build.parse_markdown(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], [], "html", "", False) + + def test_render_page(self): + output = dactyl_build.render_page(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], "html", "", [], mocktemplate, False) + assert output == "This page is md_test." + + def test_target_slug_name(self): + print("target_slug_name is", dactyl_build.target_slug_name("conditionals")) + fields_to_use = ["display_name"] + sep = "," + assert dactyl_build.target_slug_name("conditionals", fields_to_use, sep) == "Conditional_Text_Target" + + def test_es_index_name(self): + test_target = dactyl_build.get_target("es_index_test_target") + dactyl_build.config["es_index_fields"] = ["foo1", "foo2"] + dactyl_build.config["es_index_separator"] = "--" + assert dactyl_build.es_index_name(test_target) == "Foo_Value_1--Foo_Value_TWOOOO" + + def test_make_adhoc_target(self): + assert dactyl_build.make_adhoc_target(["gfm-compat.md"]) == {'name': '__ADHOC__', 'display_name': 'GitHub Markdown Compatibility'} + + def test_get_categories(self): + assert dactyl_build.get_categories(dactyl_build.config["pages"]) == ['Filters', 'Tests', 'Markdown'] + +if __name__ == '__main__': + unittest.main()