Skip to content

Commit 086fbb1

Browse files
committed
template the output files
1 parent e6238f9 commit 086fbb1

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ ENV/
101101
.mypy_cache/
102102

103103
# pytest
104-
*.pytest
104+
*.pytest*
105105

106106
# editors
107107
*.idea

src/snippet/config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class Config:
99
# IO
1010
project_root = '.' # the project root used for relative IO paths (set by commandline)
1111
input_glob = 'tests/example/*.py'
12-
output_template = '```python\n# example: {{{name}}}\n{{{code}}}\n```\n' # a mustache template for each file
13-
output_append = False # if the output file exists, append to it
12+
output_append = True # if the output file exists, append to it
1413
output_dir = '.'
15-
output_file_ext = 'md'
14+
output_file_name_template = '{{name}}.md' # a mustache template for the output file name
15+
16+
# Language and style
17+
output_template = '```python\n# example: {{{name}}}\n{{{code}}}\n```\n' # a mustache template for each file
1618

1719
# Logging
1820
log_level = logging.INFO

src/snippet/file_wrangler.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
from snippet.config import Config
88

99

10-
def write_example(config: Config, path, example_name, example_block):
10+
def write_example(config: Config, example_name, example_block):
1111
"""Writes example to file"""
1212
output = pystache.render(
1313
config.output_template,
1414
name=example_name,
1515
code=example_block
1616
)
17+
18+
output_file_name = pystache.render(
19+
config.output_file_name_template,
20+
name=example_name.strip().replace(' ', '')
21+
)
22+
1723
if not os.path.exists(config.output_dir):
24+
logging.info('creating output directory %s', config.output_dir)
1825
os.makedirs(config.output_dir)
19-
# TODO: this, properly...
20-
clean_name = example_name.strip().replace(' ', '')
21-
output_file = os.path.join(config.output_dir, clean_name) + os.extsep + config.output_file_ext
26+
output_file = os.path.join(config.output_dir, output_file_name)
2227
logging.info('writing %r to %s', example_name, output_file)
2328
with open(output_file, 'a' if config.output_append else 'w') as fh:
2429
fh.write(output)

src/snippet/workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def run(config: Config):
4343
logging.debug('example code: %s', example_block)
4444

4545
wrap(config, failures, path, partial(
46-
file_wrangler.write_example, config, path, example_name, example_block
46+
file_wrangler.write_example, config, example_name, example_block
4747
))
4848

4949
return examples, paths, failures

tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import os
22
tmp_test_dirname = 'tmp_test_dir'
3-
tmp_test_dir = os.path.join(os.path.dirname(__file__), tmp_test_dirname)
4-
sample_input_dir = os.path.join(os.path.dirname(__file__), 'samples')
3+
tmp_test_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), tmp_test_dirname))
4+
sample_input_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'samples'))

tests/test_direct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ def test_run(self):
2121
config.output_append = True
2222

2323
# only detect the python file
24-
config.input_glob = 'tests/samples/example.py'
24+
config.input_glob = os.path.join(sample_input_dir, 'example.py')
2525
snippet.main(config=config)
2626

2727
# only detect the java file
2828
config.output_template = '```java\n# example: {{{name}}}\n{{{code}}}\n```'
29-
config.input_glob = 'tests/samples/example.java'
29+
config.input_glob = os.path.join(sample_input_dir, 'example.java')
3030
snippet.main(config=config)
3131

3232
self.assertTrue(filecmp.cmp(

0 commit comments

Comments
 (0)