implement auto-reloading for bookmarks

This commit is contained in:
Jakob Ketterl 2021-02-13 01:10:36 +01:00
parent 8acfb8c1cf
commit 5e51beac46

View File

@ -1,4 +1,6 @@
from datetime import datetime, timezone
import json import json
import os
import logging import logging
@ -38,10 +40,29 @@ class Bookmarks(object):
return Bookmarks.sharedInstance return Bookmarks.sharedInstance
def __init__(self): def __init__(self):
self.bookmarks = self.loadBookmarks() self.file_modified = None
self.bookmarks = []
self.fileList = ["/etc/openwebrx/bookmarks.json", "bookmarks.json"]
def loadBookmarks(self): def refresh(self):
for file in ["/etc/openwebrx/bookmarks.json", "bookmarks.json"]: modified = self._getFileModifiedTimestamp()
if self.file_modified is None or modified > self.file_modified:
logger.debug("reloading bookmarks from disk due to file modification")
self.bookmarks = self._loadBookmarks()
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 _loadBookmarks(self):
for file in self.fileList:
try: try:
f = open(file, "r") f = open(file, "r")
bookmarks_json = json.load(f) bookmarks_json = json.load(f)
@ -58,5 +79,6 @@ class Bookmarks(object):
return [] return []
def getBookmarks(self, range): def getBookmarks(self, range):
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]