-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
61 lines (50 loc) · 2.23 KB
/
util.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
# Copyright (C) 2022 luckytyphlosion
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import codecs
import struct
import itertools
def min_sec_ms_to_time(minutes, seconds, milliseconds):
return minutes * 100000 + seconds * 1000 + milliseconds
def utf_16_hex(s):
s_as_utf_16_bytes = s.encode(encoding="utf-16")
if s_as_utf_16_bytes[:2] == codecs.BOM_UTF16_LE:
struct_format = "<H"
s_as_utf_16_bytes_no_bom = s_as_utf_16_bytes[2:]
elif s_as_utf_16_bytes[:2] == codecs.BOM_UTF16_BE:
struct_format = ">H"
s_as_utf_16_bytes_no_bom = s_as_utf_16_bytes[2:]
else:
struct_format = "<H"
s_as_utf_16_bytes_no_bom = s_as_utf_16_bytes
return "".join(f"{num[0]:04x}" for num in struct.iter_unpack(struct_format, s_as_utf_16_bytes_no_bom))
def grouper(iterable, n, fillvalue=None):
"Collect data into non-overlapping fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
def arg_default_or_validate_from_choices(arg, *choices_and_error_message):
default = choices_and_error_message[0]
choices = choices_and_error_message[:-1]
error_message = choices_and_error_message[-1]
if arg is None:
arg = default
elif arg not in choices:
raise RuntimeError(error_message.format(arg))
return arg
def arg_default_select(arg, default):
return arg if arg is not None else default
def join_conditional_modifier(*modifiers):
return " ".join(modifier for modifier in modifiers if modifier is not None)