From 0d51634ec28b5344ccaf279bf16246b339a483ec Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 20 Oct 2021 14:39:12 -0400 Subject: [PATCH] Enable configuring where to save the objectstore data This patch adds a new config entry aprsd.save_location which is the directory used to store and load the objectstore data. --- aprsd/config.py | 12 +++++++----- aprsd/main.py | 9 ++++----- aprsd/objectstore.py | 20 +++++++++++++++++++- aprsd/packets.py | 6 ++++++ 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/aprsd/config.py b/aprsd/config.py index 6053a21..ebcc5cb 100644 --- a/aprsd/config.py +++ b/aprsd/config.py @@ -10,6 +10,12 @@ import yaml from aprsd import utils +home = str(Path.home()) +DEFAULT_CONFIG_DIR = f"{home}/.config/aprsd/" +DEFAULT_SAVE_FILE = f"{home}/.config/aprsd/aprsd.p" +DEFAULT_CONFIG_FILE = f"{home}/.config/aprsd/aprsd.yml" + + LOG_LEVELS = { "CRITICAL": logging.CRITICAL, "ERROR": logging.ERROR, @@ -72,6 +78,7 @@ DEFAULT_CONFIG_DICT = { "logfile": "/tmp/aprsd.log", "logformat": DEFAULT_LOG_FORMAT, "dateformat": DEFAULT_DATE_FORMAT, + "save_location": DEFAULT_CONFIG_DIR, "trace": False, "enabled_plugins": CORE_MESSAGE_PLUGINS, "units": "imperial", @@ -129,11 +136,6 @@ DEFAULT_CONFIG_DICT = { }, } -home = str(Path.home()) -DEFAULT_CONFIG_DIR = f"{home}/.config/aprsd/" -DEFAULT_SAVE_FILE = f"{home}/.config/aprsd/aprsd.p" -DEFAULT_CONFIG_FILE = f"{home}/.config/aprsd/aprsd.yml" - class Config(collections.UserDict): def _get(self, d, keys, default=None): diff --git a/aprsd/main.py b/aprsd/main.py index 91e5bdd..2adf64c 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -479,15 +479,14 @@ def server( if flush: LOG.debug("Deleting saved MsgTrack.") messaging.MsgTrack().flush() + packets.PacketList(config=config) + packets.WatchList(config=config) else: # Try and load saved MsgTrack list LOG.debug("Loading saved MsgTrack object.") messaging.MsgTrack().load() - packets.WatchList().load() - packets.SeenList().load() - - packets.PacketList(config=config) - packets.WatchList(config=config) + packets.WatchList(config=config).load() + packets.SeenList(config=config).load() rx_thread = threads.APRSDRXThread( msg_queues=threads.msg_queues, diff --git a/aprsd/objectstore.py b/aprsd/objectstore.py index 35ccab0..33e0934 100644 --- a/aprsd/objectstore.py +++ b/aprsd/objectstore.py @@ -35,9 +35,27 @@ class ObjectStoreMixin: with self.lock: return self.data[id] + def _init_store(self): + sl = self._save_location() + if not os.path.exists(sl): + LOG.warning(f"Save location {sl} doesn't exist") + try: + os.makedirs(sl) + except Exception as ex: + LOG.exception(ex) + + def _save_location(self): + save_location = self.config.get("aprsd.save_location", None) + if not save_location: + save_location = aprsd_config.DEFAULT_CONFIG_DIR + return save_location + def _save_filename(self): + save_location = self._save_location() + + LOG.debug(f"{self.__class__.__name__}::Using save location {save_location}") return "{}/{}.p".format( - aprsd_config.DEFAULT_CONFIG_DIR, + save_location, self.__class__.__name__.lower(), ) diff --git a/aprsd/packets.py b/aprsd/packets.py index a9d4d9c..fd0f683 100644 --- a/aprsd/packets.py +++ b/aprsd/packets.py @@ -97,6 +97,7 @@ class WatchList(objectstore.ObjectStoreMixin): ring_size, ), } + self._init_store() def is_enabled(self): if self.config and "watch_list" in self.config["aprsd"]: @@ -165,6 +166,11 @@ class SeenList(objectstore.ObjectStoreMixin): cls.data = {} return cls._instance + def __init__(self, config=None): + if config: + self.config = config + self._init_store() + def update_seen(self, packet): callsign = None if "fromcall" in packet: