combox

split and encrypted files between online file storage providers
git clone git://git.ricketyspace.net/combox.git
Log | Files | Refs

commit f47f4e21067342c0b90b185e8eeeab8003a81e59
parent 4caa09b8093eec6919fccd8e2296430a17593d68
Author: Siddharth Ravikumar <sravik@bgsu.edu>
Date:   Thu, 30 Oct 2014 11:36:49 -0400

updated  combox/file.py: renamed split_file -> split_data and glue_file -> glue_data

split_data: reads a string or a stream of bytes and splits them into `n' parts.
glue_data: reads an array of strings / stream of bytes and glues them into one string / stream of bytes.
read_file: reads content from file and returns it.

---

"Python has been an important part of Google since the beginning, and
remains so as the system grows and evolves. Today dozens of Google
engineers use Python, and we're looking for more people with skills in
this language." said Peter Norvig, director of search quality at
Google, Inc.

---

Google has employed a Gingrich-era Republican as its head lobbyist in
DC. Now, Google is effectively helping Comcast kill the Internet as we
know it, and end "Net Neutrality" forever. -- SumOfUs.org

Sign a petition to ask Google to stand up for Net Neutrality:

   http://action.sumofus.org/a/googlesupportnn/

Diffstat:
combox/file.py | 72+++++++++++++++++++++++++++++++++++++++++-------------------------------
combox/tests/test_file.py | 9+++++----
2 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/combox/file.py b/combox/file.py @@ -20,52 +20,63 @@ from os import path from sys import exit -def split_file(filename, n): - """Split the file into `n' parts and return them as an array. +def split_data(data, n): + """Split data into `n' parts and return them as an array. - filename: Absolute pathname of the file. + data: Stream of bytes or string n: Number of parts the file has to be split. """ - file_ = None - try: - file_ = open(filename, 'rb') - except IOError: - print "ERROR: opening %s" % (filename) - exit(1) - - f_parts = [] + d_parts = [] # File size in bytes. - file_size = path.getsize(filename) - # No. of bytes for each file part. - part_size = file_size / n - # Take note of remaining bytes; this is non-zero when file_size is + data_size = len(data) + # No. of bytes for each data part. + part_size = data_size / n + # Take note of remaining bytes; this is non-zero when data_size is # not divisible by `n'. - rem_bytes = file_size % n + rem_bytes = data_size % n - i = 0 - while i < n: - f_parts.append(file_.read(part_size)) - i += 1 + start = 0 + end = part_size + while end <= data_size: + d_parts.append(data[start:end]) + start = end + end = end + part_size - # read the remaining bytes into the last file part. - f_parts[n-1] += file_.read(rem_bytes) + # read the remaining bytes into the last data part. + end += start + rem_bytes + d_parts[n-1] += data[start:end] - return f_parts + return d_parts -def glue_file(f_parts): - """Glue different parts of the file to one. +def glue_data(d_parts): + """Glue different parts of the data to one. - f_parts: Array containing different parts of the file. Each part + f_parts: Array containing different parts of the data. Each part is a sequence of bytes. """ - file_content = '' - for part in f_parts: - file_content += part + data = '' + for part in d_parts: + data += part + + return data - return file_content + +def read_file(filename): + """Read file and return it as a string. + + filename: Absolute pathname of the file. + """ + file_ = None + try: + file_ = open(filename, 'rb') + except IOError: + print "ERROR: opening %s" % (filename) + exit(1) + + return file_.read() def write_file(filename, filecontent): @@ -74,7 +85,6 @@ def write_file(filename, filecontent): filename: Absolute pathname of the file. filecontent: String/bytstream to write to filename. """ - file_ = None try: file_ = open(filename, 'wb') diff --git a/combox/tests/test_file.py b/combox/tests/test_file.py @@ -18,12 +18,13 @@ from os import path -from file import split_file, glue_file, write_file +from file import split_data, glue_data, write_file, read_file ### Test to split, glue and create a copy of the image file from the ### glued image file. f = path.abspath('tests/files/the-red-star.jpg') +f_content = read_file(f) +f_parts = split_data(f_content, 5) +f_content = glue_data(f_parts) f_copy = path.abspath('tests/files/the-red-star-copy.jpg') -f_parts = split_file(f, 3) -filecontent = glue_file(f_parts) -write_file(f_copy, filecontent) +write_file(f_copy, f_content)