From bed060f1c5c4e2265c90ac9dbb8db4e81a530e0a Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 6 Jul 2022 18:14:25 -0400 Subject: [PATCH] Refactor utils to directory This patch moves the utils.py to utils/__init__.py and fuzzyclock.py to utils and separates the ring_buffer to it's own file in utils --- aprsd/plugins/time.py | 5 ++- aprsd/{utils.py => utils/__init__.py} | 55 ++------------------------- aprsd/{ => utils}/fuzzyclock.py | 0 aprsd/utils/ring_buffer.py | 37 ++++++++++++++++++ tests/plugins/test_time.py | 2 +- 5 files changed, 45 insertions(+), 54 deletions(-) rename aprsd/{utils.py => utils/__init__.py} (71%) rename aprsd/{ => utils}/fuzzyclock.py (100%) create mode 100644 aprsd/utils/ring_buffer.py diff --git a/aprsd/plugins/time.py b/aprsd/plugins/time.py index 8cfdd83..92033c9 100644 --- a/aprsd/plugins/time.py +++ b/aprsd/plugins/time.py @@ -5,7 +5,8 @@ import time from opencage.geocoder import OpenCageGeocode import pytz -from aprsd import fuzzyclock, plugin, plugin_utils, trace +from aprsd import plugin, plugin_utils, trace +from aprsd.utils import fuzzy LOG = logging.getLogger("APRSD") @@ -32,7 +33,7 @@ class TimePlugin(plugin.APRSDRegexCommandPluginBase): local_short_str = local_t.strftime("%H:%M %Z") local_hour = local_t.strftime("%H") local_min = local_t.strftime("%M") - cur_time = fuzzyclock.fuzzy(int(local_hour), int(local_min), 1) + cur_time = fuzzy(int(local_hour), int(local_min), 1) reply = "{} ({})".format( cur_time, diff --git a/aprsd/utils.py b/aprsd/utils/__init__.py similarity index 71% rename from aprsd/utils.py rename to aprsd/utils/__init__.py index 124443c..ae5eef4 100644 --- a/aprsd/utils.py +++ b/aprsd/utils/__init__.py @@ -2,25 +2,17 @@ import collections import errno -import functools import os import re -import threading import update_checker import aprsd - -def synchronized(wrapped): - lock = threading.Lock() - - @functools.wraps(wrapped) - def _wrap(*args, **kwargs): - with lock: - return wrapped(*args, **kwargs) - - return _wrap +from .fuzzyclock import fuzzy +# Make these available by anyone importing +# aprsd.utils +from .ring_buffer import RingBuffer def env(*vars, **kwargs): @@ -129,42 +121,3 @@ def parse_delta_str(s): else: m = re.match(r"(?P\d+):(?P\d+):(?P\d[\.\d+]*)", s) return {key: float(val) for key, val in m.groupdict().items()} - - -class RingBuffer: - """class that implements a not-yet-full buffer""" - - def __init__(self, size_max): - self.max = size_max - self.data = [] - - class __Full: - """class that implements a full buffer""" - - def append(self, x): - """Append an element overwriting the oldest one.""" - self.data[self.cur] = x - self.cur = (self.cur + 1) % self.max - - def get(self): - """return list of elements in correct order""" - return self.data[self.cur :] + self.data[: self.cur] - - def __len__(self): - return len(self.data) - - def append(self, x): - """append an element at the end of the buffer""" - - self.data.append(x) - if len(self.data) == self.max: - self.cur = 0 - # Permanently change self's class from non-full to full - self.__class__ = self.__Full - - def get(self): - """Return a list of elements from the oldest to the newest.""" - return self.data - - def __len__(self): - return len(self.data) diff --git a/aprsd/fuzzyclock.py b/aprsd/utils/fuzzyclock.py similarity index 100% rename from aprsd/fuzzyclock.py rename to aprsd/utils/fuzzyclock.py diff --git a/aprsd/utils/ring_buffer.py b/aprsd/utils/ring_buffer.py new file mode 100644 index 0000000..4029ce4 --- /dev/null +++ b/aprsd/utils/ring_buffer.py @@ -0,0 +1,37 @@ +class RingBuffer: + """class that implements a not-yet-full buffer""" + + def __init__(self, size_max): + self.max = size_max + self.data = [] + + class __Full: + """class that implements a full buffer""" + + def append(self, x): + """Append an element overwriting the oldest one.""" + self.data[self.cur] = x + self.cur = (self.cur + 1) % self.max + + def get(self): + """return list of elements in correct order""" + return self.data[self.cur :] + self.data[: self.cur] + + def __len__(self): + return len(self.data) + + def append(self, x): + """append an element at the end of the buffer""" + + self.data.append(x) + if len(self.data) == self.max: + self.cur = 0 + # Permanently change self's class from non-full to full + self.__class__ = self.__Full + + def get(self): + """Return a list of elements from the oldest to the newest.""" + return self.data + + def __len__(self): + return len(self.data) diff --git a/tests/plugins/test_time.py b/tests/plugins/test_time.py index 52616a4..befba45 100644 --- a/tests/plugins/test_time.py +++ b/tests/plugins/test_time.py @@ -2,8 +2,8 @@ from unittest import mock import pytz -from aprsd.fuzzyclock import fuzzy from aprsd.plugins import time as time_plugin +from aprsd.utils import fuzzy from .. import fake, test_plugin