X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2FFileStorage.py;h=ec5644eb412aca0cfb2be8351fc4dd0fb1583ebf;hb=refs%2Fheads%2Fmaster;hp=af19c9d0a69360573eef5b42ec3bffefb86c85fd;hpb=502333554888c174d18232692e820d28b2c1b372;p=nqaap diff --git a/src/FileStorage.py b/src/FileStorage.py index af19c9d..ec5644e 100644 --- a/src/FileStorage.py +++ b/src/FileStorage.py @@ -2,6 +2,7 @@ from __future__ import with_statement # enable with import os import simplejson +import codecs import logging @@ -26,7 +27,7 @@ class FileStorage(object): os.makedirs(self.path) try: - with open(self.books_path, "r") as settingsFile: + with codecs.open(self.books_path, "r", "utf-8") as settingsFile: settings = simplejson.load(settingsFile) except IOError, e: _moduleLogger.info("No settings") @@ -47,7 +48,7 @@ class FileStorage(object): "selected": self.selected, "books": self._books, } - with open(self.books_path, "w") as settingsFile: + with codecs.open(self.books_path, "w", "utf-8") as settingsFile: simplejson.dump(settings, settingsFile) def get_selected(self): @@ -57,7 +58,6 @@ class FileStorage(object): 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, @@ -88,8 +88,13 @@ class FileStorage(object): 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()) + try: + chapter = int(f.readline()) + position = int(f.readline()) + except ValueError: + _moduleLogger.exception("Trouble loading old settings from %s" % book_file) + chapter = 0 + position = 0 self._books[book] = { "chapter": chapter, "position": position,