Test Failed
Pull Request — main (#5)
by Mohammad
04:56
created

tracking_policy_agendas.api   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 44.64 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 25
loc 56
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A downloader() 13 13 3
A get_resources() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
43
44 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...
45
    """
46
    A tool to download required resources over internet.
47
    :param dir_path:        Path to the https link of the resource
48
    :param resource_name:   Resource name.
49
    :return:                Path to the downloaded resource.
50
    """
51
    save_dir = dir_path + 'model_dir/'
52
    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...
53
    os.makedirs(save_dir, exist_ok=True)
54
    downloader(path=http_dict[resource_name], save_path=save_dir)
55
    return str(save_dir + resource_name)
56