Storing resume information in utf-8 for non-english filenames
[nqaap] / src / FileStorage.py
1 from __future__ import with_statement   # enable with
2
3 import os
4 import simplejson
5 import codecs
6 import logging
7
8
9 _moduleLogger = logging.getLogger(__name__)
10
11
12 # @todo Add bookmarks
13
14
15 class FileStorage(object):
16
17         def __init__(self, path="~/.SornPlayer/"):
18                 # Setup dir
19                 _moduleLogger.info("init filestorage")
20                 self.path = path
21                 self.books_path = os.path.join(self.path, "books.json")
22                 self.selected = None
23                 self._books = {}
24
25         def load(self):
26                 if not os.path.isdir(self.path):
27                         os.makedirs(self.path)
28
29                 try:
30                         with codecs.open(self.books_path, "r", "utf-8") as settingsFile:
31                                 settings = simplejson.load(settingsFile)
32                 except IOError, e:
33                         _moduleLogger.info("No settings")
34                         settings = {}
35                 except ValueError:
36                         _moduleLogger.info("Settings were corrupt")
37                         settings = {}
38
39                 if settings:
40                         self._books = settings["books"]
41                         self.selected = settings["selected"]
42                 else:
43                         _moduleLogger.info("Falling back to old settings format")
44                         self._load_old_settings()
45
46         def save(self):
47                 settings = {
48                         "selected": self.selected,
49                         "books": self._books,
50                 }
51                 with codecs.open(self.books_path, "w", "utf-8") as settingsFile:
52                         simplejson.dump(settings, settingsFile)
53
54         def get_selected(self):
55                 """returns the currently selected book"""
56                 return self.selected
57
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                 book_file = os.path.join(self.books_path, bookName)
62                 if bookName not in self._books:
63                         self._books[bookName] = {
64                                 "chapter": 0,
65                                 "position": 0,
66                         }
67
68                 self.selected = bookName
69
70         def set_time(self, chapter, position):
71                 """ Sets the current time for the book that is currently selected"""
72                 bookInfo = self._books[self.selected]
73                 bookInfo["chapter"] = chapter
74                 bookInfo["position"] = position
75
76         def get_time(self):
77                 """Returns the current saved time for the current selected book"""
78                 bookInfo = self._books[self.selected]
79                 return bookInfo["chapter"], bookInfo["position"]
80
81         def _load_old_settings(self):
82                 conf = os.path.join(self.path, "current")
83
84                 try:
85                         with open(conf) as f:
86                                 self.selected = f.readline()
87
88                         books_path = os.path.join(self.path, "books/")
89                         for book in os.listdir(books_path):
90                                 book_file = os.path.join(books_path, book)
91                                 with open(book_file, 'r') as f:
92                                         try:
93                                                 chapter = int(f.readline())
94                                                 position = int(f.readline())
95                                         except ValueError:
96                                                 _moduleLogger.exception("Trouble loading old settings from %s" % book_file)
97                                                 chapter = 0
98                                                 position = 0
99                                 self._books[book] = {
100                                         "chapter": chapter,
101                                         "position": position,
102                                 }
103                 except IOError, e:
104                         if e.errno == 2:
105                                 pass
106                         else:
107                                 raise
108                 except OSError, e:
109                         if e.errno == 2:
110                                 pass
111                         else:
112                                 raise