auto-reload bookmarks from file

This commit is contained in:
Jakob Ketterl 2021-02-13 01:29:21 +01:00
parent 5e51beac46
commit ae76470612
2 changed files with 28 additions and 5 deletions

View File

@ -1,5 +1,7 @@
from owrx.modes import Modes from owrx.modes import Modes
from datetime import datetime, timezone
import json import json
import os
import logging import logging
@ -55,10 +57,29 @@ class Bandplan(object):
return Bandplan.sharedInstance return Bandplan.sharedInstance
def __init__(self): def __init__(self):
self.bands = self.loadBands() self.bands = []
self.file_modified = None
self.fileList = ["/etc/openwebrx/bands.json", "bands.json"]
def loadBands(self): def _refresh(self):
for file in ["/etc/openwebrx/bands.json", "bands.json"]: modified = self._getFileModifiedTimestamp()
if self.file_modified is None or modified > self.file_modified:
logger.debug("reloading bands from disk due to file modification")
self.bands = self._loadBands()
self.file_modified = modified
def _getFileModifiedTimestamp(self):
timestamp = 0
for file in self.fileList:
try:
timestamp = os.path.getmtime(file)
break
except FileNotFoundError:
pass
return datetime.fromtimestamp(timestamp, timezone.utc)
def _loadBands(self):
for file in self.fileList:
try: try:
f = open(file, "r") f = open(file, "r")
bands_json = json.load(f) bands_json = json.load(f)
@ -75,6 +96,7 @@ class Bandplan(object):
return [] return []
def findBands(self, freq): def findBands(self, freq):
self._refresh()
return [band for band in self.bands if band.inBand(freq)] return [band for band in self.bands if band.inBand(freq)]
def findBand(self, freq): def findBand(self, freq):
@ -85,4 +107,5 @@ class Bandplan(object):
return None return None
def collectDialFrequencies(self, range): def collectDialFrequencies(self, range):
self._refresh()
return [e for b in self.bands for e in b.getDialFrequencies(range)] return [e for b in self.bands for e in b.getDialFrequencies(range)]

View File

@ -44,7 +44,7 @@ class Bookmarks(object):
self.bookmarks = [] self.bookmarks = []
self.fileList = ["/etc/openwebrx/bookmarks.json", "bookmarks.json"] self.fileList = ["/etc/openwebrx/bookmarks.json", "bookmarks.json"]
def refresh(self): def _refresh(self):
modified = self._getFileModifiedTimestamp() modified = self._getFileModifiedTimestamp()
if self.file_modified is None or modified > self.file_modified: if self.file_modified is None or modified > self.file_modified:
logger.debug("reloading bookmarks from disk due to file modification") logger.debug("reloading bookmarks from disk due to file modification")
@ -79,6 +79,6 @@ class Bookmarks(object):
return [] return []
def getBookmarks(self, range): def getBookmarks(self, range):
self.refresh() self._refresh()
(lo, hi) = range (lo, hi) = range
return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi] return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi]