Exemplo n.º 1
0
    def setUp(self):
        file_factory_test_dir = os.path.join(
            os.path.split(__file__)[0], 'FileFactoryTestFiles')

        self.test_file = os.path.join(file_factory_test_dir, 'test1.txt')
        self.other_test_file = os.path.join(file_factory_test_dir, 'test2.txt')
        self.uut = FileFactory(self.test_file)
        self.other_file_factory = FileFactory(self.other_test_file)
Exemplo n.º 2
0
class FileFactoryTest(unittest.TestCase):
    def setUp(self):
        file_factory_test_dir = os.path.join(
            os.path.split(__file__)[0], 'FileFactoryTestFiles')

        self.test_file = os.path.join(file_factory_test_dir, 'test1.txt')
        self.other_test_file = os.path.join(file_factory_test_dir, 'test2.txt')
        self.uut = FileFactory(self.test_file)
        self.other_file_factory = FileFactory(self.other_test_file)

    def test_equal(self):
        self.assertEqual(self.uut, FileFactory(self.test_file))
        self.assertNotEqual(self.uut, self.other_file_factory)

    def test_iter(self):
        self.assertEqual(list(self.uut), ['This is a test file.\n'])

    def test_line(self):
        self.assertEqual(self.uut.get_line(0), 'This is a test file.\n')
        with self.assertRaises(IndexError):
            self.uut.get_line(1)

    def test_deprecated_dict_getitem(self):
        self.assertEqual(self.uut[0], 'This is a test file.\n')
        with self.assertRaises(IndexError):
            self.uut[1]

    def test_lines(self):
        self.assertEqual(self.uut.lines, ('This is a test file.\n', ))
        self.assertEqual(self.uut.lines, ('This is a test file.\n', ))

    def test_raw(self):
        self.assertEqual(self.uut.raw, b'This is a test file.')
        self.assertEqual(self.uut.raw, b'This is a test file.')

    def test_string(self):
        self.assertEqual(self.uut.string, 'This is a test file.')
        self.assertEqual(self.uut.string, 'This is a test file.')

    def test_timestamp(self):
        self.assertEqual(self.uut.timestamp, os.path.getmtime(self.test_file))

    def test_name(self):
        self.assertEqual(
            get_path_components(self.uut.name)[-4:],
            ['tests', 'io', 'FileFactoryTestFiles', 'test1.txt'])
Exemplo n.º 3
0
def get_file_dict(filename_list, log_printer=None, allow_raw_files=False):
    """
    Reads all files into a dictionary.

    :param filename_list:   List of names of paths to files to get contents of.
    :param log_printer:     The logger which logs errors.
    :param allow_raw_files: Allow the usage of raw files (non text files),
                            disabled by default
    :return:                Reads the content of each file into a dictionary
                            with filenames as keys.
    """
    file_dict = FileDict()
    for filename in filename_list:
        try:
            file_dict[filename] = FileFactory(filename)
            # This is a workaround to purposely raise a
            # ``UnicodeDecodeError`` to move into the
            # except clause.
            FileFactory(filename).string
        except UnicodeDecodeError:
            if allow_raw_files:
                file_dict[filename] = None
                continue
            else:
                # In case raw files are not allowed the
                # ``FileFactory`` object created for it
                # needs to be evicted from the dictionary.
                del file_dict[filename]
            logging.warning(
                "Failed to read file '{}'. It seems to contain "
                'non-unicode characters. Leaving it out.'.format(filename))
        except OSError as exception:
            log_exception("Failed to read file '{}' because of an unknown "
                          'error. Leaving it out.'.format(filename),
                          exception,
                          log_level=LOG_LEVEL.WARNING)

    return file_dict
Exemplo n.º 4
0
 def test_equal(self):
     self.assertEqual(self.uut, FileFactory(self.test_file))
     self.assertNotEqual(self.uut, self.other_file_factory)