X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=lcm%2Fosm_common%2Fmsglocal.py;h=238ee35f0e0dd2eeef202e4bc26e44d44decda9f;hb=ae501920e1c0e03c8571bece610dd5518e6e86b9;hp=a380e618da6565f5844a5020acc2080120cb025a;hpb=c887cc2fdd02c898ecd1f08d6927043027fd07eb;p=osm%2FRO.git diff --git a/lcm/osm_common/msglocal.py b/lcm/osm_common/msglocal.py index a380e618..238ee35f 100644 --- a/lcm/osm_common/msglocal.py +++ b/lcm/osm_common/msglocal.py @@ -1,18 +1,24 @@ +import logging import os import yaml import asyncio from msgbase import MsgBase, MsgException +__author__ = "Alfonso Tierno " -class msgLocal(MsgBase): - def __init__(self): +class MsgLocal(MsgBase): + + def __init__(self, logger_name='msg'): + self.logger = logging.getLogger(logger_name) self.path = None # create a different file for each topic self.files = {} def connect(self, config): try: + if "logger_name" in config: + self.logger = logging.getLogger(config["logger_name"]) self.path = config["path"] if not self.path.endswith("/"): self.path += "/" @@ -48,9 +54,16 @@ class msgLocal(MsgBase): def read(self, topic): try: + msg = "" if topic not in self.files: - self.files[topic] = open(self.path + topic, "r+") - msg = self.files[topic].read() + self.files[topic] = open(self.path + topic, "a+") + # ignore previous content + for line in self.files[topic]: + if not line.endswith("\n"): + msg = line + msg += self.files[topic].readline() + if not msg.endswith("\n"): + return None msg_dict = yaml.load(msg) assert len(msg_dict) == 1 for k, v in msg_dict.items(): @@ -60,16 +73,18 @@ class msgLocal(MsgBase): async def aioread(self, topic, loop=None): try: + msg = "" if not loop: loop = asyncio.get_event_loop() if topic not in self.files: - self.files[topic] = open(self.path + topic, "r+") + self.files[topic] = open(self.path + topic, "a+") # ignore previous content - while self.files[topic].read(): - pass + for line in self.files[topic]: + if not line.endswith("\n"): + msg = line while True: - msg = self.files[topic].read() - if msg: + msg += self.files[topic].readline() + if msg.endswith("\n"): break await asyncio.sleep(2, loop=loop) msg_dict = yaml.load(msg)