Completed
Push — master ( 70f06b...a3f76f )
by Amin
16s queued 12s
created

ffmpeg_streaming._utiles.get_os()   A

Complexity

Conditions 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
"""
2
ffmpeg_streaming.streams
3
~~~~~~~~~~~~
4
5
Useful methods
6
7
8
:copyright: (c) 2020 by Amin Yazdanpanah.
9
:website: https://www.aminyazdanpanah.com
10
:email: [email protected]
11
:license: MIT, see LICENSE for more details.
12
"""
13
import logging
14
import os
15
import re
16
import time
17
import warnings
18
from sys import platform
19
20
21
def get_path_info(path):
22
    """
23
    @TODO: add documentation
24
    """
25
    dirname = os.path.dirname(path)
26
    name = str(os.path.basename(path).rsplit('.', 1)[0])
27
28
    return dirname, name
29
30
31
def mkdir(dirname: str) -> None:
32
    """
33
    @TODO: add documentation
34
    """
35
    try:
36
        os.makedirs(dirname)
37
    except OSError as exc:
38
        logging.info(exc)
39
40
41
def rm(path: str) -> None:
42
    """
43
    @TODO: add documentation
44
    """
45
    try:
46
        os.remove(path)
47
    except OSError as exc:
48
        logging.info(exc)
49
50
51
def clean_args(args: list) -> list:
52
    """
53
    @TODO: add documentation
54
    """
55
    clean_args_ = []
56
    for arg in args:
57
        if " " in arg:
58
            arg = '"' + arg + '"'
59
        clean_args_.append(arg.replace("\\", "/").replace("__COLON__", ":"))
60
61
    return clean_args_
62
63
64
def convert_to_sec(time):
65
    """
66
    @TODO: add documentation
67
    """
68
    h, m, s = time.split(":")
69
    return int(h) * 3600 + int(m) * 60 + int(s)
70
71
72
def get_time(key, string, default):
73
    """
74
    @TODO: add documentation
75
    """
76
    time = re.search('(?<={})\w+:\w+:\w+'.format(key), string)
77
    return convert_to_sec(time.group(0)) if time else default
78
79
80
def time_left(start_time, unit, total):
81
    """
82
    @TODO: add documentation
83
    """
84
    if unit != 0:
85
        diff_time = time.time() - start_time
86
        return total * diff_time / unit - diff_time
87
    else:
88
        return 0
89
90
91
def deprecated(func):
92
    """
93
    @TODO: add documentation
94
    """
95
    def deprecated_fun(*args, **kwargs):
96
        warnings.warn('The {} method is deprecated and will be removed in a future release'.format(func.__name__)
97
                      , DeprecationWarning, stacklevel=2)
98
        return func(*args, **kwargs)
99
    return deprecated_fun
100
101
102
def get_os():
103
    """
104
    @TODO: add documentation
105
    """
106
    if platform in ["linux", "linux2"]:
107
        return 'linux'
108
    elif platform == "darwin":
109
        return 'os_x'
110
    elif platform in ["win32", "Windows"]:
111
        return 'windows'
112
    else:
113
        return 'unknown'
114
115
116
def cnv_options_to_args(options: dict):
117
    """
118
    @TODO: add documentation
119
    """
120
    args = []
121
    for k, v in options.items():
122
        args.append('-{}'.format(k))
123
        if v is not None:
124
            args.append('{}'.format(v))
125
126
    return args
127