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