diff options
Diffstat (limited to 'combox/file.py')
-rw-r--r-- | combox/file.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/combox/file.py b/combox/file.py index ab76315..ec0372e 100644 --- a/combox/file.py +++ b/combox/file.py @@ -18,6 +18,7 @@ from os import path from sys import exit +from glob import glob def split_data(data, n): @@ -92,3 +93,35 @@ def write_file(filename, filecontent): except IOError: print "ERROR: creating and writing content to %s" % (filename) exit(1) + +def write_shards(shards, directory, shard_basename): + """Write shards to respective files respective files. + + shard: list of strings (ciphers or data). + directory: absolute path of directory to which it shards must be written to. + shard_basename: base name of the shard. + """ + + # partial filename of the shard + p_filename = path.join(directory, shard_basename) + shard_no = 0 + for shard in shards: + shard_name = "%s.shard%s" % (p_filename, shard_no) + write_file(shard_name, shard) + shard_no += 1 + +def read_shards(directory, shard_basename): + """Read the shards from directory and return it as a list. + + directory: absolute path of directory from which to read the shards. + shard_basename: base name of the shard. + """ + + shards = [] + # partial filename of the shard + p_filename = "%s.shard*" % path.join(directory, shard_basename) + for shard_file in sorted(glob(p_filename)): + shard_content = read_file(shard_file) + shards.append(shard_content) + + return shards |