Adding support for multiple bookmark files from bookmarks.d folder, etc.
This commit is contained in:
parent
eee5b68d6b
commit
95e53c6897
|
|
@ -1 +1,2 @@
|
||||||
/etc/openwebrx/openwebrx.conf.d
|
/etc/openwebrx/openwebrx.conf.d
|
||||||
|
/etc/openwebrx/bookmarks.d
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from owrx.config.core import CoreConfig
|
from owrx.config.core import CoreConfig
|
||||||
import json
|
import json
|
||||||
|
import os.path
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
@ -9,10 +10,11 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Bookmark(object):
|
class Bookmark(object):
|
||||||
def __init__(self, j):
|
def __init__(self, j, srcFile: str = None):
|
||||||
self.name = j["name"]
|
self.name = j["name"]
|
||||||
self.frequency = j["frequency"]
|
self.frequency = j["frequency"]
|
||||||
self.modulation = j["modulation"]
|
self.modulation = j["modulation"]
|
||||||
|
self.srcFile = srcFile
|
||||||
|
|
||||||
def getName(self):
|
def getName(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
@ -23,6 +25,9 @@ class Bookmark(object):
|
||||||
def getModulation(self):
|
def getModulation(self):
|
||||||
return self.modulation
|
return self.modulation
|
||||||
|
|
||||||
|
def getSrcFile(self):
|
||||||
|
return self.srcFile
|
||||||
|
|
||||||
def __dict__(self):
|
def __dict__(self):
|
||||||
return {
|
return {
|
||||||
"name": self.getName(),
|
"name": self.getName(),
|
||||||
|
|
@ -61,7 +66,20 @@ class Bookmarks(object):
|
||||||
self.file_modified = None
|
self.file_modified = None
|
||||||
self.bookmarks = []
|
self.bookmarks = []
|
||||||
self.subscriptions = []
|
self.subscriptions = []
|
||||||
self.fileList = [Bookmarks._getBookmarksFile(), "/etc/openwebrx/bookmarks.json", "bookmarks.json"]
|
# Known bookmark files, starting with the main file
|
||||||
|
self.fileList = [
|
||||||
|
Bookmarks._getBookmarksFile(),
|
||||||
|
"bookmarks.json",
|
||||||
|
"/etc/openwebrx/bookmarks.json",
|
||||||
|
]
|
||||||
|
# Find additional bookmark files in the bookmarks.d folder
|
||||||
|
try:
|
||||||
|
self.fileList += [ file
|
||||||
|
for file in os.listdir("/etc/openwebrx/bookmarks.d")
|
||||||
|
if os.path.isfile(file) and file.endswith(".json")
|
||||||
|
]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def _refresh(self):
|
def _refresh(self):
|
||||||
modified = self._getFileModifiedTimestamp()
|
modified = self._getFileModifiedTimestamp()
|
||||||
|
|
@ -74,29 +92,31 @@ class Bookmarks(object):
|
||||||
timestamp = 0
|
timestamp = 0
|
||||||
for file in self.fileList:
|
for file in self.fileList:
|
||||||
try:
|
try:
|
||||||
timestamp = os.path.getmtime(file)
|
timestamp = max(timestamp, os.path.getmtime(file))
|
||||||
break
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
return datetime.fromtimestamp(timestamp, timezone.utc)
|
return datetime.fromtimestamp(timestamp, timezone.utc)
|
||||||
|
|
||||||
def _loadBookmarks(self):
|
def _loadBookmarks(self):
|
||||||
|
mainFile = Bookmarks._getBookmarksFile()
|
||||||
|
result = []
|
||||||
|
# Collect bookmarks from all files in the result
|
||||||
for file in self.fileList:
|
for file in self.fileList:
|
||||||
|
# Main file bookmarks will not have srcFile set
|
||||||
|
srcFile = file if file != mainFile else None
|
||||||
try:
|
try:
|
||||||
with open(file, "r") as f:
|
with open(file, "r") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
if content:
|
if content:
|
||||||
bookmarks_json = json.loads(content)
|
bookmarks_json = json.loads(content)
|
||||||
return [Bookmark(d) for d in bookmarks_json]
|
result += [Bookmark(d, srcFile) for d in bookmarks_json]
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
logger.exception("error while parsing bookmarks file %s", file)
|
logger.exception("error while parsing bookmarks file %s", file)
|
||||||
return []
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("error while processing bookmarks from %s", file)
|
logger.exception("error while processing bookmarks from %s", file)
|
||||||
return []
|
return result
|
||||||
return []
|
|
||||||
|
|
||||||
def getBookmarks(self, range=None):
|
def getBookmarks(self, range=None):
|
||||||
self._refresh()
|
self._refresh()
|
||||||
|
|
@ -112,8 +132,12 @@ class Bookmarks(object):
|
||||||
return "{data_directory}/bookmarks.json".format(data_directory=coreConfig.get_data_directory())
|
return "{data_directory}/bookmarks.json".format(data_directory=coreConfig.get_data_directory())
|
||||||
|
|
||||||
def store(self):
|
def store(self):
|
||||||
# don't write directly to file to avoid corruption on exceptions
|
# Don't write directly to file to avoid corruption on exceptions
|
||||||
jsonContent = json.dumps([b.__dict__() for b in self.bookmarks], indent=4)
|
# Only save main file bookmarks, i.e. ones with no srcFle
|
||||||
|
jsonContent = json.dumps(
|
||||||
|
[b.__dict__() for b in self.bookmarks if b.getSrcFile() is None],
|
||||||
|
indent=4
|
||||||
|
)
|
||||||
with open(Bookmarks._getBookmarksFile(), "w") as file:
|
with open(Bookmarks._getBookmarksFile(), "w") as file:
|
||||||
file.write(jsonContent)
|
file.write(jsonContent)
|
||||||
self.file_modified = self._getFileModifiedTimestamp()
|
self.file_modified = self._getFileModifiedTimestamp()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue