Improve PEP8 gluon/recfile.py

This commit is contained in:
Hardirc
2015-03-06 22:15:42 -05:00
parent 8ac3191e33
commit d00667e804
+10 -5
View File
@@ -11,21 +11,23 @@ Generates names for cache and session files
""" """
import os, uuid import os, uuid
def generate(filename, depth=2, base=512): def generate(filename, depth=2, base=512):
if os.path.sep in filename: if os.path.sep in filename:
path, filename = os.path.split(filename) path, filename = os.path.split(filename)
else: else:
path = None 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 = [] folders = []
for level in range(depth-1,-1,-1): for level in range(depth-1, -1, -1):
code, dummyhash = divmod(dummyhash, base**level) code, dummyhash = divmod(dummyhash, base**level)
folders.append("%03x" % code) folders.append("%03x" % code)
folders.append(filename) folders.append(filename)
if path: if path:
folders.insert(0,path) folders.insert(0, path)
return os.path.join(*folders) return os.path.join(*folders)
def exists(filename, path=None): def exists(filename, path=None):
if os.path.exists(filename): if os.path.exists(filename):
return True return True
@@ -36,6 +38,7 @@ def exists(filename, path=None):
return True return True
return False return False
def remove(filename, path=None): def remove(filename, path=None):
if os.path.exists(filename): if os.path.exists(filename):
return os.unlink(filename) return os.unlink(filename)
@@ -46,6 +49,7 @@ def remove(filename, path=None):
return os.unlink(fullfilename) return os.unlink(fullfilename)
raise IOError raise IOError
def open(filename, mode="r", path=None): def open(filename, mode="r", path=None):
if not path: if not path:
path, filename = os.path.split(filename) path, filename = os.path.split(filename)
@@ -60,13 +64,14 @@ def open(filename, mode="r", path=None):
os.makedirs(os.path.dirname(fullfilename)) os.makedirs(os.path.dirname(fullfilename))
return file(fullfilename, mode) return file(fullfilename, mode)
def test(): def test():
if not os.path.exists('tests'): if not os.path.exists('tests'):
os.mkdir('tests') os.mkdir('tests')
for k in range(20): 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') open(filename, "w").write('test')
assert open(filename, "r").read()=='test' assert open(filename, "r").read() == 'test'
if exists(filename): if exists(filename):
remove(filename) remove(filename)