Test Failed
Pull Request — main (#5)
by Mohammad
02:48
created

tracking_policy_agendas.api.downloader()   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 8
dl 14
loc 14
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
"""
2
API
3
....................................................................................................
4
MIT License
5
Copyright (c) 2021-2023 AUT Iran, Mohammad H Forouhesh
6
Copyright (c) 2021-2022 MetoData.ai, Mohammad H Forouhesh
7
....................................................................................................
8
This module contains tools to download resources over http connections.
9
supported http links are:
10
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/xgb_vaccine',
0 ignored issues
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...
11
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

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

Loading history...
12
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

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

Loading history...
13
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

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

Loading history...
14
"""
15
16
import os
17
from typing import Union
18
19
import requests
20
import zipfile
0 ignored issues
show
introduced by
standard import "import zipfile" should be placed before "import requests"
Loading history...
21
from io import BytesIO
0 ignored issues
show
introduced by
standard import "from io import BytesIO" should be placed before "import requests"
Loading history...
22
23
http_dict = {'xgb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/xgb_vaccine.zip',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

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

Loading history...
24
             'pa_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine.zip',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

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

Loading history...
25
             'lasso_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine.zip',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (133/100).

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

Loading history...
26
             'gnb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine.zip'}
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

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

Loading history...
27
28
29 View Code Duplication
def downloader(path: str, save_path: str) -> Union[int, None]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
30
    """
31
    A tool to download and save files over https.
32
    :param path:        The path to the desired file.
33
    :param save_path:   The intended storage path.
34
    :return:            If the file exists, it returns 0 (int), otherwise nothing would be returned.
35
    """
36
    try:
37
        model_bin = requests.get(path, allow_redirects=True)
38
        with zipfile.ZipFile(BytesIO(model_bin.content)) as resource:
39
            resource.extractall(save_path)
40
    except Exception:
41
        raise Exception('not a proper webpage')
42
    return 0
43
44
45 View Code Duplication
def get_resources(dir_path: str, resource_name: str) -> Union[int, str]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
46
    """
47
    A tool to download required resources over internet.
48
    :param dir_path:        Path to the https link of the resource
49
    :param resource_name:   Resource name.
50
    :return:                Path to the downloaded resource.
51
    """
52
    save_dir = dir_path + 'model_dir/'
53
    if os.path.isdir(save_dir + f'{resource_name}/'): return 0
0 ignored issues
show
Coding Style introduced by
More than one statement on a single line
Loading history...
54
    os.makedirs(save_dir, exist_ok=True)
55
    downloader(path=http_dict[resource_name], save_path=save_dir)
56
    return str(save_dir + resource_name)
57