Skip to content

Commit

Permalink
Improves cache file selection for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
wagnerrp committed Mar 29, 2012
1 parent 08bffa5 commit 263d868
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions tmdb3/cache_file.py
Expand Up @@ -45,11 +45,25 @@ def __exit__(self, exc_type, exc_value, exc_tb):
suppress = self.callback(exc_type, exc_value, exc_tb)
fcntl.flock(self.fileobj, fcntl.LOCK_UN)
return suppress

def parse_filename(filename):
if '$' in filename:
# replace any environmental variables
filename = os.path.expandvars(filename)
if filename.startswith('~'):
# check for home directory
return os.path.expanduser(filename)
elif not filename.startswith('/'):
# check for absolute path
return filename
# return path with temp directory prepended
return '/tmp/' + filename

except ImportError:
import msvcrt
class Flock( object ):
LOCK_EX = msvcrt.NBLCK
LOCK_SH = msvcrt.NBLCK
LOCK_EX = msvcrt.LK_LOCK
LOCK_SH = msvcrt.LK_LOCK

def __init__(self, fileobj, operation, callback=None):
self.fileobj = fileobj
Expand All @@ -65,6 +79,23 @@ def __exit__(self, exc_type, exc_value, exc_tb):
msvcrt.locking(self.fileobj.fileno(), msvcrt.LK_UNLCK, self.size)
return suppress

def parse_filename(filename):
if '%' in filename:
# replace any environmental variables
filename = os.path.expandvars(filename)
if filename.startswith('~'):
# check for home directory
return os.path.expanduser(filename)
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
elif (filename.count('\\') >= 3) and (filename.startswith('\\\\')):
# 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))

class FileEngine( CacheEngine ):
"""Simple file-backed engine."""
name = 'file'
Expand All @@ -86,10 +117,8 @@ def _init_cache(self):

if self.cachefile is None:
raise TMDBCacheError("No cache filename given.")
if self.cachefile.startswith('~'):
self.cachefile = os.path.expanduser(self.cachefile)
elif not self.cachefile.startswith('/'):
self.cachefile = '/tmp/' + self.cachefile

self.cachefile = parse_filename(self.cachefile)

try:
# attempt to read existing cache at filename
Expand Down

0 comments on commit 263d868

Please sign in to comment.