Passed
Push — master ( 2f86f7...74d48b )
by Markus
01:48
created

tcllib.tclrequest.TclRequest.do_request()   B

Complexity

Conditions 3

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 27
nop 5
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
import binascii
4
import hashlib
5
import random
6
import time
7
from math import floor
8
import zlib
9
from collections import OrderedDict
10
from defusedxml import ElementTree
11
12
'''
13
    private HashMap<String, String> buildDownloadUrisParams(UpdatePackageInfo updatePackageInfo) {
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (98/80).

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

Loading history...
14
        FotaLog.m28v(TAG, "doAfterCheck");
15
        String salt = FotaUtil.salt();
16
        HashMap linkedHashMap = new LinkedHashMap();
17
        linkedHashMap.put("id", this.internalBuilder.getParam("id"));
18
        linkedHashMap.put("salt", salt);
19
        linkedHashMap.put("curef", updatePackageInfo.mCuref);
20
        linkedHashMap.put("fv", updatePackageInfo.mFv);
21
        linkedHashMap.put("tv", updatePackageInfo.mTv);
22
        linkedHashMap.put("type", "Firmware");
23
        linkedHashMap.put("fw_id", updatePackageInfo.mFirmwareId);
24
        linkedHashMap.put("mode", "2");
25
        linkedHashMap.put("vk", generateVk2((LinkedHashMap) linkedHashMap.clone()));
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

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

Loading history...
26
        linkedHashMap.put("cltp", "10");
27
        linkedHashMap.put("cktp", this.internalBuilder.getParam("cktp"));
28
        linkedHashMap.put("rtd", this.internalBuilder.getParam("rtd"));
29
        linkedHashMap.put("chnl", this.internalBuilder.getParam("chnl"));
30
        return linkedHashMap;
31
    }
32
'''
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
33
34
class TclRequestMixin:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
35
    @staticmethod
36
    def get_salt():
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
37
        millis = floor(time.time() * 1000)
38
        tail = "{:06d}".format(random.randint(0, 999999))
39
        return "{}{}".format(str(millis), tail)
40
41
    def get_vk2(self, params_dict, cltp):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
42
        params_dict["cltp"] = cltp
43
        query = ""
44
        for k, v in params_dict.items():
0 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
45
            if len(query) > 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
46
                query += "&"
47
            query += k + "=" + str(v)
48
        vdk = zlib.decompress(binascii.a2b_base64(self.VDKEY))
49
        query += vdk.decode("utf-8")
50
        engine = hashlib.sha1()
51
        engine.update(bytes(query, "utf-8"))
52
        hexhash = engine.hexdigest()
53
        return hexhash
54
55
    def do_request(self, curef, fv, tv, fw_id):
0 ignored issues
show
Coding Style Naming introduced by
The name fv does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name tv does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
56
        url = "https://" + self.g2master + "/download_request.php"
57
        params = OrderedDict()
58
        params["id"]    = self.serid
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
59
        params["salt"]  = self.get_salt()
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
60
        params["curef"] = curef
61
        params["fv"]    = fv
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
62
        params["tv"]    = tv
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
63
        params["type"]  = self.ftype
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
64
        params["fw_id"] = fw_id
65
        params["mode"]  = self.mode.value
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
66
        params["vk"]    = self.get_vk2(params, self.cltp.value)
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
67
        params["cltp"]  = self.cltp.value
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
68
        params["cktp"]  = self.cktp.value
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
69
        params["rtd"]   = self.rtd.value
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
70
        if self.mode == self.MODE.FULL:
71
            params["foot"]  = 1
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
72
        params["chnl"]  = self.chnl.value
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
73
74
        #print(repr(dict(params)))
75
        req = self.sess.post(url, data=params)
76
        if req.status_code == 200:
77
            req.encoding = "utf-8"    # Force encoding as server doesn't give one
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (81/80).

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

Loading history...
78
            self.write_dump(req.text)
79
            return req.text
80
        else:
81
            print("REQUEST: " + repr(req))
82
            print(repr(req.headers))
83
            print(repr(req.text))
84
            raise SystemExit
85
86
    @staticmethod
87
    def parse_request(xmlstr):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
88
        root = ElementTree.fromstring(xmlstr)
89
        file = root.find("FILE_LIST").find("FILE")
90
        fileid  = file.find("FILE_ID").text
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
91
        fileurl = file.find("DOWNLOAD_URL").text
92
        s3_fileurl_node = file.find("S3_DOWNLOAD_URL")
93
        s3_fileurl = ""
94
        if s3_fileurl_node:
95
            s3_fileurl = s3_fileurl_node.text
96
        slave_list = root.find("SLAVE_LIST").findall("SLAVE")
97
        enc_list = root.find("SLAVE_LIST").findall("ENCRYPT_SLAVE")
98
        s3_slave_list = root.find("SLAVE_LIST").findall("S3_SLAVE")
99
        slaves = [s.text for s in slave_list]
100
        encslaves = [s.text for s in enc_list]
101
        s3_slaves = [s.text for s in s3_slave_list]
102
        return fileid, fileurl, slaves, encslaves, s3_fileurl, s3_slaves
103