1 from __future__ import with_statement # enable with
8 _moduleLogger = logging.getLogger(__name__)
11 class FileStorage(object):
13 def __init__(self, path="~/.SornPlayer/"):
15 _moduleLogger.info("init filestorage")
17 self.books_path = os.path.join(self.path, "books.json")
22 if not os.path.isdir(self.path):
23 os.makedirs(self.path)
26 with open(self.books_path, "r") as settingsFile:
27 settings = simplejson.load(settingsFile)
29 _moduleLogger.info("No settings")
32 _moduleLogger.info("Settings were corrupt")
36 self._books = settings["books"]
37 self.selected = settings["selected"]
39 _moduleLogger.info("Falling back to old settings format")
40 self._load_old_settings()
44 "selected": self.selected,
47 with open(self.books_path, "w") as settingsFile:
48 simplejson.dump(settings, settingsFile)
50 def get_selected(self):
51 """returns the currently selected book"""
54 def select_book(self, bookName):
55 """ Sets the book as the currently playing, and adds it to the
56 database if it is not already there"""
57 book_file = os.path.join(self.books_path, bookName)
58 if bookName not in self._books:
59 self._books[bookName] = {
64 self.selected = bookName
66 def set_time(self, chapter, position):
67 """ Sets the current time for the book that is currently selected"""
68 bookInfo = self._books[self.selected]
69 bookInfo["chapter"] = chapter
70 bookInfo["position"] = position
73 """Returns the current saved time for the current selected book"""
74 bookInfo = self._books[self.selected]
75 return bookInfo["chapter"], bookInfo["position"]
77 def _load_old_settings(self):
78 conf = os.path.join(self.path, "current")
82 self.selected = f.readline()
84 books_path = os.path.join(self.path, "books/")
85 for book in os.listdir(books_path):
86 book_file = os.path.join(books_path, book)
87 with open(book_file, 'r') as f:
88 chapter = int(f.readline())
89 position = int(f.readline())