Skip to content

Commit

Permalink
Rough draft #2 of part of the bookmarks module.
Browse files Browse the repository at this point in the history
Define a class of Bookmark, for an individual bookmark, and a class of BookmarkSet, as a set of bookmarks.
  • Loading branch information
MaineTim committed May 5, 2016
1 parent 5f30e8d commit 319d6d0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
48 changes: 37 additions & 11 deletions rig_remote/bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -90,17 +119,14 @@ 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()

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, ',')
bookmark_list.csv_save(self.filename, ',')
3 changes: 3 additions & 0 deletions rig_remote/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class InvalidPathError (NonRetriableError):
class UnsupportedScanningConfigError(NonRetriableError):
pass

class InvalidBookmark(NonRetriableError):
pass

# retriable custom exceptions

class RetriableError (object):
Expand Down
3 changes: 3 additions & 0 deletions test/test-bad-bookmark-file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,FM,,O
155000400,,,O
155000300,TINCAN,This is a bad bookmark
12 changes: 9 additions & 3 deletions test/test_bookmarks.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 319d6d0

Please sign in to comment.