From 319d6d0e697992bd067bfab9d484c8576723d70a Mon Sep 17 00:00:00 2001 From: Tim Sweeney Date: Thu, 5 May 2016 05:50:05 -0400 Subject: [PATCH] Rough draft #2 of part of the bookmarks module. Define a class of Bookmark, for an individual bookmark, and a class of BookmarkSet, as a set of bookmarks. --- rig_remote/bookmarks.py | 48 +++++++++++++++++++++++++-------- rig_remote/exceptions.py | 3 +++ test/test-bad-bookmark-file.csv | 3 +++ test/test_bookmarks.py | 12 ++++++--- 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 test/test-bad-bookmark-file.csv diff --git a/rig_remote/bookmarks.py b/rig_remote/bookmarks.py index 0605d0d..9c19813 100644 --- a/rig_remote/bookmarks.py +++ b/rig_remote/bookmarks.py @@ -28,9 +28,11 @@ import os import logging +from collections import MutableMapping + from rig_remote.constants import CBB_MODES from rig_remote.disk_io import IO -from rig_remote.exceptions import InvalidPathError +from rig_remote.exceptions import InvalidPathError, InvalidBookmark from rig_remote.constants import LEN_BM from rig_remote.constants import BM @@ -60,10 +62,38 @@ def find_existing_bookmarks_file(): ".rig-remote/rig-remote-bookmarks.csv")) return filename +class Bookmark(MutableMapping): + """Bookmark stores an individual bookmark, each instance to contain: + frequency: a string of digits + mode: a string from CBB_MODE, + description: string + lock: string, either 'O' (Open) or 'L' (Locked) + and an id_key, for use in mapping to a UI tree element. + """ + + def __init__(self, id_key, *args, **kwargs): + self.id_key = id_key + self.store = dict() + self.update(dict(*args, **kwargs)) # use the free update to set keys + + def __getitem__(self, key): + return self.store[key] + + def __setitem__(self, key, value): + self.store[key] = value + + def __delitem__(self, key): + del self.store[key] + + def __iter__(self): + return iter(self.store) + + def __len__(self): + return len(self.store) -class Bookmarks(object): - """ Bookmarks is a list of dicts, each dict containing a frequency, mode, description - and lock value. Each instance will have an associated file name stored. +class BookmarkSet(object): + """ BookmarkSet is a list of Bookmarks objects. + Each instance will have an associated file name stored. """ def __init__(self, filename): @@ -81,7 +111,6 @@ def load_from_file(self, silent = False): return count = 0 for line in bookmark_list.row_list: - count += 1 error = False if len(line) < LEN_BM: line.append("O") @@ -90,12 +119,9 @@ def load_from_file(self, silent = False): if line[BM.mode] not in CBB_MODES: error = True if error == True: - if not silent: - tkMessageBox.showerror("Error", "Invalid value in " \ - "Bookmark #%i. " \ - "Skipping..." % count) + raise InvalidBookmark else: - self.bookmarks.append(dict(zip(bookmark_keys,line))) + self.bookmarks.append(Bookmark('', zip(bookmark_keys,line))) def save_to_file(self): bookmark_list = IO() @@ -103,4 +129,4 @@ def save_to_file(self): for entry in self.bookmarks: btuple = (entry['frequency'], entry['mode'], entry['description'], entry['lock']) bookmark_list.row_list.append(btuple) - bookmark_list.csv_save(self.filename, ',') \ No newline at end of file + bookmark_list.csv_save(self.filename, ',') diff --git a/rig_remote/exceptions.py b/rig_remote/exceptions.py index 61a6c59..1d755ef 100644 --- a/rig_remote/exceptions.py +++ b/rig_remote/exceptions.py @@ -28,6 +28,9 @@ class InvalidPathError (NonRetriableError): class UnsupportedScanningConfigError(NonRetriableError): pass +class InvalidBookmark(NonRetriableError): + pass + # retriable custom exceptions class RetriableError (object): diff --git a/test/test-bad-bookmark-file.csv b/test/test-bad-bookmark-file.csv new file mode 100644 index 0000000..b35ae60 --- /dev/null +++ b/test/test-bad-bookmark-file.csv @@ -0,0 +1,3 @@ +,FM,,O +155000400,,,O +155000300,TINCAN,This is a bad bookmark diff --git a/test/test_bookmarks.py b/test/test_bookmarks.py index c6ef73f..d820e2c 100644 --- a/test/test_bookmarks.py +++ b/test/test_bookmarks.py @@ -1,15 +1,21 @@ import pytest import csv -from rig_remote.bookmarks import Bookmarks +from rig_remote.bookmarks import BookmarkSet +from rig_remote.exceptions import InvalidBookmark import logging logger = logging.getLogger(__name__) def test_bookmark_load(): - mybm = Bookmarks("./test/test-bookmark-file.csv") + mybm = BookmarkSet("./test/test-bookmark-file.csv") mybm.load_from_file() +def test_bookmark_load2(): + mybm = BookmarkSet("./test/test-bad-bookmark-file.csv") + with pytest.raises(InvalidBookmark): + mybm.load_from_file() + def test_bookmark_save(): - mybm = Bookmarks("./test/test-bookmark-file.csv") + mybm = BookmarkSet("./test/test-bookmark-file.csv") mybm.load_from_file() mybm.save_to_file()