Bump to 0.8.12
[nqaap] / src / FileStorage.py
index af19c9d..ec5644e 100644 (file)
@@ -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,