-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bafr.py
136 lines (98 loc) · 3.3 KB
/
Bafr.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import subprocess
import sys
class Cmd:
def __init__(self, cmd, flags):
self.cmd = cmd
self.flags = flags
self.error_code = 0
self.error_str = None
self.result = None
def exec(self):
result = subprocess.run([self.cmd] + self.flags, capture_output=True, text=True)
if result.returncode > 0:
self._set_error_code(result.returncode)
self._set_error_str(result.stderr)
return False
self.result = result.stdout
return True
def _set_error_code(self, returncode):
self.error_code = returncode
def _set_error_str(self, error_str):
self.error_str = error_str
def get_error_code(self):
return self.error_code
def get_error_str(self):
return self.error_str
def get_result(self):
return self.result
class Bafr:
def __init__(self, mod_name=None):
if not mod_name:
mod_name = "bafr"
self.mod_name = mod_name
self.mod_params = {}
self.cmd = None
self.flags = []
self.parser = None
self.raw = None
self.parsed_data = None
self.attributes = {}
self.signal_num = None
####################################
self.error_code = 0
self.error_str = None
def set_cmd(self, cmd):
self.cmd = cmd
def set_parser(self, parser):
self.parser = parser
def set_cmd_flags(self, flags):
self.flags = flags
def run(self):
cmd = Cmd(self.cmd, self.flags)
if not cmd.exec():
self.error_code = cmd.get_error_code()
self.error_str = cmd.get_error_str()
return False
self._set_raw(cmd.get_result())
if not self.parser:
self.parsed_data = self.raw
else:
self.parsed_data = self.parser(self.raw)
return True
def _set_raw(self, raw_data):
self.raw = raw_data
def get_parsed_data(self):
return self.parsed_data
def get_error_code(self):
return self.error_code
def get_error_str(self):
return self.error_str
def set_attr(self, attr, val):
self.attributes[attr] = val
def get_attr(self, attr):
if attr not in self.attributes: return None
return self.attributes[attr]
def echo(self, end=""):
print(self.get_parsed_data(), end=end)
def eecho(self, end=""):
print(f"[bafr.{self.mod_name}.ERR]: {self.get_error_str()}", file=sys.stderr, end=end)
def exit(self):
sys.exit(self.get_error_code())
def set_signal_num(self, num):
self.signal_num = num
def get_signal_num(self):
return self.signal_num
def signal_i3blocks(self):
if not self.get_signal_num(): return False
flags = [f"-{self.get_signal_num()}", "i3blocks"]
cmd = Cmd("pkill", flags)
if not cmd.exec():
self.error_code = cmd.get_error_code()
self.error_str = cmd.get_error_str()
return False
return True
def set_params(params):
self.mod_params = params
def get_param(param_key):
if param_key in self.mod_params:
return self.mod_params[param_key]