1 from __future__ import with_statement # enable with
9 _moduleLogger = logging.getLogger(__name__)
15 class FileStorage(object):
17 def __init__(self, path="~/.SornPlayer/"):
19 _moduleLogger.info("init filestorage")
21 self.books_path = os.path.join(self.path, "books.json")
26 if not os.path.isdir(self.path):
27 os.makedirs(self.path)
30 with codecs.open(self.books_path, "r", "utf-8") as settingsFile:
31 settings = simplejson.load(settingsFile)
33 _moduleLogger.info("No settings")
36 _moduleLogger.info("Settings were corrupt")
40 self._books = settings["books"]
41 self.selected = settings["selected"]
43 _moduleLogger.info("Falling back to old settings format")
44 self._load_old_settings()
48 "selected": self.selected,
51 with codecs.open(self.books_path, "w", "utf-8") as settingsFile:
52 simplejson.dump(settings, settingsFile)
54 def get_selected(self):
55 """returns the currently selected book"""
58 def select_book(self, bookName):
59 """ Sets the book as the currently playing, and adds it to the
60 database if it is not already there"""
61 if bookName not in self._books:
62 self._books[bookName] = {
67 self.selected = bookName
69 def set_time(self, chapter, position):
70 """ Sets the current time for the book that is currently selected"""
71 bookInfo = self._books[self.selected]
72 bookInfo["chapter"] = chapter
73 bookInfo["position"] = position
76 """Returns the current saved time for the current selected book"""
77 bookInfo = self._books[self.selected]
78 return bookInfo["chapter"], bookInfo["position"]
80 def _load_old_settings(self):
81 conf = os.path.join(self.path, "current")
85 self.selected = f.readline()
87 books_path = os.path.join(self.path, "books/")
88 for book in os.listdir(books_path):
89 book_file = os.path.join(books_path, book)
90 with open(book_file, 'r') as f:
92 chapter = int(f.readline())
93 position = int(f.readline())
95 _moduleLogger.exception("Trouble loading old settings from %s" % book_file)
100 "position": position,