Skip to content

Commit

Permalink
Dactyl v0.7.1: ElasticSearch index formula is configurable separate f…
Browse files Browse the repository at this point in the history
…rom PDF filename formula
  • Loading branch information
mDuo13 committed Feb 17, 2018
1 parent c0bc5b5 commit 4672bfb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 83 deletions.
17 changes: 10 additions & 7 deletions dactyl/dactyl_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down
4 changes: 4 additions & 0 deletions dactyl/default-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dactyl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.0'
__version__ = '0.7.1'
7 changes: 5 additions & 2 deletions tests/test-config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
content_path: test_files
pdf_filename_fields:
pdf_filename_fields:
- display_name
pdf_filename_separator: '-'
skip_preprocessor: false
Expand All @@ -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
Expand All @@ -31,4 +34,4 @@ pages:
targets:
- filters_target
filters:
- mock_filter
- mock_filter
151 changes: 78 additions & 73 deletions tests/testdactylbuild.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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()
#!/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()

0 comments on commit 4672bfb

Please sign in to comment.