summaryrefslogtreecommitdiffstats
path: root/combox/file.py
diff options
context:
space:
mode:
authorrsiddharth <rsiddharth@ninthfloor.org>2014-10-30 09:10:14 -0400
committerrsiddharth <rsiddharth@ninthfloor.org>2014-10-30 09:10:14 -0400
commit4caa09b8093eec6919fccd8e2296430a17593d68 (patch)
treea27e45818db878e1f3d1b32aaa1d63dc8a8d3723 /combox/file.py
parent2def977472b2e77ee88c9177f2d03f12b0263eb0 (diff)
renamed: utils/file.py -> file.py, tests/utils_file.py -> tests/test_file.py
utils/ directory deleted now.
Diffstat (limited to 'combox/file.py')
-rw-r--r--combox/file.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/combox/file.py b/combox/file.py
new file mode 100644
index 0000000..bb27592
--- /dev/null
+++ b/combox/file.py
@@ -0,0 +1,84 @@
+# Copyright (C) 2014 Combox author(s). See AUTHORS.
+#
+# This file is part of Combox.
+#
+# Combox is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Combox is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Combox (see COPYING). If not, see
+# <http://www.gnu.org/licenses/>.
+
+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.
+
+ filename: Absolute pathname of the file.
+ 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 = []
+ # 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
+ # not divisible by `n'.
+ rem_bytes = file_size % n
+
+ i = 0
+ while i < n:
+ f_parts.append(file_.read(part_size))
+ i += 1
+
+ # read the remaining bytes into the last file part.
+ f_parts[n-1] += file_.read(rem_bytes)
+
+ return f_parts
+
+
+def glue_file(f_parts):
+ """Glue different parts of the file to one.
+
+ f_parts: Array containing different parts of the file. Each part
+ is a sequence of bytes.
+ """
+
+ file_content = ''
+ for part in f_parts:
+ file_content += part
+
+ return file_content
+
+
+def write_file(filename, filecontent):
+ """Write `filecontent' to `filename'.
+
+ filename: Absolute pathname of the file.
+ filecontent: String/bytstream to write to filename.
+ """
+
+ file_ = None
+ try:
+ file_ = open(filename, 'wb')
+ file_.write(filecontent)
+ except IOError:
+ print "ERROR: creating and writing content to %s" % (filename)
+ exit(1)