File: directive.py

package info (click to toggle)
sphinxcontrib-datatemplates 0.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 580 kB
  • sloc: python: 828; makefile: 172; xml: 38; javascript: 1
file content (536 lines) | stat: -rw-r--r-- 18,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import codecs
import csv
import json
import mimetypes
from collections import defaultdict

import defusedxml.ElementTree as ET
import jinja2
import yaml
from docutils import nodes
from docutils.parsers import rst
from docutils.statemachine import ViewList
from sphinx.jinja2glue import BuiltinTemplateLoader
from sphinx.util import logging, import_object
from sphinx.util.nodes import nested_parse_with_titles
from sphinxcontrib.datatemplates import helpers, loaders

LOG = logging.getLogger(__name__)
_default_templates = None


def _templates(builder):
    global _default_templates

    # Some builders have no templates manager at all, and some
    # have the attribute set to None.
    templates = getattr(builder, 'templates', None)

    if not templates:
        if not _default_templates:
            # Initialize default templates manager once
            if builder.config.template_bridge:
                _default_templates = import_object(
                    objname=builder.config.template_bridge,
                    source="template_bridge setting",
                )()
            else:
                _default_templates = BuiltinTemplateLoader()
            _default_templates.init(builder)

        templates = _default_templates

    return templates


def flag_true(argument):
    """
    Check for a valid flag option (no argument) and return ``True``.
    (Directive option conversion function.)
    Raise ``ValueError`` if an argument is found.
    """
    if argument and argument.strip():
        raise ValueError('no argument is allowed; "%s" supplied' % argument)
    else:
        return True


def unknown_option(argument):
    """
    Check for a valid flag option (no argument) and return ``True``,
    else return argument stripped.
    (Directive option conversion function.)

    For unknown options we cannot know if they should be
    passed to the loader as flags or strings.
    We could pass ``None`` if the option string contains nothing
    except whitespace but this would not be intuitive for
    keyword argument flags as ``bool(None) is False``.
    """
    if argument:
        stripped = argument.strip()
        if stripped:
            return stripped
    return True


def unchanged_factory():
    return unknown_option


class DataTemplateBase(rst.Directive):

    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = defaultdict(unchanged_factory, {
        'source': rst.directives.path,
        'template': rst.directives.path,
    })
    has_content = True

    @staticmethod
    def loader():
        return NotImplemented

    def _make_context(self, data, config, env):
        return {
            'make_list_table': helpers.make_list_table,
            'make_list_table_from_mappings':
            helpers.make_list_table_from_mappings,
            'escape_rst': helpers.escape_rst,
            'escape_rst_url': helpers.escape_rst_url,
            'data': data,
            'config': config,
            'options': self.options,
            'env': env,
            'load': self._dynamic_load,
        }

    def _dynamic_load(self, source, data_format=None, **input_loader_options):
        # FIXME: This does not work for dbm or other databases because
        # the handle is closed.
        env = self.state.document.settings.env
        relative_resolved_path, absolute_resolved_path = env.relfn2path(source)
        # Only add a dependency if we were given an explicit
        # source. Otherwise, we end up adding a directory as a
        # dependency, which is illegal. See
        # https://github.com/sphinx-contrib/datatemplates/pull/83
        if source:
            env.note_dependency(absolute_resolved_path)

        if data_format is not None:
            loader = loaders.loader_by_name(data_format)
            if loader is None:
                raise ValueError('Could not find loader named {!r}'.format(
                    data_format))
        else:
            loader = loaders.loader_for_source(source, default=self.loader)

        loader_options = {
            "source": source,
            "relative_resolved_path": relative_resolved_path,
            "absolute_resolved_path": absolute_resolved_path,
        }

        if loader == self.loader:
            for k, v in self.options.items():
                # make identifier-compatible if trivially possible
                k = k.lower().replace(
                    "-", "_")
                loader_options.setdefault(k, v)  # do not overwrite

        loader_options.update(input_loader_options)

        with loader(**loader_options) as data:
            return data

    def run(self):
        env = self.state.document.settings.env
        app = env.app
        builder = app.builder

        if 'source' in self.options:
            source = self.options['source']
        elif self.arguments:
            source = self.arguments[0]
        elif self.loader in {loaders.load_import_module, loaders.load_nodata}:
            source = ""
        else:
            error = self.state_machine.reporter.error(
                'Source file is required',
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        relative_resolved_path, absolute_resolved_path = env.relfn2path(source)

        # Only add a dependency if we were given an explicit
        # source. Otherwise, we end up adding a directory as a
        # dependency, which is illegal. See
        # https://github.com/sphinx-contrib/datatemplates/pull/83
        if source:
            env.note_dependency(absolute_resolved_path)

        if 'template' in self.options:
            template = self.options['template']
            render_function = _templates(builder).render
        else:
            template = '\n'.join(self.content)
            render_function = _templates(builder).render_string

        if not template:
            error = self.state_machine.reporter.error(
                "Template is empty",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        loader_options = {
            "source": source,
            "relative_resolved_path": relative_resolved_path,
            "absolute_resolved_path": absolute_resolved_path,
        }
        for k, v in self.options.items():
            k = k.lower().replace(
                "-", "_")  # make identifier-compatible if trivially possible
            loader_options.setdefault(k, v)  # do not overwrite

        try:
            with self.loader(**loader_options) as data:
                context = self._make_context(data, app.config, env)
                rendered_template = render_function(
                    template,
                    context,
                )
        except FileNotFoundError:
            error = self.state_machine.reporter.error(
                f"Source file '{relative_resolved_path}' not found",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]
        except loaders.LoaderError as err:
            error = self.state_machine.reporter.error(
                f"Error in source '{relative_resolved_path}': {err}",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]
        except jinja2.exceptions.TemplateNotFound:
            error = self.state_machine.reporter.error(
                f"Template file '{template}' not found",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]
        except jinja2.exceptions.TemplateSyntaxError as err:
            error = self.state_machine.reporter.error(
                f"Error in template file '{template}' line {err.lineno}: "
                f"{err.message}",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        result = ViewList()
        for line in rendered_template.splitlines():
            result.append(line, source)
        node = nodes.section()
        node.document = self.state.document
        nested_parse_with_titles(self.state, result, node)
        return node.children

    @classmethod
    def usage(cls):
        _, sep, doc = cls.__doc__.partition(".. rst:directive::")
        return sep + doc


class DataTemplateWithEncoding(DataTemplateBase):
    option_spec = defaultdict(unchanged_factory, DataTemplateBase.option_spec,
                              **{
                                  'encoding': rst.directives.encoding,
                              })


class DataTemplateNoData(DataTemplateBase):
    """
    .. rst:directive:: .. datatemplate:nodata::

        Load ``None`` as ``data`` and render
        using ``template`` given in directive body.

        .. rst:directive:option:: template: template name, optional

            The name of a template file on the Sphinx template search path.
            Overrides directive body.
    """

    loader = staticmethod(loaders.load_nodata)


class DataTemplateJSON(DataTemplateWithEncoding):
    """
    .. rst:directive:: .. datatemplate:json:: source-path

        Load file at ``source-path`` (relative to the documentation
        build directory) via :py:func:`json.load` and render using
        ``template`` given in directive body.

        .. rst:directive:option:: template: template name, optional

            The name of a template file on the Sphinx template search path.
            Overrides directive body.

        .. rst:directive:option:: encoding: optional, defaults to ``utf-8-sig``

            The text encoding that will be used to read the source file.
            See :any:`standard-encodings`
    """

    loader = staticmethod(loaders.load_json)


def _handle_dialect_option(argument):
    return rst.directives.choice(argument, ["auto"] + csv.list_dialects())


class DataTemplateCSV(DataTemplateWithEncoding):
    """
    .. rst:directive:: .. datatemplate:csv:: source-path

        Load file at ``source-path`` (relative to the documentation
        build directory) via :py:func:`csv.reader` or
        :py:class:`csv.DictReader` depending on ``header``
        and render using ``template`` given in directive body.

        .. rst:directive:option:: template: template name, optional

            The name of a template file on the Sphinx template search path.
            Overrides directive body.

        .. rst:directive:option:: encoding: optional, defaults to ``utf-8-sig``

            The text encoding that will be used to read the source file.
            See :any:`standard-encodings`

        .. rst:directive:option:: header: flag, optional

                Set to use :py:class:`csv.DictReader` for reading the file.
                If not set :py:func:`csv.reader` is used.

        .. rst:directive:option:: dialect: optional

                Set to select a specific :py:class:`csv.Dialect`.
                Set to ``auto``, to try autodetection.
                If not set the default dialect is used.
    """

    option_spec = defaultdict(
        unchanged_factory, DataTemplateBase.option_spec, **{
            'headers': flag_true,
            'dialect': _handle_dialect_option,
        })

    loader = staticmethod(loaders.load_csv)


class DataTemplateYAML(DataTemplateWithEncoding):
    """
    .. rst:directive:: .. datatemplate:yaml:: source-path

        Load file at ``source-path`` (relative to the documentation build
        directory)  via PyYAML_ (:py:func:`yaml.safe_load`) and render
        using ``template`` given in directive body.

        .. _PyYAML: https://pyyaml.org

        .. rst:directive:option:: template: template name, optional

            The name of a template file on the Sphinx template search path.
            Overrides directive body.

        .. rst:directive:option:: encoding: optional, defaults to ``utf-8-sig``

            The text encoding that will be used to read the source file.
            See :any:`standard-encodings`

        .. rst:directive:option:: multiple-documents: flag, optional

            Set to read multiple documents from the file into
            a :py:class:`list`
    """

    option_spec = defaultdict(unchanged_factory, DataTemplateBase.option_spec,
                              **{
                                  'multiple-documents': flag_true,
                              })

    loader = staticmethod(loaders.load_yaml)


class DataTemplateXML(DataTemplateBase):
    """
    .. rst:directive:: .. datatemplate:xml:: source-path

        Load file at ``source-path`` (relative to the documentation
        build directory)  via :py:func:`xml.etree.ElementTree.parse`
        (actually using ``defusedxml``) and render using ``template``
        given in directive body.

        .. rst:directive:option:: template: template name, optional

            The name of a template file on the Sphinx template search path.
            Overrides directive body.
    """

    loader = staticmethod(loaders.load_xml)


class DataTemplateDBM(DataTemplateBase):
    """
    .. rst:directive:: datatemplate:dbm:: source-path

        Load DB at ``source-path`` (relative to the documentation
        build directory)  via :py:func:`dbm.open` and render using
        ``template`` given in directive body.

        .. rst:directive:option:: template: template name, optional

                The name of a template file on the Sphinx template search path.
                Overrides directive body.
    """

    loader = staticmethod(loaders.load_dbm)


class DataTemplateImportModule(DataTemplateBase):
    """
    .. rst:directive:: .. datatemplate:import-module:: module-name

        Load module ``module-name`` (must be importable in ``conf.py``)
        via :py:func:`importlib.import_module` and render using
        ``template`` given in directive body.

        .. rst:directive:option:: template: template name, optional

                The name of a template file on the Sphinx template search path.
                Overrides directive body.
    """

    loader = staticmethod(loaders.load_import_module)


class DataTemplateLegacy(rst.Directive):

    option_spec = {
        'source': rst.directives.path,
        'template': rst.directives.path,
        'csvheaders': rst.directives.flag,
        'csvdialect': _handle_dialect_option,
        'encoding': rst.directives.encoding,
    }
    has_content = False

    def _load_csv(self, filename, encoding):
        try:
            if encoding is None:
                f = open(filename, 'r', newline='')
            else:
                f = codecs.open(filename, 'r', newline='', encoding=encoding)
            dialect = self.options.get('csvdialect')
            if dialect == "auto":
                sample = f.read(8192)
                f.seek(0)
                sniffer = csv.Sniffer()
                dialect = sniffer.sniff(sample)
            if 'csvheaders' in self.options:
                if dialect is None:
                    r = csv.DictReader(f)
                else:
                    r = csv.DictReader(f, dialect=dialect)
            else:
                if dialect is None:
                    r = csv.reader(f)
                else:
                    r = csv.reader(f, dialect=dialect)
            return list(r)
        finally:
            f.close()

    def _load_json(self, filename, encoding):
        try:
            if encoding is None:
                f = open(filename, 'r')
            else:
                f = codecs.open(filename, 'r', encoding=encoding)
            return json.load(f)
        finally:
            f.close()

    def _load_yaml(self, filename, encoding):
        try:
            if encoding is None:
                f = open(filename, 'r')
            else:
                f = codecs.open(filename, 'r', encoding=encoding)
            return yaml.safe_load(f)
        finally:
            f.close()

    def _load_data(self, env, data_source, encoding):
        rel_filename, filename = env.relfn2path(data_source)
        if data_source.endswith('.yaml'):
            return self._load_yaml(filename, encoding)
        elif data_source.endswith('.json'):
            return self._load_json(filename, encoding)
        elif data_source.endswith('.csv'):
            return self._load_csv(filename, encoding)
        elif "xml" in mimetypes.guess_type(data_source)[0]:
            # there are many XML based formats
            return ET.parse(filename).getroot()
        else:
            raise NotImplementedError('cannot load file type of %s' %
                                      data_source)

    def run(self):
        env = self.state.document.settings.env
        app = env.app
        builder = app.builder

        try:
            data_source = self.options['source']
        except KeyError:
            error = self.state_machine.reporter.error(
                'No source set for datatemplate directive',
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        try:
            template_name = self.options['template']
        except KeyError:
            error = self.state_machine.reporter.error(
                'No template set for datatemplate directive',
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        encoding = self.options.get('encoding', None)

        data = self._load_data(env, data_source, encoding)

        context = {
            'make_list_table': helpers.make_list_table,
            'make_list_table_from_mappings':
            helpers.make_list_table_from_mappings,
            'data': data,
        }
        rendered_template = _templates(builder).render(
            template_name,
            context,
        )

        result = ViewList()
        for line in rendered_template.splitlines():
            result.append(line, data_source)
        node = nodes.section()
        node.document = self.state.document
        nested_parse_with_titles(self.state, result, node)
        return node.children