2021-02-13 00:10:36 +00:00
|
|
|
from datetime import datetime, timezone
|
2021-02-13 23:41:03 +00:00
|
|
|
from owrx.config.core import CoreConfig
|
2019-09-27 22:53:58 +00:00
|
|
|
import json
|
2021-02-13 00:10:36 +00:00
|
|
|
import os
|
2019-09-27 22:53:58 +00:00
|
|
|
|
2019-12-15 16:44:31 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-09-27 22:53:58 +00:00
|
|
|
|
|
|
|
class Bookmark(object):
|
|
|
|
def __init__(self, j):
|
|
|
|
self.name = j["name"]
|
|
|
|
self.frequency = j["frequency"]
|
|
|
|
self.modulation = j["modulation"]
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def getFrequency(self):
|
|
|
|
return self.frequency
|
|
|
|
|
|
|
|
def getModulation(self):
|
|
|
|
return self.modulation
|
|
|
|
|
|
|
|
def __dict__(self):
|
|
|
|
return {
|
|
|
|
"name": self.getName(),
|
|
|
|
"frequency": self.getFrequency(),
|
|
|
|
"modulation": self.getModulation(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Bookmarks(object):
|
|
|
|
sharedInstance = None
|
2019-11-23 00:12:21 +00:00
|
|
|
|
2019-09-27 22:53:58 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
|
|
|
if Bookmarks.sharedInstance is None:
|
|
|
|
Bookmarks.sharedInstance = Bookmarks()
|
|
|
|
return Bookmarks.sharedInstance
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-02-13 00:10:36 +00:00
|
|
|
self.file_modified = None
|
|
|
|
self.bookmarks = []
|
2021-02-13 23:41:03 +00:00
|
|
|
self.fileList = [Bookmarks._getBookmarksFile(), "/etc/openwebrx/bookmarks.json", "bookmarks.json"]
|
2019-12-08 20:00:01 +00:00
|
|
|
|
2021-02-13 00:29:21 +00:00
|
|
|
def _refresh(self):
|
2021-02-13 00:10:36 +00:00
|
|
|
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:
|
2019-12-08 20:00:01 +00:00
|
|
|
try:
|
|
|
|
f = open(file, "r")
|
|
|
|
bookmarks_json = json.load(f)
|
|
|
|
f.close()
|
|
|
|
return [Bookmark(d) for d in bookmarks_json]
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2019-12-15 16:44:31 +00:00
|
|
|
except json.JSONDecodeError:
|
2020-02-01 20:37:43 +00:00
|
|
|
logger.exception("error while parsing bookmarks file %s", file)
|
|
|
|
return []
|
|
|
|
except Exception:
|
|
|
|
logger.exception("error while processing bookmarks from %s", file)
|
2019-12-15 16:44:31 +00:00
|
|
|
return []
|
2019-12-08 20:00:01 +00:00
|
|
|
return []
|
|
|
|
|
2021-02-13 17:35:15 +00:00
|
|
|
def getBookmarks(self, range=None):
|
2021-02-13 00:29:21 +00:00
|
|
|
self._refresh()
|
2021-02-13 17:35:15 +00:00
|
|
|
if range is None:
|
|
|
|
return self.bookmarks
|
|
|
|
else:
|
|
|
|
(lo, hi) = range
|
|
|
|
return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi]
|
2021-02-13 23:41:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _getBookmarksFile():
|
|
|
|
coreConfig = CoreConfig()
|
|
|
|
return "{data_directory}/bookmarks.json".format(data_directory=coreConfig.get_data_directory())
|
|
|
|
|
|
|
|
def store(self):
|
2021-02-16 16:17:09 +00:00
|
|
|
# don't write directly to file to avoid corruption on exceptions
|
|
|
|
jsonContent = json.dumps([b.__dict__() for b in self.bookmarks], indent=4)
|
2021-02-13 23:41:03 +00:00
|
|
|
with open(Bookmarks._getBookmarksFile(), "w") as file:
|
2021-02-16 16:17:09 +00:00
|
|
|
file.write(jsonContent)
|
2021-02-13 23:41:03 +00:00
|
|
|
self.file_modified = self._getFileModifiedTimestamp()
|
2021-02-14 15:21:09 +00:00
|
|
|
|
|
|
|
def addBookmark(self, bookmark: Bookmark):
|
|
|
|
self.bookmarks.append(bookmark)
|
2021-02-14 15:51:16 +00:00
|
|
|
|
|
|
|
def removeBookmark(self, bookmark: Bookmark):
|
|
|
|
if bookmark not in self.bookmarks:
|
|
|
|
return
|
|
|
|
self.bookmarks.remove(bookmark)
|