-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
148 lines (122 loc) · 3.93 KB
/
base.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
137
138
139
140
141
142
143
144
145
146
147
148
import json
class Port:
def __init__(self, cfg=dict()):
self.view = ""
self.width = cfg.get('width', -1)
self.color = cfg.get('color', None)
self.justify = cfg.get('justify', None)
self.segments = []
self.w = 0
def add(self, text, color=None, separator=False, sepWidth=-1, seamless=False):
if color is None:
color = self.color
if seamless:
sepWidth = 0
separator = False
self.addSegment(PortSegment(text, color, separator, sepWidth))
def addSegment(self, seg):
self.w += seg.textWidth()
self.segments.append(seg)
def draw(self):
if not self.segments:
self.view = ''
else:
if self.justify and self.width > 0:
excess = self.width - self.w
if self.justify == 'left':
self.segments[len(self.segments)-1].padRight(excess)
elif self.justify == 'right':
self.segments[0].padLeft(excess)
elif self.justify == 'center':
self.segments[0].padLeft(excess / 2)
self.segments[len(self.segments)-1].padRight(self.width - excess/2)
self.w = self.width
self.view = ','.join(filter(None, [seg.draw(self) for seg in self.segments]))
def availableWidth(self):
return self.w
def useWidth(self, w):
self.w -= w
def clear(self):
self.view = None
self.w = 0
self.segments = []
def getView(self):
if self.view == None:
self.draw()
return self.view
class PortSegment:
def __init__(self, text, color, separator, sepWidth):
self.text = text
self.color = color
self.separator = separator
self.sepWidth = sepWidth
def draw(self, port):
if port.width >= 0:
aw = port.availableWidth()
if aw <= 0:
return ''
if aw < self.textWidth():
self.text = self.text[0:aw]
port.useWidth(self.textWidth())
data = {
'full_text': self.text,
'color': self.color
}
if not self.separator:
data['separator'] = False
if self.sepWidth >= 0:
data['separator_block_width'] = self.sepWidth
return json.dumps(data)
def textWidth(self):
return len(self.text)
def padLeft(self, pad):
self.text = self.text.rjust(len(self.text) + pad)
def padRight(self, pad):
self.text = self.text.ljust(len(self.text) + pad)
class Module:
def __init__(self, name):
self.ports = dict()
self.name = name
self.channels = {}
def register(self, svc):
self.onRegister(svc)
self.update()
def onRegister(self, svc):
return # override
def addPort(self, port, channel):
self.ports[channel] = port
self.onEnableChannel(channel)
def onEnableChannel(self, channel):
self.channels[channel] = True
def update(self):
updated = self.onUpdate()
if updated:
self.onDraw()
return updated
def onUpdate(self):
return False # override
def onDraw(self):
return # override
class PollingModule(Module):
def __init__(self, name):
Module.__init__(self, name)
self.ticks = 0
self.interval = 60
def tick(self):
self.ticks += 1
if self.ticks >= self.interval:
self.ticks = 0
return self.update()
return False
def register(self, svc):
Module.register(self, svc)
svc.registerTicker(self)
class DBusModule(Module):
def __init__(self, name):
Module.__init__(self, name)
self.dbus = None
self.svc = None
def register(self, svc):
self.dbus = svc.getDBus()
self.svc = svc
Module.register(self, svc)