Passed
Push — main ( dd1fed...7811f0 )
by Sat CFDI
05:19
created

satcfdi.printer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 72.97%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 61
ccs 27
cts 37
cp 0.7297
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Representable.html_write() 0 8 2
A Representable.html_str() 0 8 2
A Representable.pdf_bytes() 0 3 1
A Representable.pdf_write() 0 4 1
A Representable.html_str_all() 0 9 2
A Representable.html_write_all() 0 9 2
1 1
from lxml.etree import QName
2 1
from weasyprint import HTML, CSS
3
4 1
from .transform import *
5 1
from .transform.pdf_environment import PDFEnvironment
6
7 1
PDF_CSS = CSS(string="@page {margin: 1.0cm 1.27cm 1.1cm 0.85cm;}")
8
9
10 1
class Representable:
11 1
    tag = None
12
13 1
    def html_write(self, target, templates_path=None):
14
        if templates_path:
15
            env = PDFEnvironment(templates_path=templates_path)
16
            init_template = env.get_template("_init.html")
17
        else:
18
            init_template = PDF_INIT_TEMPLATE
19
20
        init_template.stream({"c": self, "k": QName(self.tag).localname}).dump(target)
21
22 1
    def html_str(self, templates_path=None) -> str:
23 1
        if templates_path:
24 1
            env = PDFEnvironment(templates_path=templates_path)
25 1
            init_template = env.get_template("_init.html")
26
        else:
27 1
            init_template = PDF_INIT_TEMPLATE
28
29 1
        return init_template.render({"c": self, "k": QName(self.tag).localname})
30
31 1
    def pdf_write(self, target, templates_path=None):
32 1
        HTML(string=self.html_str(templates_path=templates_path)).write_pdf(
33
            target=target,
34
            stylesheets=[PDF_CSS]
35
        )
36
37 1
    def pdf_bytes(self, templates_path=None) -> bytes:
38
        return HTML(string=self.html_str(templates_path=templates_path)).write_pdf(
39
            stylesheets=[PDF_CSS]
40
        )
41
42 1
    @staticmethod
43 1
    def html_write_all(objs, target, templates_path=None):
44 1
        if templates_path:
45
            env = PDFEnvironment(templates_path=templates_path)
46
            init_template = env.get_template("_multiple.html")
47
        else:
48 1
            init_template = PDF_INIT_TEMPLATE
49
50 1
        init_template.stream({"c": [(QName(a.tag).localname, a) for a in objs], "k": '_multiple'}).dump(target)
51
52 1
    @staticmethod
53 1
    def html_str_all(objs, templates_path=None) -> str:
54 1
        if templates_path:
55
            env = PDFEnvironment(templates_path=templates_path)
56
            init_template = env.get_template("_multiple.html")
57
        else:
58 1
            init_template = PDF_INIT_TEMPLATE
59
60
        return init_template.render({"c": [(QName(a.tag).localname, a) for a in objs], "k": '_multiple'})
61