From 1535a2f1200db9da8e60f05500bc8cb74fef9360 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Wed, 5 Nov 2014 22:43:07 -0500 Subject: file.py: wrote two new functions -- write_shards(), read_shards() modified: file.py modified: tests/test_file.py --- combox/file.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'combox/file.py') 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 -- cgit v1.2.3