This repository was archived by the owner on Feb 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormat.py
More file actions
executable file
·90 lines (74 loc) · 2.78 KB
/
Format.py
File metadata and controls
executable file
·90 lines (74 loc) · 2.78 KB
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
def FileNameSafer(Cfg = None):
import re
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Name' : '',
'MaxLength' : 50,
'ForceReplace': []
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Name' : ''
}
try:
_ = Config['Name']
for Rule in Config['ForceReplace']:
if len(Rule) == 2: _ = _.replace(Rule[0], Rule[1])
Response['Name'] = re.sub(r'\s+', ' ', re.sub(r'[\\/:*?\"<>|\n]', ' ', _)).lstrip()[:Config['MaxLength']].rstrip()
except Exception as errorMsg:
Response['ErrorCode'] = 50000
Response['ErrorMsg'] = f'Fail to safety given name, {str(errorMsg).lower().rstrip(".")}'
return Response
def StorageUnitProcesser(Cfg = None):
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Size' : -1,
'Unit' : 'B',
'TargetDecimal': 2,
'TargetUnit' : 'AUTO',
'TargetFormat' : '{Size} {Unit}'
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Size' : ''
}
Unit_List = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
Unit_Weight = {'B': 0, 'KB': 1, 'MB': 2, 'GB': 3, 'TB': 4, 'PB': 5, 'EB': 6, 'ZB': 7, 'YB': 8}
if Config['Size'] < 0:
Response['ErrorCode'] = 50001
Response['ErrorMsg'] = f'Value error, size must be positive'
return Response
if Config['Unit'].upper() not in Unit_List:
Response['ErrorCode'] = 50002
Response['ErrorMsg'] = f'Units error, unit must in B, KB, MB, GB, TB, PB, EB, ZB or YB'
return Response
if Config['TargetUnit'].upper() == 'AUTO':
_InputExponent = _OutputExponent = Unit_Weight[Config['Unit'].upper()]
_Size = Config['Size']
if _Size >= 1:
while _Size >= 1024 and _OutputExponent < 8:
_OutputExponent += 1
_Size /= 1024
else:
while _Size < 1 and _OutputExponent > 0:
_OutputExponent -= 1
_Size *= 1024
_Size = '{:.{}f}'.format(_Size, Config['TargetDecimal'])
_Unit = Unit_List[_OutputExponent]
else:
_InputExponent = Unit_Weight[Config['Unit'].upper()]
_OutputExponent = Unit_Weight[Config['TargetUnit'].upper()]
_Size = '{:.{}f}'.format(Config['Size'] * 1.00000000001 * (1024 ** (_InputExponent - _OutputExponent)), Config['TargetDecimal'])
_Unit = Config['TargetUnit']
Response['Size'] = Config['TargetFormat'].replace('{Size}', str(_Size)).replace('{Unit}', _Unit)
return Response