Replaced conditionals with polymorphism
This commit is contained in:
parent
561e60efee
commit
8ec1972a3e
@ -5,18 +5,26 @@ import json
|
|||||||
import urllib.request, urllib.error, urllib.parse
|
import urllib.request, urllib.error, urllib.parse
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
class Module:
|
class BaseModule:
|
||||||
output = None
|
output = None
|
||||||
async = False
|
|
||||||
|
|
||||||
def registered(self, status_handler):
|
def registered(self, status_handler):
|
||||||
"""Called when this module is registered with a status handler"""
|
"""Called when this module is registered with a status handler"""
|
||||||
|
|
||||||
def tick(self):
|
def tick(self):
|
||||||
"""Only called if async is False. Called once per tick"""
|
"""Called once per tick"""
|
||||||
|
|
||||||
|
class Module(BaseModule):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AsyncModule(BaseModule):
|
||||||
|
def registered(self, status_handler):
|
||||||
|
self.thread = Thread(target=self.mainloop)
|
||||||
|
self.thread.daemon = True
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
def mainloop(self):
|
def mainloop(self):
|
||||||
"""This is run in a separate daemon-thread if async is True"""
|
"""This is run in a separate daemon-thread"""
|
||||||
|
|
||||||
class I3statusHandler:
|
class I3statusHandler:
|
||||||
modules = []
|
modules = []
|
||||||
@ -28,6 +36,7 @@ class I3statusHandler:
|
|||||||
"""Register a new module."""
|
"""Register a new module."""
|
||||||
|
|
||||||
self.modules.append(module)
|
self.modules.append(module)
|
||||||
|
module.registered(self)
|
||||||
|
|
||||||
def print_line(self, message):
|
def print_line(self, message):
|
||||||
"""Unbuffered printing to stdout."""
|
"""Unbuffered printing to stdout."""
|
||||||
@ -53,13 +62,6 @@ class I3statusHandler:
|
|||||||
self.print_line(self.read_line())
|
self.print_line(self.read_line())
|
||||||
self.print_line(self.read_line())
|
self.print_line(self.read_line())
|
||||||
|
|
||||||
# Start threads for asynchronous modules
|
|
||||||
for module in self.modules:
|
|
||||||
if module.async:
|
|
||||||
module.thread = Thread(target=module.mainloop)
|
|
||||||
module.thread.daemon = True
|
|
||||||
module.thread.start()
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line, prefix = self.read_line(), ""
|
line, prefix = self.read_line(), ""
|
||||||
|
|
||||||
@ -70,7 +72,6 @@ class I3statusHandler:
|
|||||||
j = json.loads(line)
|
j = json.loads(line)
|
||||||
|
|
||||||
for module in self.modules:
|
for module in self.modules:
|
||||||
if not module.async:
|
|
||||||
module.tick()
|
module.tick()
|
||||||
|
|
||||||
output = module.output
|
output = module.output
|
||||||
|
@ -9,17 +9,14 @@ import re
|
|||||||
import http.cookiejar
|
import http.cookiejar
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
from i3pystatus import Module
|
from i3pystatus import AsyncModule
|
||||||
|
|
||||||
class ModsDeChecker(Module):
|
class ModsDeChecker(AsyncModule):
|
||||||
"""
|
"""
|
||||||
This class returns i3status parsable output of the number of
|
This class returns i3status parsable output of the number of
|
||||||
unread posts in any bookmark in the mods.de forums.
|
unread posts in any bookmark in the mods.de forums.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async = True
|
|
||||||
output = None
|
|
||||||
|
|
||||||
login_url = "http://login.mods.de/"
|
login_url = "http://login.mods.de/"
|
||||||
bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php"
|
bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php"
|
||||||
opener = None
|
opener = None
|
||||||
|
@ -13,17 +13,14 @@ import json
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from i3pystatus import Module
|
from i3pystatus import AsyncModule
|
||||||
|
|
||||||
class ThunderbirdMailChecker(Module):
|
class ThunderbirdMailChecker(AsyncModule):
|
||||||
"""
|
"""
|
||||||
This class listens for dbus signals emitted by
|
This class listens for dbus signals emitted by
|
||||||
the dbus-sender extension for thunderbird.
|
the dbus-sender extension for thunderbird.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async = True
|
|
||||||
output = None
|
|
||||||
|
|
||||||
settings = {
|
settings = {
|
||||||
"format": "%d new email"
|
"format": "%d new email"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user