# -*- python -*- from common import * class MyCG(Entry): def __init__(self, title, command, cg_class, *args, priority = None, **kwargs): self.title = title self.cg_class = cg_class rule = '::'.join(cg_class.split('::')[2:]) self.start_command = [command, '-R', rule] if priority is not None: self.start_command.extend(['-p', str(priority)]) self.stop_command = [command, '-R', rule, '-x'] Entry.__init__(self, *args, **kwargs) def action(self, col, button, x, y): proc = subprocess.Popen(['cg-query', '-c', '?'], stdout = subprocess.PIPE) output = [] while True: chunk = proc.stdout.read(128) if not chunk: break output.append(chunk) proc.stdout.close() proc.wait() output = b''.join(output).decode('utf-8', 'replace') crtcs = output.split('\n') output = [] for crtc in crtcs: if not crtc: continue proc = subprocess.Popen(['cg-query', '-c', crtc], stdout = subprocess.PIPE) while True: chunk = proc.stdout.read(128) if not chunk: break output.append(chunk) proc.stdout.close() proc.wait() output = b''.join(output).decode('utf-8', 'replace') active = ('\n Class: %s\n' % self.cg_class) in output del output print(active) print(repr(self.stop_command if active else self.start_command)) subprocess.Popen(self.stop_command if active else self.start_command).wait() def function(self): return self.title class MyCGNegative(MyCG): def __init__(self, *args, title = 'Negative', rule = 'xpybar', priority = None, **kwargs): MyCG.__init__(self, title, 'cg-negative', 'cg-tools::cg-negative::' + rule, *args, priority = priority, **kwargs)