Skip to content

Commit 4672bfb

Browse files
committed
Dactyl v0.7.1: ElasticSearch index formula is configurable separate from PDF filename formula
1 parent c0bc5b5 commit 4672bfb

File tree

5 files changed

+98
-83
lines changed

5 files changed

+98
-83
lines changed

dactyl/dactyl_build.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,26 @@
4141
ADHOC_TARGET = "__ADHOC__"
4242
ES_EVAL_KEY = "__dactyl_eval__"
4343

44-
def target_slug_name(target):
45-
"""Make a name for the target that's safe for use in URLs, filenames, and
46-
similar places (ElasticSearch index names too) from human-readable fields"""
44+
def target_slug_name(target, fields_to_use=[], separator="-"):
45+
"""Make a name for the target that's safe for use in URLs & filenames,
46+
from human-readable fields"""
4747
target = get_target(target)
4848
filename_segments = []
49-
for fieldname in config["pdf_filename_fields"]:
49+
for fieldname in fields_to_use:
5050
if fieldname in target.keys():
5151
filename_segments.append(slugify(target[fieldname]))
5252

5353
if filename_segments:
54-
return config["pdf_filename_separator"].join(filename_segments)
54+
return separator.join(filename_segments)
5555
else:
5656
return slugify(target["name"])
5757

58+
def es_index_name(target):
59+
return target_slug_name(target, config["es_index_fields"], config["es_index_separator"])
60+
5861
def default_pdf_name(target):
5962
"""Choose a reasonable name for a PDF file in case one isn't specified."""
60-
return target_slug_name(target)+".pdf"
63+
return target_slug_name(target, config["pdf_filename_fields"], config["pdf_filename_separator"])+".pdf"
6164

6265

6366
# 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,
706709

707710
if es_upload != NO_ES_UP:
708711
es_base_url = get_es_instance(es_upload)
709-
es_index = target_slug_name(target)
712+
es_index = es_index_name(target)
710713
# Note: this doesn't delete the old index
711714

712715
if mode == "pdf" or mode == "html":

dactyl/default-config.yml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pdf_filename_fields:
3434
- display_name
3535
pdf_filename_separator: '-'
3636

37+
es_index_fields:
38+
- name
39+
es_index_separator: '-'
40+
3741
prince_executable: prince
3842

3943
## If this is true, parses the files as Markdown without Jinja syntax

dactyl/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.7.0'
1+
__version__ = '0.7.1'

tests/test-config.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
content_path: test_files
2-
pdf_filename_fields:
2+
pdf_filename_fields:
33
- display_name
44
pdf_filename_separator: '-'
55
skip_preprocessor: false
@@ -11,6 +11,9 @@ targets:
1111
- name: conditionals
1212
display_name: Conditional Text Target
1313
- name: markdown
14+
- name: es_index_test_target
15+
foo1: "Foo Value #1"
16+
foo2: Foo Value TWOOOO
1417

1518
pages:
1619
- name: filters_page
@@ -31,4 +34,4 @@ pages:
3134
targets:
3235
- filters_target
3336
filters:
34-
- mock_filter
37+
- mock_filter

tests/testdactylbuild.py

100644100755
+78-73
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,78 @@
1-
#!/usr/bin/env python3
2-
3-
import argparse
4-
import sys
5-
import unittest
6-
7-
from dactyl import dactyl_build
8-
9-
class MockCliArgs:
10-
version=None
11-
bypass_errors=False
12-
config="test-config.yml"
13-
debug=False
14-
quiet=False
15-
out_dir=None
16-
skip_preprocessor=False
17-
18-
from dactyl.config import DactylConfig
19-
20-
class MockDactylConfig(DactylConfig):
21-
def load_filters(self):
22-
pass
23-
24-
try:
25-
dc = MockDactylConfig(MockCliArgs)
26-
dactyl_build.config = dc
27-
except ModuleNotFoundError:
28-
print("Oh no! A module wasn't found, and this statement is extremely unhelpful!")
29-
30-
from jinja2 import Template
31-
mocktemplate = Template("This page is {{ currentpage.name }}.")
32-
33-
class TestDactylBuild(unittest.TestCase):
34-
#IMPORTANT: Please run these tests from the "tests" directory, to ensure test config files and mocks are correctly loaded.
35-
36-
#P1 tests listed below
37-
38-
def test_default_pdf_name(self):
39-
test_result = dactyl_build.default_pdf_name("conditionals")
40-
assert test_result == "Conditional_Text_Target.pdf"
41-
42-
def test_get_target(self):
43-
assert dactyl_build.get_target(None) == {"name": "test_target"}
44-
assert dactyl_build.get_target("conditionals") == {"name": "conditionals", "display_name": "Conditional Text Target"}
45-
assert dactyl_build.get_target({"name": "runtime_target"}) == {"name": "runtime_target"}
46-
47-
def test_get_pages(self):
48-
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']}]
49-
50-
def test_get_filters_for_page(self):
51-
# 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.
52-
assert dactyl_build.get_filters_for_page(dactyl_build.config["pages"][0], dactyl_build.get_target("filters_target")) == set()
53-
54-
def test_parse_markdown(self):
55-
output = dactyl_build.parse_markdown(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], [], "html", "", False)
56-
57-
def test_render_page(self):
58-
output = dactyl_build.render_page(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], "html", "", [], mocktemplate, False)
59-
assert output == "This page is md_test."
60-
61-
#P2 tests listed below
62-
63-
def test_target_slug_name(self):
64-
assert dactyl_build.target_slug_name("conditionals") == "Conditional_Text_Target"
65-
66-
def test_make_adhoc_target(self):
67-
assert dactyl_build.make_adhoc_target(["gfm-compat.md"]) == {'name': '__ADHOC__', 'display_name': 'GitHub Markdown Compatibility'}
68-
69-
def test_get_categories(self):
70-
assert dactyl_build.get_categories(dactyl_build.config["pages"]) == ['Filters', 'Tests', 'Markdown']
71-
72-
if __name__ == '__main__':
73-
unittest.main()
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import sys
5+
import unittest
6+
7+
from dactyl import dactyl_build
8+
9+
class MockCliArgs:
10+
version=None
11+
bypass_errors=False
12+
config="test-config.yml"
13+
debug=False
14+
quiet=False
15+
out_dir=None
16+
skip_preprocessor=False
17+
18+
from dactyl.config import DactylConfig
19+
20+
class MockDactylConfig(DactylConfig):
21+
def load_filters(self):
22+
pass
23+
24+
try:
25+
dc = MockDactylConfig(MockCliArgs)
26+
dactyl_build.config = dc
27+
except ModuleNotFoundError:
28+
print("Oh no! A module wasn't found, and this statement is extremely unhelpful!")
29+
30+
from jinja2 import Template
31+
mocktemplate = Template("This page is {{ currentpage.name }}.")
32+
33+
class TestDactylBuild(unittest.TestCase):
34+
#IMPORTANT: Please run these tests from the "tests" directory, to ensure test config files and mocks are correctly loaded.
35+
36+
def test_default_pdf_name(self):
37+
test_result = dactyl_build.default_pdf_name("conditionals")
38+
assert test_result == "Conditional_Text_Target.pdf"
39+
40+
def test_get_target(self):
41+
assert dactyl_build.get_target(None) == {"name": "test_target"}
42+
assert dactyl_build.get_target("conditionals") == {"name": "conditionals", "display_name": "Conditional Text Target"}
43+
assert dactyl_build.get_target({"name": "runtime_target"}) == {"name": "runtime_target"}
44+
45+
def test_get_pages(self):
46+
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']}]
47+
48+
def test_get_filters_for_page(self):
49+
# 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.
50+
assert dactyl_build.get_filters_for_page(dactyl_build.config["pages"][0], dactyl_build.get_target("filters_target")) == set()
51+
52+
def test_parse_markdown(self):
53+
output = dactyl_build.parse_markdown(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], [], "html", "", False)
54+
55+
def test_render_page(self):
56+
output = dactyl_build.render_page(dactyl_build.config["pages"][2], "filters_target", dactyl_build.config["pages"], "html", "", [], mocktemplate, False)
57+
assert output == "This page is md_test."
58+
59+
def test_target_slug_name(self):
60+
print("target_slug_name is", dactyl_build.target_slug_name("conditionals"))
61+
fields_to_use = ["display_name"]
62+
sep = ","
63+
assert dactyl_build.target_slug_name("conditionals", fields_to_use, sep) == "Conditional_Text_Target"
64+
65+
def test_es_index_name(self):
66+
test_target = dactyl_build.get_target("es_index_test_target")
67+
dactyl_build.config["es_index_fields"] = ["foo1", "foo2"]
68+
dactyl_build.config["es_index_separator"] = "--"
69+
assert dactyl_build.es_index_name(test_target) == "Foo_Value_1--Foo_Value_TWOOOO"
70+
71+
def test_make_adhoc_target(self):
72+
assert dactyl_build.make_adhoc_target(["gfm-compat.md"]) == {'name': '__ADHOC__', 'display_name': 'GitHub Markdown Compatibility'}
73+
74+
def test_get_categories(self):
75+
assert dactyl_build.get_categories(dactyl_build.config["pages"]) == ['Filters', 'Tests', 'Markdown']
76+
77+
if __name__ == '__main__':
78+
unittest.main()

0 commit comments

Comments
 (0)