JSONExporter.entry_to_dict()   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 34
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 30
nop 2
dl 0
loc 34
rs 4.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like jrnl.plugins.json_exporter.JSONExporter.entry_to_dict() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
# encoding: utf-8
3
4
import json
5
6
from .text_exporter import TextExporter
7
from .util import get_tags_count
8
9
10
class JSONExporter(TextExporter):
11
    """This Exporter can convert entries and journals into json."""
12
13
    names = ["json"]
14
    extension = "json"
15
16
    @classmethod
17
    def entry_to_dict(cls, entry):
18
        entry_dict = {
19
            "title": entry.title,
20
            "body": entry.body,
21
            "date": entry.date.strftime("%Y-%m-%d"),
22
            "time": entry.date.strftime("%H:%M"),
23
            "tags": entry.tags,
24
            "starred": entry.starred,
25
        }
26
        if hasattr(entry, "uuid"):
27
            entry_dict["uuid"] = entry.uuid
28
        if (
29
            hasattr(entry, "creator_device_agent")
30
            or hasattr(entry, "creator_generation_date")
31
            or hasattr(entry, "creator_host_name")
32
            or hasattr(entry, "creator_os_agent")
33
            or hasattr(entry, "creator_software_agent")
34
        ):
35
            entry_dict["creator"] = {}
36
            if hasattr(entry, "creator_device_agent"):
37
                entry_dict["creator"]["device_agent"] = entry.creator_device_agent
38
            if hasattr(entry, "creator_generation_date"):
39
                entry_dict["creator"]["generation_date"] = str(
40
                    entry.creator_generation_date
41
                )
42
            if hasattr(entry, "creator_host_name"):
43
                entry_dict["creator"]["host_name"] = entry.creator_host_name
44
            if hasattr(entry, "creator_os_agent"):
45
                entry_dict["creator"]["os_agent"] = entry.creator_os_agent
46
            if hasattr(entry, "creator_software_agent"):
47
                entry_dict["creator"]["software_agent"] = entry.creator_software_agent
48
49
        return entry_dict
50
51
    @classmethod
52
    def export_entry(cls, entry):
53
        """Returns a json representation of a single entry."""
54
        return json.dumps(cls.entry_to_dict(entry), indent=2) + "\n"
55
56
    @classmethod
57
    def export_journal(cls, journal):
58
        """Returns a json representation of an entire journal."""
59
        tags = get_tags_count(journal)
60
        result = {
61
            "tags": {tag: count for count, tag in tags},
62
            "entries": [cls.entry_to_dict(e) for e in journal.entries],
63
        }
64
        return json.dumps(result, indent=2)
65