Passed
Push — master ( da3663...1303fa )
by John
01:36
created

tcllib.tclchecksum   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B TclChecksumMixin.do_checksum() 0 24 3
A TclChecksumMixin.parse_checksum() 0 10 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# pylint: disable=C0111,C0326,C0103
5
6
"""Tools to interface with TCL's checksum API."""
7
8
import json
9
10
from defusedxml import ElementTree
11
12
13
class TclChecksumMixin:
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
14
    """A mixin component for TCL's checksum API."""
15
    def do_checksum(self, encslave, address, uri):
16
        """Perform checksum request with given parameters."""
17
        url = "http://" + encslave + "/checksum.php"
18
        params = self.get_creds2()
19
20
        payload = {address: uri}
21
        payload_json = json.dumps(payload)
22
        params[b"address"] = bytes(payload_json, "utf-8")
23
24
        # print(repr(dict(params)))
25
        req = self.sess.post(url, data=params)
26
        if req.status_code == 200:
27
            req.encoding = "utf-8"  # Force encoding as server doesn't give one
28
            self.write_dump(req.text)
29
            # <ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER> means ERROR (invalid ADDRESS!)
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
30
            if "<ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER>" in req.text:
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
31
                print("INVALID URI: {}".format(uri))
32
                raise SystemExit
33
            return req.text
34
        else:
35
            print("CHECKSUM: " + repr(req))
36
            print(repr(req.headers))
37
            print(repr(req.text))
38
            raise SystemExit
39
40
    @staticmethod
41
    def parse_checksum(xmlstr):
42
        """Parse output of ``do_checksum``."""
43
        root = ElementTree.fromstring(xmlstr)
44
        file = root.find("FILE_CHECKSUM_LIST").find("FILE")
45
        file_addr = file.find("ADDRESS").text
46
        sha1_enc_footer = file.find("ENCRYPT_FOOTER").text
47
        sha1_footer = file.find("FOOTER").text
48
        sha1_body = file.find("BODY").text
49
        return file_addr, sha1_body, sha1_enc_footer, sha1_footer
50