mirror of
https://github.com/craigerl/aprsd.git
synced 2024-11-25 17:38:44 -05:00
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
This commit is contained in:
parent
1c052a63c0
commit
6cb8d11f39
@ -5,7 +5,8 @@ import time
|
|||||||
from opencage.geocoder import OpenCageGeocode
|
from opencage.geocoder import OpenCageGeocode
|
||||||
import pytz
|
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")
|
LOG = logging.getLogger("APRSD")
|
||||||
@ -32,7 +33,7 @@ class TimePlugin(plugin.APRSDRegexCommandPluginBase):
|
|||||||
local_short_str = local_t.strftime("%H:%M %Z")
|
local_short_str = local_t.strftime("%H:%M %Z")
|
||||||
local_hour = local_t.strftime("%H")
|
local_hour = local_t.strftime("%H")
|
||||||
local_min = local_t.strftime("%M")
|
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(
|
reply = "{} ({})".format(
|
||||||
cur_time,
|
cur_time,
|
||||||
|
@ -2,25 +2,17 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import errno
|
import errno
|
||||||
import functools
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import threading
|
|
||||||
|
|
||||||
import update_checker
|
import update_checker
|
||||||
|
|
||||||
import aprsd
|
import aprsd
|
||||||
|
|
||||||
|
from .fuzzyclock import fuzzy
|
||||||
def synchronized(wrapped):
|
# Make these available by anyone importing
|
||||||
lock = threading.Lock()
|
# aprsd.utils
|
||||||
|
from .ring_buffer import RingBuffer
|
||||||
@functools.wraps(wrapped)
|
|
||||||
def _wrap(*args, **kwargs):
|
|
||||||
with lock:
|
|
||||||
return wrapped(*args, **kwargs)
|
|
||||||
|
|
||||||
return _wrap
|
|
||||||
|
|
||||||
|
|
||||||
def env(*vars, **kwargs):
|
def env(*vars, **kwargs):
|
||||||
@ -129,42 +121,3 @@ def parse_delta_str(s):
|
|||||||
else:
|
else:
|
||||||
m = re.match(r"(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)", s)
|
m = re.match(r"(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)", s)
|
||||||
return {key: float(val) for key, val in m.groupdict().items()}
|
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)
|
|
37
aprsd/utils/ring_buffer.py
Normal file
37
aprsd/utils/ring_buffer.py
Normal file
@ -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)
|
@ -2,8 +2,8 @@ from unittest import mock
|
|||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from aprsd.fuzzyclock import fuzzy
|
|
||||||
from aprsd.plugins import time as time_plugin
|
from aprsd.plugins import time as time_plugin
|
||||||
|
from aprsd.utils import fuzzy
|
||||||
|
|
||||||
from .. import fake, test_plugin
|
from .. import fake, test_plugin
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user