diff --git a/i3pystatus/__init__.py b/i3pystatus/__init__.py index effbb19..2375f5f 100644 --- a/i3pystatus/__init__.py +++ b/i3pystatus/__init__.py @@ -1,70 +1,34 @@ #!/usr/bin/env python import sys -from threading import Thread -import time -import functools from .core import io from .core.util import * +from .core.modules import * __all__ = [ "SettingsBase", - "ClassFinder", "Module", "AsyncModule", "IntervalModule", - "i3pystatus", "I3statusHandler", + "Status", "I3statusHandler", ] -class Module(SettingsBase): - output = None - - def registered(self, status_handler): - """Called when this module is registered with a status handler""" - - def inject(self, json): - if self.output: - if "name" not in self.output: - self.output["name"] = self.__name__ - json.insert(0, self.output) - -class AsyncModule(Module): - def registered(self, status_handler): - self.thread = Thread(target=self.mainloop) - self.thread.daemon = True - self.thread.start() - - def mainloop(self): - """This is run in a separate daemon-thread""" - -class IntervalModule(AsyncModule): - interval = 5 # seconds - - def run(self): - """Called every self.interval seconds""" - - def mainloop(self): - while True: - self.run() - time.sleep(self.interval) - -class i3pystatus: +class Status: def __init__(self, standalone=False, interval=1, input_stream=sys.stdin): if standalone: self.io = core.io.StandaloneIO(interval) else: self.io = core.io.IOHandler(input_stream) - self.finder = ClassFinder(Module) - self.modules = ModuleList(self) + self.modules = ModuleList(self, Module) def register(self, module, *args, **kwargs): """Register a new module.""" if module: - self.modules.append(self.finder.instanciate_class_from_module(module, *args, **kwargs)) + self.modules.append(module, *args, **kwargs) def run(self): for j in core.io.JSONIO(self.io).read(): for module in self.modules: module.inject(j) -I3statusHandler = i3pystatus +I3statusHandler = Status diff --git a/i3pystatus/core/io.py b/i3pystatus/core/io.py index ef311b1..e5b4144 100644 --- a/i3pystatus/core/io.py +++ b/i3pystatus/core/io.py @@ -101,4 +101,4 @@ class JSONIO: j = json.loads(line) yield j - self.io.write_line(prefix + json.dumps(j)) \ No newline at end of file + self.io.write_line(prefix + json.dumps(j)) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py new file mode 100644 index 0000000..e8bc319 --- /dev/null +++ b/i3pystatus/core/modules.py @@ -0,0 +1,40 @@ +from threading import Thread +import time + +from .util import SettingsBase + +__all__ = [ + "Module", "AsyncModule", "IntervalModule", +] + +class Module(SettingsBase): + output = None + + def registered(self, status_handler): + """Called when this module is registered with a status handler""" + + def inject(self, json): + if self.output: + if "name" not in self.output: + self.output["name"] = self.__name__ + json.insert(0, self.output) + +class AsyncModule(Module): + def registered(self, status_handler): + self.thread = Thread(target=self.mainloop) + self.thread.daemon = True + self.thread.start() + + def mainloop(self): + """This is run in a separate daemon-thread""" + +class IntervalModule(AsyncModule): + interval = 5 # seconds + + def run(self): + """Called every self.interval seconds""" + + def mainloop(self): + while True: + self.run() + time.sleep(self.interval) diff --git a/i3pystatus/core/util.py b/i3pystatus/core/util.py index 8a10b13..3635ef2 100644 --- a/i3pystatus/core/util.py +++ b/i3pystatus/core/util.py @@ -12,11 +12,13 @@ __all__ = [ ] class ModuleList(collections.UserList): - def __init__(self, status_handler): + def __init__(self, status_handler, module_base): self.status_handler = status_handler + self.finder = ClassFinder(module_base) super().__init__() - def append(self, module): + def append(self, module, *args, **kwargs): + module = self.finder.instanciate_class_from_module(module, *args, **kwargs) module.registered(self.status_handler) super().append(module) diff --git a/i3pystatus/mkdocs.py b/i3pystatus/mkdocs.py index d1a943f..a720002 100755 --- a/i3pystatus/mkdocs.py +++ b/i3pystatus/mkdocs.py @@ -9,6 +9,8 @@ import textwrap import i3pystatus import i3pystatus.mail +from .core.util import ClassFinder + IGNORE = ("__main__", "mkdocs") MODULE_FORMAT = """ {heading} {name} @@ -132,7 +134,7 @@ with open("template.md", "r") as template: tpl = template.read() tpl = tpl.replace("!!module_doc!!", generate_doc_for_module(i3pystatus.__path__)) - finder = i3pystatus.ClassFinder(baseclass=i3pystatus.mail.Backend, exclude=[i3pystatus.mail.Backend]) + finder = i3pystatus.ClassFinder(baseclass=i3pystatus.mail.Backend) tpl = tpl.replace("!!i3pystatus.mail!!", generate_doc_for_module(i3pystatus.mail.__path__, "###", finder).replace("\n", "\n> ")) print(tpl)