Example #1
0
    def test_convert_files(self):
        numarray_file_path = os.path.join(self.test_data_path, 'numarray_inputs', 'do_not_change_me.sometext')
        numpy_file_path = os.path.join(self.test_data_path, 'numpy_inputs', 'do_not_change_me.sometext')
        
        output_directory = self.temp_dir
        convert = ConvertNumarrayCacheToNumpyCache()
        
        convert.convert_file(os.path.join(self.test_data_path, 'numarray_inputs'), 'do_not_change_me.sometext', output_directory)
        self.assert_(os.path.exists(os.path.join(output_directory, 'do_not_change_me.sometext')))
        
        endian = file_flt_storage.storage_file(None)._get_native_endian_file_extension_character()

        convert.convert_file(os.path.join(self.test_data_path, 'numarray_inputs'), 'f.Int32', output_directory)
        self.assert_(os.path.exists(os.path.join(output_directory, 'f.%si4' % endian)))
        
        convert.convert_file(os.path.join(self.test_data_path, 'numarray_inputs'), 'd.Float32', output_directory)
        self.assert_(os.path.exists(os.path.join(output_directory, 'd.%sf4' % endian)))
        
        convert.convert_file(os.path.join(self.test_data_path, 'numarray_inputs'), 'c.txt', output_directory)
        self.assert_(os.path.exists(os.path.join(output_directory, 'c.iS7')))
        
        # Does the file contain the expected data?
        f = open(os.path.join(output_directory, 'c.iS7'), 'rb')
        actual = f.read()
        f.close()
        
        f = open(os.path.join(self.test_data_path, 'numpy_inputs', 'c.iS7'), 'rb')
        expected = f.read()
        f.close()
        
        self.assertEqual(expected, actual)
    def old_to_new_extension_mapping_for_binary_files(self):
        endian = file_flt_storage.storage_file(None)._get_native_endian_file_extension_character()

        return {
            ".Bool": ".ib1",
            ".Int8": ".%si1" % endian,
            ".Int16": ".%si2" % endian,
            ".Int32": ".%si4" % endian,
            ".Int64": ".%si8" % endian,
            ".Float32": ".%sf4" % endian,
            ".Float64": ".%sf8" % endian,
        }
    def old_to_new_extension_mapping_for_binary_files(self):
        endian = file_flt_storage.storage_file(
            None)._get_native_endian_file_extension_character()

        return {
            '.Bool': '.ib1',
            '.Int8': '.%si1' % endian,
            '.Int16': '.%si2' % endian,
            '.Int32': '.%si4' % endian,
            '.Int64': '.%si8' % endian,
            '.Float32': '.%sf4' % endian,
            '.Float64': '.%sf8' % endian,
        }
Example #4
0
    def test_convert_files(self):
        numarray_file_path = os.path.join(self.test_data_path,
                                          'numarray_inputs',
                                          'do_not_change_me.sometext')
        numpy_file_path = os.path.join(self.test_data_path, 'numpy_inputs',
                                       'do_not_change_me.sometext')

        output_directory = self.temp_dir
        convert = ConvertNumarrayCacheToNumpyCache()

        convert.convert_file(
            os.path.join(self.test_data_path, 'numarray_inputs'),
            'do_not_change_me.sometext', output_directory)
        self.assert_(
            os.path.exists(
                os.path.join(output_directory, 'do_not_change_me.sometext')))

        endian = file_flt_storage.storage_file(
            None)._get_native_endian_file_extension_character()

        convert.convert_file(
            os.path.join(self.test_data_path, 'numarray_inputs'), 'f.Int32',
            output_directory)
        self.assert_(
            os.path.exists(os.path.join(output_directory, 'f.%si4' % endian)))

        convert.convert_file(
            os.path.join(self.test_data_path, 'numarray_inputs'), 'd.Float32',
            output_directory)
        self.assert_(
            os.path.exists(os.path.join(output_directory, 'd.%sf4' % endian)))

        convert.convert_file(
            os.path.join(self.test_data_path, 'numarray_inputs'), 'c.txt',
            output_directory)
        self.assert_(os.path.exists(os.path.join(output_directory, 'c.iS7')))

        # Does the file contain the expected data?
        f = open(os.path.join(output_directory, 'c.iS7'), 'rb')
        actual = f.read()
        f.close()

        f = open(os.path.join(self.test_data_path, 'numpy_inputs', 'c.iS7'),
                 'rb')
        expected = f.read()
        f.close()

        self.assertEqual(expected, actual)
    def convert_file(self, file_directory, file_name, output_directory):
        file_path = os.path.join(file_directory, file_name)
        file_stem, extension = os.path.splitext(file_name)

        if not os.path.exists(output_directory):
            os.makedirs(output_directory)

        if extension in self.old_to_new_extension_mapping_for_binary_files():
            # Copy file to name with new extension
            new_extension = self.old_to_new_extension_mapping_for_binary_files()[extension]
            new_file_path = os.path.join(output_directory, "%s%s" % (file_stem, new_extension))
            copyfile(file_path, new_file_path)

        elif extension == ".txt":
            data = load_from_text_file(file_path)
            numpy_array = numpy.array(data)
            storage = file_flt_storage.storage_file(None)
            storage._write_to_file(output_directory, file_stem, numpy_array)

        else:
            copyfile(file_path, os.path.join(output_directory, file_name))
    def convert_file(self, file_directory, file_name, output_directory):
        file_path = os.path.join(file_directory, file_name)
        file_stem, extension = os.path.splitext(file_name)

        if not os.path.exists(output_directory):
            os.makedirs(output_directory)

        if extension in self.old_to_new_extension_mapping_for_binary_files():
            # Copy file to name with new extension
            new_extension = self.old_to_new_extension_mapping_for_binary_files(
            )[extension]
            new_file_path = os.path.join(output_directory,
                                         '%s%s' % (file_stem, new_extension))
            copyfile(file_path, new_file_path)

        elif extension == '.txt':
            data = load_from_text_file(file_path)
            numpy_array = numpy.array(data)
            storage = file_flt_storage.storage_file(None)
            storage._write_to_file(output_directory, file_stem, numpy_array)

        else:
            copyfile(file_path, os.path.join(output_directory, file_name))