openwebrx-clone/owrx/bookmarks.py

63 lines
1.6 KiB
Python
Raw Normal View History

2019-09-27 22:53:58 +00:00
import json
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):
self.bookmarks = self.loadBookmarks()
def loadBookmarks(self):
for file in ["/etc/openwebrx/bookmarks.json", "bookmarks.json"]:
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:
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 []
return []
2019-09-27 22:53:58 +00:00
def getBookmarks(self, range):
(lo, hi) = range
return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi]