Christophe HENRY
3d71aa2e6f
It's not designed to be used for cryptographic stuff, unless you know what you're doing or you want to hide from your little sister. Xit processes files in the memory and is not designed to treat huge data.
80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
import re
|
|
|
|
class Sha1File:
|
|
""" Manages file names such as "name-{5}.txt" """
|
|
|
|
DIGEST_LEN = 40
|
|
|
|
def __init__(self, fileName):
|
|
self._fileName = fileName
|
|
res = re.findall(r'{(\d+)}', fileName)
|
|
self._ok = bool(res) and len(res)==1 and int(res[0])>0
|
|
self._occurences = int(res[0]) if self._ok else None
|
|
self._left, self._right = re.split(r'{\d+}', fileName) if self._ok else (None, None)
|
|
|
|
def __bool__(self): return self._ok
|
|
|
|
def __repr__(self): return self._fileName
|
|
|
|
def __int__(self): return self._occurences
|
|
|
|
def __len__(self):
|
|
if self._ok:
|
|
return self.DIGEST_LEN + len(self._left+self._right)
|
|
else:
|
|
return len(self._fileName)
|
|
|
|
def nameWithSha1(self, sha1):
|
|
assert(len(sha1)==self.DIGEST_LEN)
|
|
return self._left + sha1 + self._right
|
|
|
|
|
|
class StrKey:
|
|
""" Manages string key parameters enclosed by underscores such _string key_ """
|
|
|
|
def __init__(self, strKey):
|
|
m = re.match('^_(.+)_$', strKey)
|
|
self._ok, self._value = (True, m.group(1)) if m else (False, None)
|
|
|
|
def __bool__(self): return self._ok
|
|
|
|
def __repr__(self): return self._value
|
|
|
|
def __len__(self): return len(self._value)
|
|
|
|
|
|
def evalMultiple(size):
|
|
""" Takes size as "123k" and return "123000", same for m, g, and t.
|
|
1024 multiples are also supported: ki, mi, gi, ti
|
|
Raises ValueError if size malformed
|
|
"""
|
|
|
|
size = ''.join(size.split()).lower() # remove any blank caracter
|
|
multiples = {"ki":1024, "mi":1024**2, "gi":1024**3, "ti":1024**4,
|
|
"k":1000, "m":1000**2, "g":1000**3, "t":1000**4, }
|
|
for m in multiples:
|
|
if size.endswith(m):
|
|
size = int(size[:-len(m)]) * multiples[m]
|
|
break
|
|
return int(size)
|
|
|
|
|
|
def splitList(L, needle):
|
|
""" Splits a list in chuncks around the needle
|
|
[1,2,3], 2 --> [ [1], [3] ]
|
|
[1,2,2,3], 2 --> [ [1], [], [3] ]
|
|
[2,1,2], 2 --> [ [], [1], [] ]
|
|
[4,5,6], 2 --> [ [4, 5, 6] ]
|
|
"""
|
|
output, lastPos = [], 0
|
|
while True:
|
|
try:
|
|
pos = L.index(needle, lastPos)
|
|
output.append(L[lastPos:pos])
|
|
lastPos = pos+1
|
|
except ValueError:
|
|
output.append(L[lastPos:])
|
|
break
|
|
return output
|
|
|