Update TMDB api

This commit is contained in:
Ruud
2014-02-16 10:48:48 +01:00
parent f22778aacb
commit a1c0b000a4
12 changed files with 797 additions and 450 deletions
+32 -23
View File
@@ -12,6 +12,7 @@
import struct
import errno
import json
import time
import os
import io
@@ -54,11 +55,11 @@ def _donothing(*args, **kwargs):
try:
import fcntl
class Flock( object ):
class Flock(object):
"""
Context manager to flock file for the duration the object exists.
Referenced file will be automatically unflocked as the interpreter
exits the context.
Context manager to flock file for the duration the object
exists. Referenced file will be automatically unflocked as the
interpreter exits the context.
Supports an optional callback to process the error and optionally
suppress it.
"""
@@ -69,8 +70,10 @@ try:
self.fileobj = fileobj
self.operation = operation
self.callback = callback
def __enter__(self):
fcntl.flock(self.fileobj, self.operation)
def __exit__(self, exc_type, exc_value, exc_tb):
suppress = False
if callable(self.callback):
@@ -101,9 +104,11 @@ except ImportError:
self.fileobj = fileobj
self.operation = operation
self.callback = callback
def __enter__(self):
self.size = os.path.getsize(self.fileobj.name)
msvcrt.locking(self.fileobj.fileno(), self.operation, self.size)
def __exit__(self, exc_type, exc_value, exc_tb):
suppress = False
if callable(self.callback):
@@ -118,7 +123,7 @@ except ImportError:
if filename.startswith('~'):
# check for home directory
return os.path.expanduser(filename)
elif (ord(filename[0]) in (range(65,91)+range(99,123))) \
elif (ord(filename[0]) in (range(65, 91) + range(99, 123))) \
and (filename[1:3] == ':\\'):
# check for absolute drive path (e.g. C:\...)
return filename
@@ -126,12 +131,12 @@ except ImportError:
# check for absolute UNC path (e.g. \\server\...)
return filename
# return path with temp directory prepended
return os.path.expandvars(os.path.join('%TEMP%',filename))
return os.path.expandvars(os.path.join('%TEMP%', filename))
class FileCacheObject( CacheObject ):
_struct = struct.Struct('dII') # double and two ints
# timestamp, lifetime, position
class FileCacheObject(CacheObject):
_struct = struct.Struct('dII') # double and two ints
# timestamp, lifetime, position
@classmethod
def fromFile(cls, fd):
@@ -150,7 +155,7 @@ class FileCacheObject( CacheObject ):
@property
def size(self):
if self._size is None:
self._buff.seek(0,2)
self._buff.seek(0, 2)
size = self._buff.tell()
if size == 0:
if (self._key is None) or (self._data is None):
@@ -159,8 +164,10 @@ class FileCacheObject( CacheObject ):
self._size = self._buff.tell()
self._size = size
return self._size
@size.setter
def size(self, value): self._size = value
def size(self, value):
self._size = value
@property
def key(self):
@@ -170,16 +177,20 @@ class FileCacheObject( CacheObject ):
except:
pass
return self._key
@key.setter
def key(self, value): self._key = value
def key(self, value):
self._key = value
@property
def data(self):
if self._data is None:
self._key, self._data = json.loads(self._buff.getvalue())
return self._data
@data.setter
def data(self, value): self._data = value
def data(self, value):
self._data = value
def load(self, fd):
fd.seek(self.position)
@@ -199,7 +210,7 @@ class FileCacheObject( CacheObject ):
class FileEngine( CacheEngine ):
"""Simple file-backed engine."""
name = 'file'
_struct = struct.Struct('HH') # two shorts for version and count
_struct = struct.Struct('HH') # two shorts for version and count
_version = 2
def __init__(self, parent):
@@ -219,7 +230,6 @@ class FileEngine( CacheEngine ):
if self.cachefile is None:
raise TMDBCacheError("No cache filename given.")
self.cachefile = parse_filename(self.cachefile)
try:
@@ -246,7 +256,7 @@ class FileEngine( CacheEngine ):
else:
# let the unhandled error continue through
raise
elif e.errno == errno.EACCESS:
elif e.errno == errno.EACCES:
# file exists, but we do not have permission to access it
raise TMDBCacheReadError(self.cachefile)
else:
@@ -257,7 +267,7 @@ class FileEngine( CacheEngine ):
self._init_cache()
self._open('r+b')
with Flock(self.cachefd, Flock.LOCK_SH): # lock for shared access
with Flock(self.cachefd, Flock.LOCK_SH):
# return any new objects in the cache
return self._read(date)
@@ -265,7 +275,7 @@ class FileEngine( CacheEngine ):
self._init_cache()
self._open('r+b')
with Flock(self.cachefd, Flock.LOCK_EX): # lock for exclusive access
with Flock(self.cachefd, Flock.LOCK_EX):
newobjs = self._read(self.age)
newobjs.append(FileCacheObject(key, value, lifetime))
@@ -283,7 +293,8 @@ class FileEngine( CacheEngine ):
# already opened in requested mode, nothing to do
self.cachefd.seek(0)
return
except: pass # catch issue of no cachefile yet opened
except:
pass # catch issue of no cachefile yet opened
self.cachefd = io.open(self.cachefile, mode)
def _read(self, date):
@@ -310,7 +321,7 @@ class FileEngine( CacheEngine ):
return []
# get end of file
self.cachefd.seek(0,2)
self.cachefd.seek(0, 2)
position = self.cachefd.tell()
newobjs = []
emptycount = 0
@@ -348,7 +359,7 @@ class FileEngine( CacheEngine ):
data = data[-1]
# determine write position of data in cache
self.cachefd.seek(0,2)
self.cachefd.seek(0, 2)
end = self.cachefd.tell()
data.position = end
@@ -387,5 +398,3 @@ class FileEngine( CacheEngine ):
def expire(self, key):
pass