-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.py
98 lines (84 loc) · 1.5 KB
/
common.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
"""
Michael duPont - [email protected]
static.py - Shared global methods
"""
# stdlib
import json
import logging
from typing import List
# module
import config as cfg
IDENT_CHARS = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
]
logger = logging.getLogger()
logger.setLevel(cfg.log_level)
if cfg.log_file is not None:
log_file = logging.FileHandler(cfg.log_file)
log_file.setLevel(cfg.log_level)
logger.addHandler(log_file)
def ident_to_station(idents: List[int]) -> str:
"""
Converts 'ident' ints to station string
"""
return "".join([IDENT_CHARS[num] for num in idents])
def station_to_ident(station: str) -> List[int]:
"""
Converts station string to 'ident' ints
"""
ret = []
for char in station:
if char.isalpha():
ret.append(ord(char) - 65)
elif char.isdigit():
ret.append(ord(char) - 22)
return ret
SESSION_PATH = cfg.LOC / "session.json"
def load_session() -> dict:
"""
Returns available session dict
"""
try:
return json.load(SESSION_PATH.open())
except FileNotFoundError:
return {}
def save_session(data: dict):
"""
Save the session dict to disk
"""
json.dump(data, SESSION_PATH.open("w"))