core.utils.cachename
View Source
import hashlib, os from core.utils import write_json_file, read_json_file, check_and_create_folder class CacheName(): """ The file names of the caches can become very long. This class checks the length and creates smaller files to not exceed the maximum length for a file system. """ def filename(name): ''' Checks a file name Args: filename (string): The file name (full path) to check Retruns: The new (short enough) file name. ''' if(len(name) > 200): basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:name.rfind('.')]; ending = name[name.rfind('.')+1:]; return CacheName._add_index(basepath, filename, ending=ending) else: return name def dirname(name): ''' Checks a directory name Args: filename (string): The directory name (full path) to check (do not add a file!!!) Retruns: The new (short enough) directory name. ''' if(len(name) > 150): if name[-1] == '/': name = name[:-1] basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:] return CacheName._add_index(basepath, filename) else: return name def _add_index(basepath, filename, ending=''): check_and_create_folder(basepath) if '-' in filename: middle = filename[:filename.find('-')] + '-' + CacheName._hash(filename) else: middle = CacheName._hash(filename); index_file = os.path.join(basepath, 'index.json') if os.path.isfile(index_file): index_json = read_json_file(index_file) else: index_json = {} if not middle in index_json: index_json[middle] = filename + ( '' if len(ending) < 1 else '.' + ending ) write_json_file(index_file, index_json) return basepath + '/' + middle + ( '' if len(ending) < 1 else '.' + ending ) def _hash(str): return hashlib.sha256(str.encode('utf-8')).hexdigest()
View Source
class CacheName(): """ The file names of the caches can become very long. This class checks the length and creates smaller files to not exceed the maximum length for a file system. """ def filename(name): ''' Checks a file name Args: filename (string): The file name (full path) to check Retruns: The new (short enough) file name. ''' if(len(name) > 200): basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:name.rfind('.')]; ending = name[name.rfind('.')+1:]; return CacheName._add_index(basepath, filename, ending=ending) else: return name def dirname(name): ''' Checks a directory name Args: filename (string): The directory name (full path) to check (do not add a file!!!) Retruns: The new (short enough) directory name. ''' if(len(name) > 150): if name[-1] == '/': name = name[:-1] basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:] return CacheName._add_index(basepath, filename) else: return name def _add_index(basepath, filename, ending=''): check_and_create_folder(basepath) if '-' in filename: middle = filename[:filename.find('-')] + '-' + CacheName._hash(filename) else: middle = CacheName._hash(filename); index_file = os.path.join(basepath, 'index.json') if os.path.isfile(index_file): index_json = read_json_file(index_file) else: index_json = {} if not middle in index_json: index_json[middle] = filename + ( '' if len(ending) < 1 else '.' + ending ) write_json_file(index_file, index_json) return basepath + '/' + middle + ( '' if len(ending) < 1 else '.' + ending ) def _hash(str): return hashlib.sha256(str.encode('utf-8')).hexdigest()
The file names of the caches can become very long. This class checks the length and creates smaller files to not exceed the maximum length for a file system.
View Source
def filename(name): ''' Checks a file name Args: filename (string): The file name (full path) to check Retruns: The new (short enough) file name. ''' if(len(name) > 200): basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:name.rfind('.')]; ending = name[name.rfind('.')+1:]; return CacheName._add_index(basepath, filename, ending=ending) else: return name
Checks a file name
Args
- filename (string): The file name (full path) to check
Retruns
The new (short enough) file name.
View Source
def dirname(name): ''' Checks a directory name Args: filename (string): The directory name (full path) to check (do not add a file!!!) Retruns: The new (short enough) directory name. ''' if(len(name) > 150): if name[-1] == '/': name = name[:-1] basepath = name[:name.rfind('/')]; filename = name[name.rfind('/')+1:] return CacheName._add_index(basepath, filename) else: return name
Checks a directory name
Args
- filename (string): The directory name (full path) to check (do not add a file!!!)
Retruns
The new (short enough) directory name.