From 4caa09b8093eec6919fccd8e2296430a17593d68 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Thu, 30 Oct 2014 09:10:14 -0400 Subject: renamed: utils/file.py -> file.py, tests/utils_file.py -> tests/test_file.py utils/ directory deleted now. --- combox/file.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++ combox/tests/test_file.py | 29 ++++++++++++++++ combox/tests/utils_file.py | 29 ---------------- combox/utils/__init__.py | 0 combox/utils/file.py | 84 ---------------------------------------------- 5 files changed, 113 insertions(+), 113 deletions(-) create mode 100644 combox/file.py create mode 100644 combox/tests/test_file.py delete mode 100644 combox/tests/utils_file.py delete mode 100644 combox/utils/__init__.py delete mode 100644 combox/utils/file.py 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 +# . + +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) diff --git a/combox/tests/test_file.py b/combox/tests/test_file.py new file mode 100644 index 0000000..08d0d85 --- /dev/null +++ b/combox/tests/test_file.py @@ -0,0 +1,29 @@ +# 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 +# . + +from os import path + +from file import split_file, glue_file, write_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_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) diff --git a/combox/tests/utils_file.py b/combox/tests/utils_file.py deleted file mode 100644 index c382d44..0000000 --- a/combox/tests/utils_file.py +++ /dev/null @@ -1,29 +0,0 @@ -# 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 -# . - -from os import path - -from utils.file import split_file, glue_file, write_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_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) diff --git a/combox/utils/__init__.py b/combox/utils/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/combox/utils/file.py b/combox/utils/file.py deleted file mode 100644 index bb27592..0000000 --- a/combox/utils/file.py +++ /dev/null @@ -1,84 +0,0 @@ -# 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 -# . - -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) -- cgit v1.2.3