jrnl.plugins.template_exporter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A GenericTemplateExporter.export_journal() 0 5 1
A GenericTemplateExporter.export_entry() 0 5 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A __exporter_from_file() 0 8 1
1
#!/usr/bin/env python
2
# encoding: utf-8
3
4
from glob import glob
5
import os
6
7
from .template import Template
8
from .text_exporter import TextExporter
9
10
11
class GenericTemplateExporter(TextExporter):
12
    """This Exporter can convert entries and journals into text files."""
13
14
    @classmethod
15
    def export_entry(cls, entry):
16
        """Returns a string representation of a single entry."""
17
        vars = {"entry": entry, "tags": entry.tags}
18
        return cls.template.render_block("entry", **vars)
19
20
    @classmethod
21
    def export_journal(cls, journal):
22
        """Returns a string representation of an entire journal."""
23
        vars = {"journal": journal, "entries": journal.entries, "tags": journal.tags}
24
        return cls.template.render_block("journal", **vars)
25
26
27
def __exporter_from_file(template_file):
28
    """Create a template class from a file"""
29
    name = os.path.basename(template_file).replace(".template", "")
30
    template = Template.from_file(template_file)
31
    return type(
32
        str(f"{name.title()}Exporter"),
33
        (GenericTemplateExporter,),
34
        {"names": [name], "extension": template.extension, "template": template},
35
    )
36
37
38
__all__ = []
39
40
# Factory pattern to create Exporter classes for all available templates
41
for template_file in glob("jrnl/templates/*.template"):
42
    __all__.append(__exporter_from_file(template_file))
43