Moving some code around.
This commit is contained in:
parent
1fd53cd60a
commit
a892a09581
@ -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
|
||||
|
40
i3pystatus/core/modules.py
Normal file
40
i3pystatus/core/modules.py
Normal 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)
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user