Improve PEP8 gluon/recfile.py

This commit is contained in:
Hardirc
2015-03-06 22:15:42 -05:00
parent 8ac3191e33
commit d00667e804

View File

@@ -11,21 +11,23 @@ Generates names for cache and session files
"""
import os, uuid
def generate(filename, depth=2, base=512):
if os.path.sep in filename:
path, filename = os.path.split(filename)
else:
path = None
dummyhash = sum(ord(c)*256**(i % 4) for i,c in enumerate(filename)) % base**depth
dummyhash = sum(ord(c)*256**(i % 4) for i, c in enumerate(filename)) % base**depth
folders = []
for level in range(depth-1,-1,-1):
for level in range(depth-1, -1, -1):
code, dummyhash = divmod(dummyhash, base**level)
folders.append("%03x" % code)
folders.append(filename)
if path:
folders.insert(0,path)
folders.insert(0, path)
return os.path.join(*folders)
def exists(filename, path=None):
if os.path.exists(filename):
return True
@@ -36,6 +38,7 @@ def exists(filename, path=None):
return True
return False
def remove(filename, path=None):
if os.path.exists(filename):
return os.unlink(filename)
@@ -46,6 +49,7 @@ def remove(filename, path=None):
return os.unlink(fullfilename)
raise IOError
def open(filename, mode="r", path=None):
if not path:
path, filename = os.path.split(filename)
@@ -60,13 +64,14 @@ def open(filename, mode="r", path=None):
os.makedirs(os.path.dirname(fullfilename))
return file(fullfilename, mode)
def test():
if not os.path.exists('tests'):
os.mkdir('tests')
for k in range(20):
filename = os.path.join('tests',str(uuid.uuid4())+'.test')
filename = os.path.join('tests', str(uuid.uuid4()) + '.test')
open(filename, "w").write('test')
assert open(filename, "r").read()=='test'
assert open(filename, "r").read() == 'test'
if exists(filename):
remove(filename)