1 | """ |
||
2 | API |
||
3 | |||
4 | .................................................................................................... |
||
5 | MIT License |
||
6 | |||
7 | Copyright (c) 2021-2023 AUT Iran, Mohammad H Forouhesh |
||
8 | Copyright (c) 2021-2022 MetoData.ai, Mohammad H Forouhesh |
||
9 | .................................................................................................... |
||
10 | This module contains tools to download resources over http connections. |
||
11 | supported http links are: |
||
12 | - https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v2.0.0.alpha/UPC_full_model_wapiti |
||
0 ignored issues
–
show
|
|||
13 | - https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v2.0.0.alpha/perpos.model |
||
0 ignored issues
–
show
|
|||
14 | - https://raw.githubusercontent.com/MohammadForouhesh/crf-pos-persian/main/resources/corrections.txt |
||
0 ignored issues
–
show
|
|||
15 | """ |
||
16 | |||
17 | 1 | import os |
|
18 | 1 | from typing import Union |
|
19 | |||
20 | 1 | import requests |
|
21 | |||
22 | 1 | http_dict = {'UPC_full_model_wapiti': 'https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v.2.2.2/UPC_full_model_wapiti', |
|
0 ignored issues
–
show
|
|||
23 | 'perpos.model': 'https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v.2.2.2/perpos.model', |
||
0 ignored issues
–
show
|
|||
24 | 'corrections.txt': 'https://raw.githubusercontent.com/MohammadForouhesh/crf-pos-persian/main/resources/corrections.txt'} |
||
0 ignored issues
–
show
|
|||
25 | |||
26 | |||
27 | 1 | def downloader(path: str, save_path: str, mode: str) -> Union[int, None]: |
|
0 ignored issues
–
show
|
|||
28 | """ |
||
29 | A tool to download and save files over https. |
||
30 | supported http links are: |
||
31 | - https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v2.0.0.alpha/UPC_full_model_wapiti |
||
0 ignored issues
–
show
|
|||
32 | - https://github.com/MohammadForouhesh/crf-pos-persian/releases/download/v.2.2.2/perpos.model |
||
33 | - https://raw.githubusercontent.com/MohammadForouhesh/crf-pos-persian/main/resources/corrections.txt |
||
0 ignored issues
–
show
|
|||
34 | |||
35 | :param path: The path to the desired file. |
||
36 | :param save_path: The intended storage path. |
||
37 | :param mode: The mode that it should be stored. |
||
38 | :return: If the file exists, it returns 0 (int), otherwise nothing would be returned. |
||
39 | """ |
||
40 | 1 | if os.path.isfile(save_path): return 0 |
|
0 ignored issues
–
show
|
|||
41 | 1 | try: |
|
42 | 1 | print('Downloading resources ...') |
|
43 | 1 | model_bin = requests.get(path, allow_redirects=True) |
|
44 | 1 | with open(save_path, mode) as resource: |
|
45 | 1 | resource.write(model_bin.content) |
|
46 | except Exception as e: |
||
0 ignored issues
–
show
Variable name "e" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)
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. ![]() |
|||
47 | raise e.with_traceback |
||
0 ignored issues
–
show
|
|||
48 | |||
49 | |||
50 | 1 | def get_resources(dir_path: str, resource_name: str) -> str: |
|
51 | """ |
||
52 | A tool to download required resources over internet. |
||
53 | :param dir_path: Path to the https link of the resource |
||
54 | :param resource_name: Resource name. |
||
55 | :return: Path to the downloaded resource. |
||
56 | """ |
||
57 | 1 | save_dir = dir_path + 'resources/' |
|
58 | 1 | os.makedirs(save_dir, exist_ok=True) |
|
59 | 1 | downloader(path=http_dict[resource_name], save_path=save_dir + resource_name, mode='wb') |
|
60 | return str(save_dir + resource_name) |
||
61 |
This check looks for lines that are too long. You can specify the maximum line length.