Skip to content

Commit

Permalink
Merge pull request sphinx-contrib#65 from dhellmann/clean-up-console-…
Browse files Browse the repository at this point in the history
…script

clean up console script
  • Loading branch information
janbrohl authored Sep 15, 2020
2 parents cabfbbb + 73d323d commit 78e3c51
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 35 deletions.
2 changes: 1 addition & 1 deletion doc/source/_templates/sample-multiple.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ List of Items

{% for item in data %}
- {{item|tojson}}

- {{item.key}}
- {{item.key1}}
{% endfor %}
Expand Down
39 changes: 31 additions & 8 deletions doc/source/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Help
====

.. runcmd:: python -m sphinxcontrib.datatemplates.cli --help
.. runcmd:: datatemplate --help

Data File
=========
Expand All @@ -19,14 +19,37 @@ Template File
.. include:: _templates/sample-multiple.tmpl
:literal:

Command Line
============
Rendering a Template
====================

.. code-block:: bat
.. code-block:: console
python -m sphinxcontrib.datatemplates.cli -o multiple-documents:1 doc/source/_templates/sample-multiple.tmpl doc/source/sample-multiple.yaml
$ datatemplate render -o multiple-documents \
doc/source/_templates/sample-multiple.tmpl \
doc/source/sample-multiple.yaml
Output
======
.. runcmd:: datatemplate render -o multiple-documents doc/source/_templates/sample-multiple.tmpl doc/source/sample-multiple.yaml

.. runcmd:: python -m sphinxcontrib.datatemplates.cli -o multiple-documents:1 doc/source/_templates/sample-multiple.tmpl doc/source/sample-multiple.yaml
Experimenting by Dumping Data
=============================

CSV Data With Headers
---------------------

.. code-block:: console
$ datatemplate dump -o dialect:excel-tab \
-o headers \
doc/source/sample.csv
.. runcmd:: datatemplate dump -o dialect:excel-tab -o headers doc/source/sample.csv

CSV Data Without Headers
------------------------

.. code-block:: console
$ datatemplate dump -o dialect:excel-tab \
doc/source/sample.csv
.. runcmd:: datatemplate dump -o dialect:excel-tab doc/source/sample.csv
3 changes: 1 addition & 2 deletions doc/source/sample-multiple.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
key1: value1

---
key: value
key1: different value


2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace_packages =

[entry_points]
console_scripts =
test-template = sphinxcontrib.datatemplates.cli:main
datatemplate = sphinxcontrib.datatemplates.cli:main

[wheel]
universal = 1
Expand Down
105 changes: 85 additions & 20 deletions sphinxcontrib/datatemplates/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import io
import os.path
import pprint

import jinja2

Expand All @@ -16,59 +17,123 @@ def main():
'--config-file',
help='the path to conf.py',
)
parser.add_argument(
subparsers = parser.add_subparsers(
title='commands',
description='valid commands',
dest='command',
)

do_render = subparsers.add_parser(
'render',
help='render a template to stdout',
)
do_render.add_argument(
'--option',
'-o',
action='append',
default=[],
help='options given as key:value passed through to loader and template'
)
parser.add_argument(
do_render.add_argument(
'template',
help='the path to the template file',
)
parser.add_argument(
do_render.add_argument(
'source',
help='the path to the data file',
)
do_render.set_defaults(func=render)

do_dump = subparsers.add_parser(
'dump',
help='dump the data to stdout without a template',
)
do_dump.add_argument(
'--option',
'-o',
action='append',
default=[],
help='options given as key:value passed through to loader and template'
)
do_dump.add_argument(
'source',
help='the path to the data file',
)
do_dump.set_defaults(func=dump)

args = parser.parse_args()
# no arguments, print help messaging, then exit with error(1)
if not args.command:
parser.print_help()
return 1

config_globals = {}
conf = {}
if args.config_file:
with io.open(args.config_file, 'r', encoding='utf-8-sig') as f:
config_body = f.read()
exec(config_body, config_globals)
# add options
config_globals.update({
k.replace("-", "_"): v
for k, _, v in (s.partition(':') for s in args.option)
})
# add special options
config_globals.update({
"source":
args.source,
"template":
args.template,
"absolute_resolved_path":
os.path.abspath(args.source)
exec(config_body, conf)

return args.func(args, conf)


def _parse_options(options):
# Process the sequence of options. If there is a colon, use it to
# separate the option name from its value. If there is no colon,
# treat the option as a flag.
results = {}
for opt in options:
if ':' in opt:
k, _, v = opt.partition(':')
else:
k = opt
v = True
results[k.replace('-', '_')] = v
return results


def render(args, conf):
conf.update(_parse_options(args.option))
conf.update({
"source": args.source,
"template": args.template,
"absolute_resolved_path": os.path.abspath(args.source)
})

load = loaders.loader_for_source(args.source)
if load is None:
print('Could not find loader for {}'.format(args.source))
return 1

with io.open(args.template, 'r', encoding='utf-8-sig') as f:
template_body = f.read()

template = jinja2.Template(template_body)
with load(**config_globals) as data:
with load(**conf) as data:
rendered = template.render(
make_list_table=helpers.make_list_table,
make_list_table_from_mappings=helpers.
make_list_table_from_mappings,
data=data,
**config_globals,
**conf
)
print(rendered)


def dump(args, conf):
conf.update(_parse_options(args.option))
conf.update({
"source": args.source,
"absolute_resolved_path": os.path.abspath(args.source)
})

load = loaders.loader_for_source(args.source)
if load is None:
print('Could not find loader for {}'.format(args.source))
return 1

with load(**conf) as data:
pprint.pprint(data)


if __name__ == '__main__':
main()
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
envlist=py36,py37,py38,linter,docs,pkglint

[testenv]
# Use a local copy of my "fix-py3-install" branch from the fork of
# pyenchant that lets us install it from source.
# https://github.com/dhellmann/pyenchant/tree/fix-py3-install
deps=
.[test]
commands=
Expand Down

0 comments on commit 78e3c51

Please sign in to comment.