diff options
author | Laurie Clark-Michalek <bluepeppers@archlinux.us> | 2010-11-19 16:17:55 +0000 |
---|---|---|
committer | Laurie Clark-Michalek <bluepeppers@archlinux.us> | 2010-11-19 16:17:55 +0000 |
commit | 2509e25926e289ad97febc0e724a9eb4d9006fbe (patch) | |
tree | f16aba2925e7db660b9a6c2173b90947c2271f49 /archey.new | |
parent | 661ddb91c49a689e458938d4a1b546ede1f42f44 (diff) |
Added @shell_command class decorator to make dealing with Display classes.
See help(shell_command) for more info
Diffstat (limited to 'archey.new')
-rw-r--r-- | archey.new | 43 |
1 files changed, 40 insertions, 3 deletions
@@ -367,9 +367,45 @@ class Terminal(BaseDisplay): class User(BaseDisplay): key = 'User' env = 'USER' - -#class Packages(): -# def __init__(self): + +def shell_command(klass): + """ + A class decorated with @shell_command will be treated as a class that runs a command, and then parses the output. + + It should have two string members, "command", the command that will be run, and "key", the key for the display. + It should also implement one method, process_output, which should take two arguments, stdout, and stderr, and return + a value to be displayed. + """ + def get_value(self): + cmd = subprocess.Popen(self.command.split(), + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = cmd.communicate() + return self.process_output(stdout.decode('ascii'), stderr.decode('ascii')) + + if not all(hasattr(klass, name) for name in ('command', 'key', 'process_output')): + raise ArcheyException("Classes decorated with @shell_command must have " + "a key and command attributes, and the process_output method") + else: + klass.get_value = get_value + + return klass + +@shell_command +class Packages(BaseDisplay): + key = 'Packman packages' + command = 'pacman -Q' + + def process_output(self, stdout, stderr): + #Return nothing if pacman returns errors + if stderr: + return None + + no_of_packages = len(stdout.split('\n')) + + return str(no_of_packages) + #class CPU(): # def __init__(self): @@ -389,6 +425,7 @@ def main(): out.append(Terminal()) out.append(Uptime()) out.append(WindowManager()) + out.append(Packages()) #out.append(DesktopEnvirornment()) out.output() |