-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ test: unittest | |
|
||
unittest: | ||
pytest --doctest-modules --last-failed --durations=3 | ||
|
||
style: | ||
autoflake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
def parse_chunks(data): | ||
alg_name, chunks = data.split('!') | ||
if chunks: | ||
chunks = [c.split(':') for c in chunks.split(',') if c] | ||
chunks = [(id, int(size)) for id, size in chunks] | ||
return alg_name, chunks | ||
else: | ||
return alg_name, [] | ||
|
||
|
||
def parse_line(line): | ||
""" | ||
>>> from pprint import pprint | ||
>>> pprint(parse_line('sum1 ./a fck0sha2!')) | ||
{'alg_name': 'fck0sha2', 'checksum': 'sum1', 'chunks': [], 'path': './a'} | ||
>>> pprint(parse_line('sum2 ./file1 fck0sha2!abcd:10')) | ||
{'alg_name': 'fck0sha2', | ||
'checksum': 'sum2', | ||
'chunks': [('abcd', 10)], | ||
'path': './file1'} | ||
>>> pprint(parse_line('sum3 ./file2 fck0sha2!bad:20,beef:30')) | ||
{'alg_name': 'fck0sha2', | ||
'checksum': 'sum3', | ||
'chunks': [('bad', 20), ('beef', 30)], | ||
'path': './file2'} | ||
""" | ||
items = line.split(' ') | ||
checksum = items[0] | ||
chunks = items[-1] | ||
if len(items) > 3: | ||
path = ' '.join(items[1:-1]) | ||
else: | ||
path = items[1] | ||
alg_name, chunks = parse_chunks(chunks) | ||
return dict(checksum=checksum, | ||
path=path, | ||
alg_name=alg_name, | ||
chunks=chunks) | ||
|
||
|
||
def parse_chunksums(file): | ||
""" | ||
>>> from pprint import pprint | ||
>>> import io | ||
>>> pprint(list(parse_chunksums(io.StringIO(''' | ||
... sum1 ./a fck0sha2! | ||
... sum2 ./file1 fck0sha2!abcd:10 | ||
... sum3 ./file2 fck0sha2!bad:20,beef:30''')))) | ||
[{'alg_name': 'fck0sha2', 'checksum': 'sum1', 'chunks': [], 'path': './a'}, | ||
{'alg_name': 'fck0sha2', | ||
'checksum': 'sum2', | ||
'chunks': [('abcd', 10)], | ||
'path': './file1'}, | ||
{'alg_name': 'fck0sha2', | ||
'checksum': 'sum3', | ||
'chunks': [('bad', 20), ('beef', 30)], | ||
'path': './file2'}] | ||
""" | ||
for line in file: | ||
line = line.strip() | ||
if not line: | ||
continue | ||
yield parse_line(line) |