summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-11-18 08:21:55 +0300
committerDevaev Maxim <[email protected]>2018-11-18 08:21:55 +0300
commit1e44482a849b3f605c95251fc59ff571e7d25a3b (patch)
tree7cf7defc9d21fcda62ba2e4c1845093ed55f6439
parentf38dc912d3069862efe5afa33a563a576c9ddfa2 (diff)
refactoring
-rw-r--r--kvmd/apps/kvmd/__init__.py8
-rw-r--r--kvmd/apps/kvmd/logreader.py48
-rw-r--r--kvmd/apps/kvmd/server.py12
-rw-r--r--kvmd/logging.py47
4 files changed, 56 insertions, 59 deletions
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index 519adf0b..e50ad1bd 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -4,10 +4,10 @@ import aiohttp
from ...application import init
from ...logging import get_logger
-from ...logging import Log
from ... import gpio
+from .logreader import LogReader
from .info import InfoManager
from .hid import Hid
from .atx import Atx
@@ -23,14 +23,14 @@ def main() -> None:
loop = asyncio.get_event_loop()
http_session = aiohttp.ClientSession(loop=loop)
- log = Log(loop)
-
info_manager = InfoManager(
meta_path=str(config["info"]["meta"]),
extras_path=str(config["info"]["extras"]),
loop=loop,
)
+ log_reader = LogReader(loop)
+
hid = Hid(
reset=int(config["hid"]["pinout"]["reset"]),
device_path=str(config["hid"]["device"]),
@@ -83,8 +83,8 @@ def main() -> None:
)
Server(
- log=log,
info_manager=info_manager,
+ log_reader=log_reader,
hid=hid,
atx=atx,
diff --git a/kvmd/apps/kvmd/logreader.py b/kvmd/apps/kvmd/logreader.py
new file mode 100644
index 00000000..5ade240e
--- /dev/null
+++ b/kvmd/apps/kvmd/logreader.py
@@ -0,0 +1,48 @@
+import re
+import asyncio
+import time
+
+from typing import Dict
+from typing import AsyncGenerator
+
+import systemd.journal
+
+
+# =====
+class LogReader:
+ def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
+ self.__loop = loop
+
+ async def poll_log(self, seek: int, follow: bool) -> AsyncGenerator[Dict, None]:
+ reader = systemd.journal.Reader()
+ reader.this_boot()
+ reader.this_machine()
+ reader.log_level(systemd.journal.LOG_DEBUG)
+
+ services = set(
+ service
+ for service in systemd.journal.Reader().query_unique("_SYSTEMD_UNIT")
+ if re.match(r"kvmd(-\w+)?\.service", service)
+ ).union(["kvmd.service"])
+
+ for service in services:
+ reader.add_match(_SYSTEMD_UNIT=service)
+ if seek > 0:
+ reader.seek_realtime(float(time.time() - seek))
+
+ for entry in reader:
+ yield self.__entry_to_record(entry)
+
+ while follow:
+ entry = reader.get_next()
+ if entry:
+ yield self.__entry_to_record(entry)
+ else:
+ await asyncio.sleep(1)
+
+ def __entry_to_record(self, entry: Dict) -> Dict[str, Dict]:
+ return {
+ "dt": entry["__REALTIME_TIMESTAMP"],
+ "service": entry["_SYSTEMD_UNIT"],
+ "msg": entry["MESSAGE"].rstrip(),
+ }
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py
index 5a7e33fe..9c6933d4 100644
--- a/kvmd/apps/kvmd/server.py
+++ b/kvmd/apps/kvmd/server.py
@@ -14,21 +14,17 @@ import aiohttp.web
import setproctitle
from ...logging import get_logger
-from ...logging import Log
from ...aioregion import RegionIsBusyError
from ... import __version__
from .info import InfoManager
-
+from .logreader import LogReader
from .hid import Hid
-
from .atx import Atx
-
from .msd import MsdOperationError
from .msd import MassStorageDevice
-
from .streamer import Streamer
@@ -107,8 +103,8 @@ def _wrap_exceptions_for_web(msg: str) -> Callable:
class Server: # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments
self,
- log: Log,
info_manager: InfoManager,
+ log_reader: LogReader,
hid: Hid,
atx: Atx,
@@ -122,8 +118,8 @@ class Server: # pylint: disable=too-many-instance-attributes
loop: asyncio.AbstractEventLoop,
) -> None:
- self.__log = log
self.__info_manager = info_manager
+ self.__log_reader = log_reader
self.__hid = hid
self.__atx = atx
@@ -202,7 +198,7 @@ class Server: # pylint: disable=too-many-instance-attributes
follow = _valid_bool("follow", request.query.get("follow", "false"))
response = aiohttp.web.StreamResponse(status=200, reason="OK", headers={"Content-Type": "text/plain"})
await response.prepare(request)
- async for record in self.__log.poll_log(seek, follow):
+ async for record in self.__log_reader.poll_log(seek, follow):
await response.write(("[%s %s] --- %s" % (
record["dt"].strftime("%Y-%m-%d %H:%M:%S"),
record["service"],
diff --git a/kvmd/logging.py b/kvmd/logging.py
index 70f6f2df..838e13d1 100644
--- a/kvmd/logging.py
+++ b/kvmd/logging.py
@@ -1,13 +1,5 @@
import sys
-import re
-import asyncio
import logging
-import time
-
-from typing import Dict
-from typing import AsyncGenerator
-
-import systemd.journal
# =====
@@ -21,42 +13,3 @@ def get_logger(depth: int=1) -> logging.Logger:
break
name = frames[depth].f_globals["__name__"]
return logging.getLogger(name)
-
-
-class Log:
- def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
- self.__loop = loop
-
- async def poll_log(self, seek: int, follow: bool) -> AsyncGenerator[Dict, None]:
- reader = systemd.journal.Reader()
- reader.this_boot()
- reader.this_machine()
- reader.log_level(systemd.journal.LOG_DEBUG)
-
- services = set(
- service
- for service in systemd.journal.Reader().query_unique("_SYSTEMD_UNIT")
- if re.match(r"kvmd(-\w+)?\.service", service)
- ).union(["kvmd.service"])
-
- for service in services:
- reader.add_match(_SYSTEMD_UNIT=service)
- if seek > 0:
- reader.seek_realtime(float(time.time() - seek))
-
- for entry in reader:
- yield self.__entry_to_record(entry)
-
- while follow:
- entry = reader.get_next()
- if entry:
- yield self.__entry_to_record(entry)
- else:
- await asyncio.sleep(1)
-
- def __entry_to_record(self, entry: Dict) -> Dict[str, Dict]:
- return {
- "dt": entry["__REALTIME_TIMESTAMP"],
- "service": entry["_SYSTEMD_UNIT"],
- "msg": entry["MESSAGE"].rstrip(),
- }