Moving some code around.

This commit is contained in:
enkore 2013-02-24 20:58:05 +01:00
parent 1fd53cd60a
commit a892a09581
5 changed files with 54 additions and 46 deletions

View File

@ -1,70 +1,34 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys import sys
from threading import Thread
import time
import functools
from .core import io from .core import io
from .core.util import * from .core.util import *
from .core.modules import *
__all__ = [ __all__ = [
"SettingsBase", "SettingsBase",
"ClassFinder",
"Module", "AsyncModule", "IntervalModule", "Module", "AsyncModule", "IntervalModule",
"i3pystatus", "I3statusHandler", "Status", "I3statusHandler",
] ]
class Module(SettingsBase): class Status:
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:
def __init__(self, standalone=False, interval=1, input_stream=sys.stdin): def __init__(self, standalone=False, interval=1, input_stream=sys.stdin):
if standalone: if standalone:
self.io = core.io.StandaloneIO(interval) self.io = core.io.StandaloneIO(interval)
else: else:
self.io = core.io.IOHandler(input_stream) self.io = core.io.IOHandler(input_stream)
self.finder = ClassFinder(Module) self.modules = ModuleList(self, Module)
self.modules = ModuleList(self)
def register(self, module, *args, **kwargs): def register(self, module, *args, **kwargs):
"""Register a new module.""" """Register a new module."""
if module: if module:
self.modules.append(self.finder.instanciate_class_from_module(module, *args, **kwargs)) self.modules.append(module, *args, **kwargs)
def run(self): def run(self):
for j in core.io.JSONIO(self.io).read(): for j in core.io.JSONIO(self.io).read():
for module in self.modules: for module in self.modules:
module.inject(j) module.inject(j)
I3statusHandler = i3pystatus I3statusHandler = Status

View File

@ -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)

View File

@ -12,11 +12,13 @@ __all__ = [
] ]
class ModuleList(collections.UserList): class ModuleList(collections.UserList):
def __init__(self, status_handler): def __init__(self, status_handler, module_base):
self.status_handler = status_handler self.status_handler = status_handler
self.finder = ClassFinder(module_base)
super().__init__() 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) module.registered(self.status_handler)
super().append(module) super().append(module)

View File

@ -9,6 +9,8 @@ import textwrap
import i3pystatus import i3pystatus
import i3pystatus.mail import i3pystatus.mail
from .core.util import ClassFinder
IGNORE = ("__main__", "mkdocs") IGNORE = ("__main__", "mkdocs")
MODULE_FORMAT = """ MODULE_FORMAT = """
{heading} {name} {heading} {name}
@ -132,7 +134,7 @@ with open("template.md", "r") as template:
tpl = template.read() tpl = template.read()
tpl = tpl.replace("!!module_doc!!", generate_doc_for_module(i3pystatus.__path__)) 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> ")) tpl = tpl.replace("!!i3pystatus.mail!!", generate_doc_for_module(i3pystatus.mail.__path__, "###", finder).replace("\n", "\n> "))
print(tpl) print(tpl)