summaryrefslogtreecommitdiffstats
path: root/combox/file.py
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2014-11-05 22:43:07 -0500
committerSiddharth Ravikumar <sravik@bgsu.edu>2014-11-05 22:43:07 -0500
commit1535a2f1200db9da8e60f05500bc8cb74fef9360 (patch)
tree25e19272f8902725f5a33c6cff368eb78ad43356 /combox/file.py
parent53ff471b90ffe5a0c25e98b6dfdc8b07f111f853 (diff)
file.py: wrote two new functions -- write_shards(), read_shards()
modified: file.py modified: tests/test_file.py
Diffstat (limited to 'combox/file.py')
-rw-r--r--combox/file.py33
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