File size: 2,114 Bytes
dfd1909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import Literal
import os, sys

PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'

class Util:
    @staticmethod 
    def print(text:str, type:Literal['info', 'success', 'warning', 'error'] = None) -> None:
        template_dict:dict = {
            'info': {
                'color': BOLD + BLUE,
                'prefix': '[Info]: '
            },
            'success': {
                'color': BOLD + GREEN,
                'prefix': '[Success]: '
            },
            'warning': {
                'color': BOLD + YELLOW,
                'prefix': '[Warning]: '
            },
            'error': {
                'color': BOLD + RED,
                'prefix': '[Error]: '
            }
        }
        color:str = template_dict.get(type, {}).get('color', '')
        prefix:str = template_dict.get(type, {}).get('prefix', '')
        print(f"{color + prefix + text + END}")

    @staticmethod
    def set_sys_path_to_parent_dir(file:str, # __file__
                                   depth_to_dir_from_file:int = 1,
                                   ) -> None:
        dir : str = os.path.abspath(os.path.dirname(file))
        for _ in range(depth_to_dir_from_file): dir = os.path.dirname(dir)
        sys.path[0] = os.path.abspath(dir)
    
    @staticmethod
    def system(command:str) -> None:
        result_id:int = os.system(command)
        assert result_id == 0, f'[Error]: Something wrong with the command [{command}]'
    
    @staticmethod
    def wget(link:str, save_dir:str) -> None:
        os.makedirs(save_dir, exist_ok=True)
        Util.system(f'wget {link} -P {save_dir}')
    
    @staticmethod
    def unzip(file_path:str, unzip_dir:str) -> None:
        os.makedirs(unzip_dir, exist_ok=True)
        file_name:str = file_path.split('/')[-1]
        if '.tar.gz' in file_name:
            Util.system(f'''tar -xzvf {file_path} -C {unzip_dir}''')
        else:
            Util.system(f'''unzip {file_path} -d {unzip_dir}''')