Passed
Branch master (929f74)
by Cyb3r
01:56
created

MetaStalk.utils.export.export()   B

Complexity

Conditions 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.0189

Importance

Changes 0
Metric Value
cc 8
eloc 15
nop 3
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
crap 8.0189
rs 7.3333
c 0
b 0
f 0
1
"""utils.export
2
---
3
4
Exports the plots as interactive html
5
"""
6 1
import logging
7 1
import os
8
9
10 1
class export():
11
    """export
12
    ---
13
14
    Deals with the export to html and images. Probably not the best way to run everything but it
15
    works the best.
16
    """
17 1
    def __init__(self, choice: str, output_dir: str, plots: dict):
18 1
        self.log = logging.getLogger("MetaStalk")
19 1
        self.choice = choice
20 1
        self.output_dir = output_dir
21 1
        self.plots = plots
22 1
        self.directory_management()
23 1
        if choice in ["html", "html_offline"]:
24 1
            self.html_export()
25 1
        elif choice in ["pdf", "svg", "webp", "jpeg", "png"]:
26 1
            self.image_export()
27
28 1
    def directory_management(self):
29
        """directory_management
30
        ---
31
        Creates directory for output if it does not exist and if it does then it will check if there
32
        are already file in it.
33
34
        Arguments:
35
            output_dir {str} -- Name of the directory to check/create.
36
        """
37 1
        if not os.path.isdir(self.output_dir):
38 1
            os.makedirs(self.output_dir)
39
        else:
40 1
            if len(os.listdir(self.output_dir)) != 0:
41 1
                self.log.warning("The chosen output directory contain files.")
42
43 1
    def image_export(self):
44
        """image_export
45
        ---
46
47
        Deals with export images
48
49
        Raises:
50
            EnvironmentError: Raised if packages from metastalk[image] are not installed.
51
        """
52 1
        for name, chart in self.plots.items():
53 1
            try:
54 1
                chart.write_image(f"{self.output_dir}/{name}.{self.choice}")
55 1
            except ValueError:
56 1
                self.log.error("Missing packages from metastalk[image]")
57 1
                raise EnvironmentError("Missing packages from metastalk[image]")
58
59 1
    def html_export(self):
60
        """html_export
61
        ---
62
        Deals with export of html.
63
        If offline is true then there will be a script tag in the .html file making it much larger
64
        but if it is cdn then the html will cotain a script tag that points to
65
        https://cdn.plot.ly/plotly-latest.min.js
66
        """
67 1
        if self.choice == "html_offline":
68 1
            offline = True
69
        else:
70 1
            offline = "cdn"
71 1
        for name, chart in self.plots.items():
72 1
            chart.write_html(f"{self.output_dir}/{name}.html",
73
                             include_plotlyjs=offline,
74
                             )
75