docma 2.2.0__tar.gz → 3.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. {docma-2.2.0 → docma-3.0.0}/MANIFEST.in +0 -1
  2. {docma-2.2.0/docma.egg-info → docma-3.0.0}/PKG-INFO +4 -3
  3. {docma-2.2.0 → docma-3.0.0}/README.md +12 -0
  4. docma-3.0.0/docma/__init__.py +83 -0
  5. {docma-2.2.0 → docma-3.0.0}/docma/cli/docma.py +25 -3
  6. {docma-2.2.0 → docma-3.0.0}/docma/commands/__common__.py +7 -8
  7. {docma-2.2.0 → docma-3.0.0}/docma/commands/compile.py +4 -1
  8. docma-3.0.0/docma/commands/completion.py +42 -0
  9. {docma-2.2.0 → docma-3.0.0}/docma/commands/html_batch.py +19 -9
  10. {docma-2.2.0 → docma-3.0.0}/docma/commands/html_render.py +4 -1
  11. {docma-2.2.0 → docma-3.0.0}/docma/commands/info.py +5 -2
  12. {docma-2.2.0 → docma-3.0.0}/docma/commands/new.py +43 -17
  13. {docma-2.2.0 → docma-3.0.0}/docma/commands/pdf_batch.py +16 -7
  14. {docma-2.2.0 → docma-3.0.0}/docma/commands/pdf_render.py +4 -1
  15. docma-3.0.0/docma/commands/version.py +40 -0
  16. {docma-2.2.0 → docma-3.0.0}/docma/compilers/__common__.py +2 -2
  17. {docma-2.2.0 → docma-3.0.0}/docma/data_providers/__common__.py +13 -10
  18. {docma-2.2.0 → docma-3.0.0}/docma/data_providers/db.py +19 -7
  19. {docma-2.2.0 → docma-3.0.0}/docma/data_providers/file.py +1 -1
  20. {docma-2.2.0 → docma-3.0.0}/docma/data_providers/params.py +1 -1
  21. {docma-2.2.0 → docma-3.0.0}/docma/docma_core.py +87 -91
  22. {docma-2.2.0 → docma-3.0.0}/docma/exceptions.py +6 -0
  23. {docma-2.2.0 → docma-3.0.0}/docma/generators/__common__.py +2 -1
  24. {docma-2.2.0 → docma-3.0.0}/docma/generators/qrcode.py +4 -4
  25. {docma-2.2.0 → docma-3.0.0}/docma/generators/swatch.py +2 -2
  26. {docma-2.2.0 → docma-3.0.0}/docma/generators/vega.py +4 -3
  27. {docma-2.2.0 → docma-3.0.0}/docma/importers/__common__.py +2 -2
  28. {docma-2.2.0 → docma-3.0.0}/docma/importers/http.py +2 -0
  29. {docma-2.2.0 → docma-3.0.0}/docma/jinja/core.py +3 -1
  30. {docma-2.2.0 → docma-3.0.0}/docma/jinja/extensions.py +4 -2
  31. {docma-2.2.0 → docma-3.0.0}/docma/jinja/resolvers.py +4 -6
  32. {docma-2.2.0 → docma-3.0.0}/docma/jinja/utils.py +0 -1
  33. docma-3.0.0/docma/lib/db.py +23 -0
  34. {docma-2.2.0 → docma-3.0.0}/docma/lib/html.py +17 -14
  35. {docma-2.2.0 → docma-3.0.0}/docma/lib/jsonschema.py +5 -4
  36. {docma-2.2.0 → docma-3.0.0}/docma/lib/metadata.py +2 -1
  37. {docma-2.2.0 → docma-3.0.0}/docma/lib/misc.py +23 -64
  38. {docma-2.2.0 → docma-3.0.0}/docma/lib/packager.py +6 -4
  39. {docma-2.2.0 → docma-3.0.0}/docma/lib/path.py +1 -1
  40. docma-3.0.0/docma/lib/pdf.py +64 -0
  41. {docma-2.2.0 → docma-3.0.0}/docma/lib/plugin.py +15 -11
  42. {docma-2.2.0 → docma-3.0.0}/docma/lib/query.py +41 -20
  43. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/au/company_ids.py +3 -4
  44. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/deprecated.py +3 -3
  45. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/utility.py +1 -1
  46. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/datetime.py +1 -1
  47. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/number.py +1 -1
  48. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/phone.py +8 -4
  49. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/utility.py +1 -1
  50. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/__common__.py +2 -2
  51. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/docma.py +2 -4
  52. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/http.py +1 -1
  53. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/s3.py +1 -1
  54. {docma-2.2.0 → docma-3.0.0}/docma/validators.py +6 -1
  55. docma-3.0.0/docma/version.py +19 -0
  56. {docma-2.2.0 → docma-3.0.0/docma.egg-info}/PKG-INFO +4 -3
  57. {docma-2.2.0 → docma-3.0.0}/docma.egg-info/SOURCES.txt +3 -1
  58. {docma-2.2.0 → docma-3.0.0}/docma.egg-info/requires.txt +3 -2
  59. {docma-2.2.0 → docma-3.0.0}/pyproject.toml +17 -4
  60. {docma-2.2.0 → docma-3.0.0}/requirements-build.txt +5 -1
  61. {docma-2.2.0 → docma-3.0.0}/requirements.txt +4 -2
  62. {docma-2.2.0 → docma-3.0.0}/setup.py +14 -3
  63. docma-2.2.0/docma/VERSION +0 -1
  64. docma-2.2.0/docma/__init__.py +0 -45
  65. docma-2.2.0/docma/lib/db.py +0 -21
  66. docma-2.2.0/docma/version.py +0 -7
  67. {docma-2.2.0 → docma-3.0.0}/CONTRIBUTING.md +0 -0
  68. {docma-2.2.0 → docma-3.0.0}/LICENCE.txt +0 -0
  69. {docma-2.2.0 → docma-3.0.0}/PYPI.md +0 -0
  70. {docma-2.2.0 → docma-3.0.0}/docma/cli/__init__.py +0 -0
  71. {docma-2.2.0 → docma-3.0.0}/docma/commands/__init__.py +0 -0
  72. {docma-2.2.0 → docma-3.0.0}/docma/compilers/__init__.py +0 -0
  73. {docma-2.2.0 → docma-3.0.0}/docma/compilers/markdown.py +0 -0
  74. {docma-2.2.0 → docma-3.0.0}/docma/config.py +0 -0
  75. {docma-2.2.0 → docma-3.0.0}/docma/data_providers/__init__.py +0 -0
  76. {docma-2.2.0 → docma-3.0.0}/docma/generators/__init__.py +0 -0
  77. {docma-2.2.0 → docma-3.0.0}/docma/importers/__init__.py +0 -0
  78. {docma-2.2.0 → docma-3.0.0}/docma/importers/s3.py +0 -0
  79. {docma-2.2.0 → docma-3.0.0}/docma/jinja/__init__.py +0 -0
  80. {docma-2.2.0 → docma-3.0.0}/docma/lib/__init__.py +0 -0
  81. {docma-2.2.0 → docma-3.0.0}/docma/lib/http.py +0 -0
  82. {docma-2.2.0 → docma-3.0.0}/docma/lib/logging.py +0 -0
  83. {docma-2.2.0 → docma-3.0.0}/docma/plugins/__init__.py +0 -0
  84. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/__init__.py +0 -0
  85. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/au/__init__.py +0 -0
  86. {docma-2.2.0 → docma-3.0.0}/docma/plugins/format_checkers/au/industry_formats.py +0 -0
  87. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/__init__.py +0 -0
  88. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/au/__init__.py +0 -0
  89. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/au/company_ids.py +0 -0
  90. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/deprecated.py +0 -0
  91. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_filters/dollars.py +0 -0
  92. {docma-2.2.0 → docma-3.0.0}/docma/plugins/jinja_tests/__init__.py +0 -0
  93. {docma-2.2.0 → docma-3.0.0}/docma/resources/__init__.py +0 -0
  94. {docma-2.2.0 → docma-3.0.0}/docma/resources/config-schema.yaml +0 -0
  95. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/cookiecutter.json +0 -0
  96. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/hooks/pre_gen_project.py +0 -0
  97. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/charts/README +0 -0
  98. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/charts/woof.yaml +0 -0
  99. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/config.yaml +0 -0
  100. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/content/README +0 -0
  101. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/content/sample.html +0 -0
  102. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/css/pdf.css +0 -0
  103. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/data/README +0 -0
  104. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/data/dogs.csv +0 -0
  105. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/fonts/README +0 -0
  106. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/overlays/grid-a4-landscape.html +0 -0
  107. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/overlays/grid-a4-portrait.html +0 -0
  108. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/queries/README +0 -0
  109. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/resources/README +0 -0
  110. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/resources/img/docma-icon-light.svg +0 -0
  111. {docma-2.2.0 → docma-3.0.0}/docma/resources/cookiecutter/{{cookiecutter.template_src_dir}}/resources/img/docma-logo-horizontal-light.svg +0 -0
  112. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/__init__.py +0 -0
  113. {docma-2.2.0 → docma-3.0.0}/docma/url_fetchers/file.py +0 -0
  114. {docma-2.2.0 → docma-3.0.0}/docma.egg-info/dependency_links.txt +0 -0
  115. {docma-2.2.0 → docma-3.0.0}/docma.egg-info/entry_points.txt +0 -0
  116. {docma-2.2.0 → docma-3.0.0}/docma.egg-info/top_level.txt +0 -0
  117. {docma-2.2.0 → docma-3.0.0}/requirements-duckdb.txt +0 -0
  118. {docma-2.2.0 → docma-3.0.0}/requirements-lava.txt +0 -0
  119. {docma-2.2.0 → docma-3.0.0}/setup.cfg +0 -0
@@ -1,7 +1,6 @@
1
1
  include *.md
2
2
  include LICENCE.txt
3
3
  include requirements*.txt
4
- include */VERSION
5
4
  recursive-include docma/resources *
6
5
  global-exclude .DS_Store .env
7
6
  global-exclude */__pycache__/*
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docma
3
- Version: 2.2.0
3
+ Version: 3.0.0
4
4
  Summary: Document manufacturer
5
5
  Home-page: https://github.com/jin-gizmo/docma
6
6
  Author: Murray Andrews
@@ -21,8 +21,9 @@ Requires-Python: >=3.11.0
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENCE.txt
23
23
  Requires-Dist: altair
24
+ Requires-Dist: argcomplete
24
25
  Requires-Dist: babel
25
- Requires-Dist: beautifulsoup4
26
+ Requires-Dist: beautifulsoup4>=4.13
26
27
  Requires-Dist: boto3
27
28
  Requires-Dist: cachetools
28
29
  Requires-Dist: colorama
@@ -43,7 +44,7 @@ Requires-Dist: referencing
43
44
  Requires-Dist: requests
44
45
  Requires-Dist: tqdm
45
46
  Requires-Dist: vl-convert-python
46
- Requires-Dist: weasyprint
47
+ Requires-Dist: weasyprint>=68.0
47
48
  Provides-Extra: all
48
49
  Requires-Dist: duckdb; extra == "all"
49
50
  Requires-Dist: jinlava>=8.0.0; extra == "all"
@@ -12,6 +12,18 @@ documents from document templates with dynamic, data driven content.
12
12
  [![GitHub Licence](https://img.shields.io/github/license/jin-gizmo/docma)](https://github.com/jin-gizmo/docma/blob/master/LICENCE.txt)
13
13
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
14
14
 
15
+ ---
16
+ <!-- toc-start min=2 max=2 style=linear -->
17
+ <!-- Generated by TOCmark v1.0.0 (https://github.com/jin-gizmo/tocmark) -->
18
+
19
+ [Genesis](#genesis) &bull;
20
+ [Features](#features) &bull;
21
+ [Installation&nbsp;and&nbsp;Usage](#installation-and-usage) &bull;
22
+ [Credits](#credits)
23
+
24
+ <!-- toc-end -->
25
+ ---
26
+
15
27
  ## Genesis
16
28
 
17
29
  **Docma** was developed at [Origin Energy](https://www.originenergy.com.au)
@@ -0,0 +1,83 @@
1
+ """
2
+ Primary docma API for compiling and rendering document templates.
3
+
4
+ Typical usage would be:
5
+
6
+ ```python
7
+ from docma import compile_template, render_template
8
+
9
+ template_src_dir = 'a/b/c'
10
+ template_location = 'my-template.zip' # ... or a directory when experimenting
11
+ pdf_location = 'my-doc.pdf'
12
+ params = { ... } # A Dict of parameters.
13
+
14
+ compile_template(template_src_dir, template_location)
15
+
16
+ pdf = render_template_to_pdf(template_location, params)
17
+
18
+ # We now have a pypdf PdfWriter object. Do with it what you will. e.g.
19
+ pdf.write(pdf_location)
20
+ ```
21
+
22
+ """
23
+
24
+ # mkdocstrings needs this in order to discover the API.
25
+ from typing import TYPE_CHECKING
26
+
27
+ from .version import __version__, __version_info__
28
+
29
+ if TYPE_CHECKING: # pragma: no cover
30
+ from .docma_core import (
31
+ compile_template,
32
+ get_template_info,
33
+ read_template_version_info,
34
+ render_template_to_html,
35
+ render_template_to_pdf,
36
+ safe_render_path,
37
+ )
38
+
39
+ # ------------------------------------------------------------------------------
40
+ # These imports are pretty heavy duty. We don't want to pre-emptively import
41
+ # everything. Among other things, doing so makes CLI command completion run like
42
+ # a camel in a bog.
43
+ _LAZY_IMPORTS = {
44
+ 'compile_template': ('docma_core', 'compile_template'),
45
+ 'get_template_info': ('docma_core', 'get_template_info'),
46
+ 'read_template_version_info': ('docma_core', 'read_template_version_info'),
47
+ 'render_template_to_html': ('docma_core', 'render_template_to_html'),
48
+ 'render_template_to_pdf': ('docma_core', 'render_template_to_pdf'),
49
+ 'safe_render_path': ('docma_core', 'safe_render_path'),
50
+ }
51
+
52
+
53
+ def __getattr__(name):
54
+ """Lazy import heavy modules only when accessed."""
55
+ try:
56
+ module_name, attr_name = _LAZY_IMPORTS[name]
57
+ except KeyError:
58
+ raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
59
+
60
+ from importlib import import_module
61
+
62
+ module = import_module(f'.{module_name}', package='docma')
63
+ value = getattr(module, attr_name)
64
+ globals()[name] = value # Cache it
65
+ return value
66
+
67
+
68
+ __all__ = [
69
+ '__version__',
70
+ '__version_info__',
71
+ 'compile_template',
72
+ 'get_template_info',
73
+ 'read_template_version_info',
74
+ 'render_template_to_html',
75
+ 'render_template_to_pdf',
76
+ 'safe_render_path',
77
+ ]
78
+
79
+
80
+ # ------------------------------------------------------------------------------
81
+ def __dir__():
82
+ """Ensure dir() shows all public attributes."""
83
+ return __all__
@@ -9,11 +9,13 @@ import sys
9
9
  from logging import getLogger
10
10
  from pathlib import Path
11
11
 
12
+ import argcomplete
13
+
14
+ from docma import __version__
12
15
  from docma.commands import CliCommand
13
16
  from docma.config import LOGNAME
14
17
  from docma.exceptions import DocmaInternalError
15
18
  from docma.lib.logging import setup_logging
16
- from docma.version import __version__
17
19
 
18
20
  __author__ = 'Murray Andrews'
19
21
 
@@ -23,6 +25,25 @@ LOGLEVEL = 'warning'
23
25
 
24
26
 
25
27
  # ------------------------------------------------------------------------------
28
+ class VersionDeprecatedAction(argparse.Action):
29
+ """The -v/--version option is deprecated - use the version subcommand instead."""
30
+
31
+ def __init__(self, option_strings, dest, **kwargs):
32
+ """Create a new VersionDeprecatedAction object."""
33
+ super().__init__(option_strings, dest, nargs=0, **kwargs)
34
+
35
+ def __call__(self, parser, namespace, values, option_string=None):
36
+ """Print the current version and exit."""
37
+ print(
38
+ f'{PROG}: WARNING: The -v/--version option is deprecated and will'
39
+ ' be removed in the next release. Use the "version" subcommand.',
40
+ file=sys.stderr,
41
+ )
42
+ print(__version__)
43
+ parser.exit(0)
44
+
45
+
46
+ # ---------------------------------------------------k---------------------------
26
47
  def process_cli_args() -> argparse.Namespace:
27
48
  """
28
49
  Process the command line arguments.
@@ -32,7 +53,7 @@ def process_cli_args() -> argparse.Namespace:
32
53
 
33
54
  argp = argparse.ArgumentParser(prog=PROG, description='Manufacture documents.')
34
55
 
35
- argp.add_argument('-v', '--version', action='version', version=__version__)
56
+ argp.add_argument('-v', '--version', action=VersionDeprecatedAction, help=argparse.SUPPRESS)
36
57
 
37
58
  # ----------------------------------------
38
59
  # Logging args
@@ -61,10 +82,11 @@ def process_cli_args() -> argparse.Namespace:
61
82
 
62
83
  # ----------------------------------------
63
84
  # Add the sub-commonads
64
- subp = argp.add_subparsers(title='subcommands', required=True)
85
+ subp = argp.add_subparsers(title='subcommands', required=True, metavar='command')
65
86
  for cmd in sorted(CliCommand.commands.values(), key=lambda c: c.name):
66
87
  cmd(subp).add_arguments()
67
88
 
89
+ argcomplete.autocomplete(argp)
68
90
  args = argp.parse_args()
69
91
 
70
92
  if not hasattr(args, 'handler'):
@@ -6,8 +6,9 @@ import json
6
6
  import sys
7
7
  from abc import ABC, abstractmethod
8
8
  from argparse import ArgumentParser, Namespace
9
+ from collections.abc import Callable
9
10
  from pathlib import Path
10
- from typing import Any, Callable
11
+ from typing import Any
11
12
 
12
13
  import yaml
13
14
 
@@ -42,7 +43,7 @@ class CliCommand(ABC):
42
43
 
43
44
  commands: dict[str, type[CliCommand]] = {}
44
45
  name = None # Set by @register decorator for subclasses.
45
- help_ = None # Set by @register from first line of docstring.
46
+ help_ = None # Must be set in subclasses.
46
47
 
47
48
  # --------------------------------------------------------------------------
48
49
  @classmethod
@@ -64,10 +65,8 @@ class CliCommand(ABC):
64
65
  def decorate(cmd: type[CliCommand]):
65
66
  """Register the command handler class."""
66
67
  cmd.name = name
67
- try:
68
- cmd.help_ = cmd.__doc__.splitlines()[0]
69
- except (AttributeError, IndexError):
70
- raise Exception(f'Class {cmd.__name__} must have a docstring')
68
+ if not hasattr(cmd, 'help_') or not cmd.help_:
69
+ raise Exception(f'Class {cmd.__name__} must have a help_ attribute')
71
70
  cls.commands[name] = cmd
72
71
  return cmd
73
72
 
@@ -80,12 +79,12 @@ class CliCommand(ABC):
80
79
  self.argp.set_defaults(handler=self)
81
80
 
82
81
  # --------------------------------------------------------------------------
83
- def add_arguments(self): # noqa B027
82
+ def add_arguments(self): # noqa: B027
84
83
  """Add arguments to the command handler."""
85
84
  pass
86
85
 
87
86
  # --------------------------------------------------------------------------
88
- @staticmethod # noqa B027
87
+ @staticmethod # noqa: B027
89
88
  def check_arguments(args: Namespace):
90
89
  """
91
90
  Validate arguments.
@@ -4,7 +4,6 @@ from __future__ import annotations
4
4
 
5
5
  from argparse import Namespace
6
6
 
7
- from docma import compile_template
8
7
  from .__common__ import CliCommand
9
8
 
10
9
 
@@ -13,6 +12,8 @@ from .__common__ import CliCommand
13
12
  class Compile(CliCommand):
14
13
  """Compile a source directory into a document template."""
15
14
 
15
+ help_ = 'Compile a source directory into a document template.'
16
+
16
17
  # --------------------------------------------------------------------------
17
18
  def add_arguments(self) -> None:
18
19
  """Add arguments to the command handler."""
@@ -36,4 +37,6 @@ class Compile(CliCommand):
36
37
  @staticmethod
37
38
  def execute(args: Namespace) -> None:
38
39
  """Execute the command."""
40
+ from docma import compile_template
41
+
39
42
  compile_template(src_dir=args.input, tpkg=args.template)
@@ -0,0 +1,42 @@
1
+ """Handler for completion CLI command."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import sys
7
+ from argparse import Namespace
8
+
9
+ from argcomplete import shellcode
10
+
11
+ from .__common__ import CliCommand
12
+
13
+ SHELL = os.path.basename(os.getenv('SHELL', 'bash'))
14
+
15
+
16
+ # ------------------------------------------------------------------------------
17
+ @CliCommand.register('completion')
18
+ class Info(CliCommand):
19
+ """Generate the command line completion script."""
20
+
21
+ help_ = 'Add arguments to the command handler.'
22
+
23
+ # --------------------------------------------------------------------------
24
+ def add_arguments(self) -> None:
25
+ """Add arguments to the command handler."""
26
+
27
+ self.argp.add_argument(
28
+ '-s',
29
+ '--shell',
30
+ default=SHELL,
31
+ help=(
32
+ 'Output code for the specified shell. Defaults to the current shell,'
33
+ ' if that can be determined, otherwise "bash".'
34
+ ),
35
+ )
36
+
37
+ # --------------------------------------------------------------------------
38
+ @staticmethod
39
+ def execute(args: Namespace) -> None:
40
+ """Execute the CLI command with the specified arguments."""
41
+
42
+ print(shellcode([os.path.basename(sys.argv[0])], shell=args.shell))
@@ -14,15 +14,10 @@ from typing import Any
14
14
  import yaml
15
15
  from tqdm import tqdm
16
16
 
17
- from docma import render_template_to_html, safe_render_path
18
17
  from docma.config import LOGNAME
19
- from docma.data_providers import DataSourceSpec, load_data
20
- from docma.docma_core import PKG_CONFIG_FILE, coalesce_docma_render_params
21
- from docma.jinja import DocmaRenderContext
22
- from docma.lib.logging import setup_logging
18
+ from docma.exceptions import DocmaCommandError
23
19
  from docma.lib.misc import deep_update_dict
24
- from docma.lib.packager import PackageReader
25
- from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_params
20
+ from .__common__ import CliCommand, add_rendering_param_args
26
21
 
27
22
  LOG = getLogger(LOGNAME)
28
23
 
@@ -42,6 +37,8 @@ def renderer(
42
37
  :param kwargs: Passed directly to render_template_to_html(),.
43
38
  """
44
39
 
40
+ from docma import render_template_to_html
41
+
45
42
  LOG.debug('PID=%d: Output to %s', os.getpid(), output_file)
46
43
  html = render_template_to_html(
47
44
  render_params=deep_update_dict({}, common_params, batch_params), **kwargs
@@ -55,6 +52,8 @@ def renderer(
55
52
  class HtmlBatch(CliCommand):
56
53
  """Render a batch of HTML documents from a single document template."""
57
54
 
55
+ help_ = 'Render a batch of HTML documents from a single document template.'
56
+
58
57
  # --------------------------------------------------------------------------
59
58
  def add_arguments(self) -> None:
60
59
  """Add arguments to the command handler."""
@@ -72,16 +71,17 @@ class HtmlBatch(CliCommand):
72
71
  ),
73
72
  )
74
73
 
74
+ cpu_count = os.cpu_count() or 1
75
75
  self.argp.add_argument(
76
76
  '-n',
77
77
  '--nproc',
78
78
  type=int,
79
79
  metavar='PROCESS-COUNT',
80
- default=os.cpu_count(),
80
+ default=cpu_count,
81
81
  action='store',
82
82
  help=(
83
83
  'Number of processes to use for rendering the batch.'
84
- f' Defaults to the CPU count ({os.cpu_count()}).'
84
+ f' Defaults to the CPU count ({cpu_count}).'
85
85
  ),
86
86
  )
87
87
 
@@ -134,6 +134,14 @@ class HtmlBatch(CliCommand):
134
134
  def execute(args: Namespace) -> None:
135
135
  """Execute the CLI command with the specified arguments."""
136
136
 
137
+ from docma import safe_render_path
138
+ from docma.data_providers import DataSourceSpec, load_data
139
+ from docma.docma_core import PKG_CONFIG_FILE, coalesce_docma_render_params
140
+ from docma.jinja import DocmaRenderContext
141
+ from docma.lib.logging import setup_logging
142
+ from docma.lib.packager import PackageReader
143
+ from .__common__ import marshal_rendering_params
144
+
137
145
  if args.realm:
138
146
  os.environ['LAVA_REALM'] = args.realm
139
147
 
@@ -149,6 +157,8 @@ class HtmlBatch(CliCommand):
149
157
  )
150
158
  context = DocmaRenderContext(tpkg, render_params)
151
159
  batch_data = load_data(data_source_spec, context)
160
+ if not batch_data:
161
+ raise DocmaCommandError('No batch data returned')
152
162
 
153
163
  # We need to create a full rendering context for the data source spec
154
164
  # specified on the command line used to generate iteration data.
@@ -5,7 +5,6 @@ from __future__ import annotations
5
5
  import os
6
6
  from argparse import Namespace
7
7
 
8
- from docma import render_template_to_html
9
8
  from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_params
10
9
 
11
10
 
@@ -14,6 +13,8 @@ from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_
14
13
  class Render(CliCommand):
15
14
  """Render a document template to HTML."""
16
15
 
16
+ help_ = 'Render a document template to HTML.'
17
+
17
18
  # --------------------------------------------------------------------------
18
19
  def add_arguments(self) -> None:
19
20
  """Add arguments to the command handler."""
@@ -51,6 +52,8 @@ class Render(CliCommand):
51
52
  def execute(args: Namespace) -> None:
52
53
  """Execute the command."""
53
54
 
55
+ from docma import render_template_to_html
56
+
54
57
  if args.realm:
55
58
  os.environ['LAVA_REALM'] = args.realm
56
59
 
@@ -7,8 +7,6 @@ from argparse import Namespace
7
7
 
8
8
  import yaml
9
9
 
10
- from docma import get_template_info
11
- from docma.lib.packager import PackageReader
12
10
  from .__common__ import CliCommand
13
11
 
14
12
 
@@ -17,6 +15,8 @@ from .__common__ import CliCommand
17
15
  class Info(CliCommand):
18
16
  """Print information about a document template."""
19
17
 
18
+ help_ = 'Print information about a document template.'
19
+
20
20
  # --------------------------------------------------------------------------
21
21
  def add_arguments(self) -> None:
22
22
  """Add arguments to the command handler."""
@@ -33,5 +33,8 @@ class Info(CliCommand):
33
33
  @staticmethod
34
34
  def execute(args: Namespace) -> None:
35
35
  """Execute the CLI command with the specified arguments."""
36
+ from docma import get_template_info
37
+ from docma.lib.packager import PackageReader
38
+
36
39
  with PackageReader.new(args.template) as tpkg:
37
40
  yaml.safe_dump(get_template_info(tpkg), sys.stdout, default_flow_style=False, indent=2)
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
+ import logging
6
7
  import os
7
8
  from argparse import Namespace
8
9
  from contextlib import contextmanager
@@ -10,18 +11,37 @@ from importlib import resources
10
11
  from pathlib import Path
11
12
 
12
13
  from babel import default_locale
14
+ from cookiecutter.exceptions import FailedHookException
13
15
  from cookiecutter.main import cookiecutter
14
16
 
15
17
  import docma
18
+ from docma.exceptions import DocmaCommandError
16
19
  from docma.lib.misc import StoreNameValuePair
17
20
  from .__common__ import CliCommand
18
21
 
19
22
 
23
+ # ------------------------------------------------------------------------------
24
+ class HookFailureFilter(logging.Filter):
25
+ """Filter to suppress stack traces in cookiecutter hook failures."""
26
+
27
+ def filter(self, record): # noqa: A003
28
+ """Suppress stack traces in cookiecutter hook failures."""
29
+ if record.name == 'cookiecutter.hooks' and record.msg.startswith(
30
+ 'Stopping generation because '
31
+ ):
32
+ record.exc_info = None
33
+ record.exc_text = None
34
+
35
+ return True
36
+
37
+
20
38
  # ------------------------------------------------------------------------------
21
39
  @CliCommand.register('new')
22
40
  class New(CliCommand):
23
41
  """Create a new docma template source directory."""
24
42
 
43
+ help_ = 'Create a new docma template source directory.'
44
+
25
45
  # --------------------------------------------------------------------------
26
46
  def add_arguments(self) -> None:
27
47
  """Add arguments to the command handler."""
@@ -40,7 +60,7 @@ class New(CliCommand):
40
60
  .joinpath('resources', 'cookiecutter', 'cookiecutter.json')
41
61
  .read_text()
42
62
  )
43
- cookie_keys = sorted(k for k in cookie_obj if not k.startswith('_'))
63
+ cookie_keys: list[str] = sorted(k for k in cookie_obj if not k.startswith('_'))
44
64
  self.argp.add_argument(
45
65
  '-p',
46
66
  '--param',
@@ -73,25 +93,31 @@ class New(CliCommand):
73
93
 
74
94
  template_path = resources.files(docma).joinpath('resources', 'cookiecutter')
75
95
 
96
+ # Cookiecutter prints horrible stack traces so we filter them out.
97
+ logging.getLogger('cookiecutter.hooks').addFilter(HookFailureFilter())
98
+
76
99
  # We want to make the docma package available to the cookiecutter
77
100
  # validation scripts. As cookiecutter runs in a subprocess we need to
78
101
  # mutate PYTHONPATH.
79
- # noinspection PyUnresolvedReferences
80
- with (
81
- pythonpath_prepended(resources.files(docma).parent.resolve()),
82
- resources.as_file(template_path) as template_dir,
83
- ):
84
- new_dir = cookiecutter(
85
- str(template_dir),
86
- overwrite_if_exists=False,
87
- extra_context={
88
- 'locale': default_locale(),
89
- 'template_id': d.stem,
90
- 'template_src_dir': args.directory,
91
- }
92
- | args.params,
93
- no_input=args.no_input,
94
- )
102
+ try:
103
+ # noinspection PyUnresolvedReferences
104
+ with (
105
+ pythonpath_prepended(resources.files(docma).parent.resolve()),
106
+ resources.as_file(template_path) as template_dir,
107
+ ):
108
+ new_dir = cookiecutter(
109
+ str(template_dir),
110
+ overwrite_if_exists=False,
111
+ extra_context={
112
+ 'locale': default_locale(),
113
+ 'template_id': d.stem,
114
+ 'template_src_dir': args.directory,
115
+ }
116
+ | args.params,
117
+ no_input=args.no_input,
118
+ )
119
+ except FailedHookException as e:
120
+ raise DocmaCommandError('Failed to create new docma template') from e
95
121
  print(f'Created {new_dir}')
96
122
 
97
123
 
@@ -13,14 +13,10 @@ from typing import Any
13
13
  import yaml
14
14
  from tqdm import tqdm
15
15
 
16
- from docma import render_template_to_pdf, safe_render_path
17
16
  from docma.config import LOGNAME
18
- from docma.data_providers import DataSourceSpec, load_data
19
- from docma.docma_core import PKG_CONFIG_FILE, coalesce_docma_render_params
20
- from docma.jinja import DocmaRenderContext
17
+ from docma.exceptions import DocmaCommandError
21
18
  from docma.lib.logging import setup_logging
22
19
  from docma.lib.misc import deep_update_dict
23
- from docma.lib.packager import PackageReader
24
20
  from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_params
25
21
 
26
22
  LOG = getLogger(LOGNAME)
@@ -41,6 +37,8 @@ def renderer(
41
37
  :param kwargs: Passed directly to render_template_to_pdf(),.
42
38
  """
43
39
 
40
+ from docma import render_template_to_pdf
41
+
44
42
  LOG.debug('PID=%d: Output to %s', os.getpid(), output_file)
45
43
  pdf = render_template_to_pdf(
46
44
  render_params=deep_update_dict({}, common_params, batch_params), **kwargs
@@ -54,6 +52,8 @@ def renderer(
54
52
  class PdfBatch(CliCommand):
55
53
  """Render a batch of PDF documents from a single document template."""
56
54
 
55
+ help_ = 'Render a batch of PDF documents from a single document template.'
56
+
57
57
  # --------------------------------------------------------------------------
58
58
  def add_arguments(self) -> None:
59
59
  """Add arguments to the command handler."""
@@ -71,16 +71,17 @@ class PdfBatch(CliCommand):
71
71
  ),
72
72
  )
73
73
 
74
+ cpu_count = os.cpu_count() or 1
74
75
  self.argp.add_argument(
75
76
  '-n',
76
77
  '--nproc',
77
78
  type=int,
78
79
  metavar='PROCESS-COUNT',
79
- default=os.cpu_count(),
80
+ default=cpu_count,
80
81
  action='store',
81
82
  help=(
82
83
  'Number of processes to use for rendering the batch.'
83
- f' Defaults to the CPU count ({os.cpu_count()}).'
84
+ f' Defaults to the CPU count ({cpu_count}).'
84
85
  ),
85
86
  )
86
87
 
@@ -180,6 +181,12 @@ class PdfBatch(CliCommand):
180
181
  def execute(args: Namespace) -> None:
181
182
  """Execute the CLI command with the specified arguments."""
182
183
 
184
+ from docma import safe_render_path
185
+ from docma.data_providers import DataSourceSpec, load_data
186
+ from docma.docma_core import PKG_CONFIG_FILE, coalesce_docma_render_params
187
+ from docma.jinja import DocmaRenderContext
188
+ from docma.lib.packager import PackageReader
189
+
183
190
  if args.realm:
184
191
  os.environ['LAVA_REALM'] = args.realm
185
192
 
@@ -194,6 +201,8 @@ class PdfBatch(CliCommand):
194
201
  )
195
202
  context = DocmaRenderContext(tpkg, render_params)
196
203
  batch_data = load_data(data_source_spec, context)
204
+ if not batch_data:
205
+ raise DocmaCommandError('No batch data returned')
197
206
  # We don't need to pass the full context params to the workers as
198
207
  # they will create their own. They just need the CLI params and
199
208
  # the batch row data.
@@ -5,7 +5,6 @@ from __future__ import annotations
5
5
  import os
6
6
  from argparse import Namespace
7
7
 
8
- from docma import render_template_to_pdf
9
8
  from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_params
10
9
 
11
10
 
@@ -14,6 +13,8 @@ from .__common__ import CliCommand, add_rendering_param_args, marshal_rendering_
14
13
  class Render(CliCommand):
15
14
  """Render a document template to PDF."""
16
15
 
16
+ help_ = 'Render a document template to PDF.'
17
+
17
18
  # --------------------------------------------------------------------------
18
19
  def add_arguments(self) -> None:
19
20
  """Add arguments to the command handler."""
@@ -98,6 +99,8 @@ class Render(CliCommand):
98
99
  def execute(args: Namespace) -> None:
99
100
  """Execute the command."""
100
101
 
102
+ from docma import render_template_to_pdf
103
+
101
104
  if args.realm:
102
105
  os.environ['LAVA_REALM'] = args.realm
103
106