forked from tcecspectator/mytcecgui
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommoner.py
110 lines (87 loc) · 3.05 KB
/
commoner.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2021-01-05
"""
Common functions
"""
from argparse import ArgumentParser
import errno
import logging
import os
def create_group(parser: ArgumentParser, group: str) -> callable:
group = parser.add_argument_group(group)
return group.add_argument
def default_int(value: int or str, default: int=None, origin: str=None, log: bool=False) -> int or None:
"""Convert a value to an int, on exception, return a default value
"""
if isinstance(value, int):
return value
if value is None:
return default
try:
value = int(float(value))
except (TypeError, ValueError):
if log:
logging.warning({
'status': 'default_int__cannot_convert', 'value': value, 'default': default, 'origin': origin})
value = default
return value
def makedirs_safe(folder: str) -> bool:
"""Create a folder recursively + handle errors
:return: True if the folder has been created or existed already, False otherwise
"""
if not folder:
return True
try:
os.makedirs(folder)
return True
except Exception as e:
if isinstance(e, OSError) and e.errno == errno.EEXIST:
return True
logging.error({'status': 'makedirs_safe__error', 'error': e, 'folder': folder})
return False
pinfo = print
def read_text_safe(filename: str, locked: bool=False, want_bytes: bool=False) -> str or bytes or None:
"""Read the content of a file and convert it to utf-8
"""
if not filename or not os.path.isfile(filename):
return None
try:
open_func = open # locked_open if locked else open
with open_func(filename, 'rb') as file:
data = file.read()
return data if want_bytes else data.decode('utf-8-sig')
except OSError as e:
logging.error({'status': 'read_text_safe__error', 'error': e, 'filename': filename})
return None
def utc_time() -> float:
"""Utility to return the utc timestamp
"""
return datetime.now(tz=timezone.utc).timestamp()
def write_text_safe(
filename: str,
data: str or bytes,
mode: str='wb',
locked: bool=False,
convert_newlines: bool=False, # convert \n to \r\n on windows
) -> bool:
"""Save text or binary to a file
"""
if not filename or '?' in filename:
return False
# windows support
if convert_newlines and isinstance(data, str) and system() == 'Windows' and '\r\n' not in data:
data = data.replace('\n', '\r\n')
# save
path = os.path.dirname(filename)
if not makedirs_safe(path):
return False
try:
open_func = open # locked_open if locked else open
with open_func(filename, mode) as file:
if data:
file.write(data.encode('utf-8') if isinstance(data, str) else data)
return True
except OSError as e:
logging.error({'status': 'write_text_safe__error', 'error': e, 'filename': filename})
return False