X-Git-Url: http://git.maemo.org/git/?p=nqaap;a=blobdiff_plain;f=src%2Fopt%2FNqa-Audiobook-player%2FFileStorage.py;h=af19c9d0a69360573eef5b42ec3bffefb86c85fd;hp=8536f8aab301dde83233cf7998ff5a927dfc6a23;hb=a08ec0bd2e212e3da7b56b6d02a1920c74535064;hpb=8dc07eeb2f8f7c686fc1fcc815bf53ef4c649e2d diff --git a/src/opt/Nqa-Audiobook-player/FileStorage.py b/src/opt/Nqa-Audiobook-player/FileStorage.py index 8536f8a..af19c9d 100644 --- a/src/opt/Nqa-Audiobook-player/FileStorage.py +++ b/src/opt/Nqa-Audiobook-player/FileStorage.py @@ -13,94 +13,94 @@ _moduleLogger = logging.getLogger(__name__) class FileStorage(object): - def __init__(self, path="~/.SornPlayer/"): - # Setup dir - _moduleLogger.info("init filestorage") - self.path = path - self.books_path = os.path.join(self.path, "books.json") - self.selected = None - self._books = {} - - def load(self): - if not os.path.isdir(self.path): - os.makedirs(self.path) - - try: - with open(self.books_path, "r") as settingsFile: - settings = simplejson.load(settingsFile) - except IOError, e: - _moduleLogger.info("No settings") - settings = {} - except ValueError: - _moduleLogger.info("Settings were corrupt") - settings = {} - - if settings: - self._books = settings["books"] - self.selected = settings["selected"] - else: - _moduleLogger.info("Falling back to old settings format") - self._load_old_settings() - - def save(self): - settings = { - "selected": self.selected, - "books": self._books, - } - with open(self.books_path, "w") as settingsFile: - simplejson.dump(settings, settingsFile) - - def get_selected(self): - """returns the currently selected book""" - return self.selected - - def select_book(self, bookName): - """ Sets the book as the currently playing, and adds it to the - database if it is not already there""" - book_file = os.path.join(self.books_path, bookName) - if bookName not in self._books: - self._books[bookName] = { - "chapter": 0, - "position": 0, - } - - self.selected = bookName - - def set_time(self, chapter, position): - """ Sets the current time for the book that is currently selected""" - bookInfo = self._books[self.selected] - bookInfo["chapter"] = chapter - bookInfo["position"] = position - - def get_time(self): - """Returns the current saved time for the current selected book""" - bookInfo = self._books[self.selected] - return bookInfo["chapter"], bookInfo["position"] - - def _load_old_settings(self): - conf = os.path.join(self.path, "current") - - try: - with open(conf) as f: - self.selected = f.readline() - - books_path = os.path.join(self.path, "books/") - for book in os.listdir(books_path): - book_file = os.path.join(books_path, book) - with open(book_file, 'r') as f: - chapter = int(f.readline()) - position = int(f.readline()) - self._books[book] = { - "chapter": chapter, - "position": position, - } - except IOError, e: - if e.errno == 2: - pass - else: - raise - except OSError, e: - if e.errno == 2: - pass - else: - raise + def __init__(self, path="~/.SornPlayer/"): + # Setup dir + _moduleLogger.info("init filestorage") + self.path = path + self.books_path = os.path.join(self.path, "books.json") + self.selected = None + self._books = {} + + def load(self): + if not os.path.isdir(self.path): + os.makedirs(self.path) + + try: + with open(self.books_path, "r") as settingsFile: + settings = simplejson.load(settingsFile) + except IOError, e: + _moduleLogger.info("No settings") + settings = {} + except ValueError: + _moduleLogger.info("Settings were corrupt") + settings = {} + + if settings: + self._books = settings["books"] + self.selected = settings["selected"] + else: + _moduleLogger.info("Falling back to old settings format") + self._load_old_settings() + + def save(self): + settings = { + "selected": self.selected, + "books": self._books, + } + with open(self.books_path, "w") as settingsFile: + simplejson.dump(settings, settingsFile) + + def get_selected(self): + """returns the currently selected book""" + return self.selected + + def select_book(self, bookName): + """ Sets the book as the currently playing, and adds it to the + database if it is not already there""" + book_file = os.path.join(self.books_path, bookName) + if bookName not in self._books: + self._books[bookName] = { + "chapter": 0, + "position": 0, + } + + self.selected = bookName + + def set_time(self, chapter, position): + """ Sets the current time for the book that is currently selected""" + bookInfo = self._books[self.selected] + bookInfo["chapter"] = chapter + bookInfo["position"] = position + + def get_time(self): + """Returns the current saved time for the current selected book""" + bookInfo = self._books[self.selected] + return bookInfo["chapter"], bookInfo["position"] + + def _load_old_settings(self): + conf = os.path.join(self.path, "current") + + try: + with open(conf) as f: + self.selected = f.readline() + + books_path = os.path.join(self.path, "books/") + for book in os.listdir(books_path): + book_file = os.path.join(books_path, book) + with open(book_file, 'r') as f: + chapter = int(f.readline()) + position = int(f.readline()) + self._books[book] = { + "chapter": chapter, + "position": position, + } + except IOError, e: + if e.errno == 2: + pass + else: + raise + except OSError, e: + if e.errno == 2: + pass + else: + raise