-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
68 lines (60 loc) · 1.77 KB
/
manager.py
File metadata and controls
68 lines (60 loc) · 1.77 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
#!/usr/bin/env python3
"""
Helpful code wrapped up in functions for clarity.
"""
import logging
import signal
import sys
import os
from conf import LOG_DIR
def user_input_handler() -> list:
""" Function to handle input from user and other threads."""
input_list = [line for line in sys.argv[1:]]
return input_list
def signal_handler(sig: signal.signal, frame) -> int:
""" Handle extern signals in order to set up coordenation."""
if sig in (signal.SIGTERM, signal.SIGINT):
LOG.fatal("Terminated by user (likely by Ctrl+C).")
try:
process.emitter_thread.socket.close()
process.listener_thread.socket.close()
except:
pass
finally:
sys.exit(0)
return 1
elif sig == signal.SIGUSR1: # halt
LOG.warning("Suspended by user (SIGUSR1).")
signal.pause()
return 0
elif sig == signal.SIGUSR2: # continue
LOG.warning("Awaken by user (SIGUSR2).")""
return 0
elif sig == signal.SIGPIPE:
if __debug__:
print("SIGPIPE")
return 0
else:
return 1
def ger_or_default(obj: object, index: int, default=0):
""" Similar to dict.get() but for iterables. """
try:
iter(obj)
return obj[index] if -len(obj) <= index < len(obj) else default
except TypeError:
return default
def log_setup() -> logging.Logger:
""" Creates log directory if not available. """
try:
os.mkdir(LOG_DIR)
except OSError:
pass
# prepping log
logging.basicConfig(
filename=os.path.join(
LOG_DIR,
"8-10000-1" + ger_or_default(user_input_handler(), 0, "") + ".log"
),
level=logging.DEBUG
)
return logging.getLogger(__name__)