This commit is contained in:
Kovid Goyal 2013-10-12 16:34:19 +05:30
commit 9038754c71

View File

@ -682,18 +682,22 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
return None return None
def _metadata_in_cache(self, uuid, ext, lastmod): def _metadata_in_cache(self, uuid, ext, lastmod):
if lastmod == 'None': try:
return None
from calibre.utils.date import parse_date, now from calibre.utils.date import parse_date, now
key = uuid+ext key = uuid+ext
if isinstance(lastmod, unicode): if isinstance(lastmod, unicode):
if lastmod == 'None':
return None
lastmod = parse_date(lastmod) lastmod = parse_date(lastmod)
if key in self.known_uuids and self.known_uuids[key]['book'].last_modified == lastmod: if key in self.known_uuids and self.known_uuids[key]['book'].last_modified == lastmod:
self.known_uuids[key]['last_used'] = now() self.known_uuids[key]['last_used'] = now()
return self.known_uuids[key]['book'].deepcopy() return self.known_uuids[key]['book'].deepcopy()
except:
traceback.print_exc()
return None return None
def _metadata_already_on_device(self, book): def _metadata_already_on_device(self, book):
try:
v = self.known_metadata.get(book.lpath, None) v = self.known_metadata.get(book.lpath, None)
if v is not None: if v is not None:
# Metadata is the same if the uuids match, if the last_modified dates # Metadata is the same if the uuids match, if the last_modified dates
@ -706,6 +710,8 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
if bool(v_thumb) != bool(b_thumb): if bool(v_thumb) != bool(b_thumb):
return False return False
return not v_thumb or v_thumb[1] == b_thumb[1] return not v_thumb or v_thumb[1] == b_thumb[1]
except:
traceback.print_exc()
return False return False
def _uuid_already_on_device(self, uuid, ext): def _uuid_already_on_device(self, uuid, ext):
@ -730,9 +736,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
'_metadata_cache.json') '_metadata_cache.json')
self.known_uuids = defaultdict(dict) self.known_uuids = defaultdict(dict)
self.known_metadata = {} self.known_metadata = {}
try:
if os.path.exists(cache_file_name): if os.path.exists(cache_file_name):
with open(cache_file_name, mode='rb') as fd: with open(cache_file_name, mode='rb') as fd:
try:
while True: while True:
rec_len = fd.readline() rec_len = fd.readline()
if len(rec_len) != 8: if len(rec_len) != 8:
@ -752,14 +758,22 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self.known_metadata[lpath] = metadata self.known_metadata[lpath] = metadata
except: except:
traceback.print_exc() traceback.print_exc()
self.known_uuids = defaultdict(dict)
self.known_metadata = {}
try:
if os.path.exists(cache_file_name):
os.remove(cache_file_name)
except:
traceback.print_exc()
def _write_metadata_cache(self): def _write_metadata_cache(self):
from calibre.utils.config import to_json from calibre.utils.config import to_json
cache_file_name = os.path.join(cache_dir(), cache_file_name = os.path.join(cache_dir(),
'device_drivers_' + self.__class__.__name__ + 'device_drivers_' + self.__class__.__name__ +
'_metadata_cache.json') '_metadata_cache.json')
with open(cache_file_name, mode='wb') as fd:
try: try:
with open(cache_file_name, mode='wb') as fd:
for uuid,book in self.known_uuids.iteritems(): for uuid,book in self.known_uuids.iteritems():
json_metadata = defaultdict(dict) json_metadata = defaultdict(dict)
json_metadata[uuid]['book'] = self.json_codec.encode_book_metadata(book['book']) json_metadata[uuid]['book'] = self.json_codec.encode_book_metadata(book['book'])
@ -770,6 +784,11 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
fd.write('\n') fd.write('\n')
except: except:
traceback.print_exc() traceback.print_exc()
try:
if os.path.exists(cache_file_name):
os.remove(cache_file_name)
except:
traceback.print_exc()
def _set_known_metadata(self, book, remove=False): def _set_known_metadata(self, book, remove=False):
from calibre.utils.date import now from calibre.utils.date import now