예제 #1
0
    def read(self, params=None):
        if not params:
            params = {
                'num_line': 1,
            }
        else:
            assert isinstance(params, dict) and 'num_line' in params

        for param in params:
            if param != 'num_line':
                self._logger.warning(
                    param +
                    " will be omitted since it is not useful as an input argument in this function."
                )
                self._SYS_LOGGER.warning(
                    param +
                    " will be omitted since it is not useful as an input argument in this function."
                )
        try:
            self.increment_rpc_count_by(n=1)
            lines = FileUtil.read_lined_txt_from_file(self._file_name)
        except Exception as _:
            lines = []

        if 'read_rule_type' in self._config and self._config[
                'read_rule_type'] == ReadRuleType.READ_FROM_END:
            lines = lines[::-1]
        if params['num_line'] < 0:
            return [line.strip() for line in lines]

        new_line_number = self._last_read_line + params['num_line']
        if new_line_number > len(lines):
            self._SYS_LOGGER.error(
                str(new_line_number) + " exceeds the file limit. [" +
                self.get_file_name() + "] only has " + str(len(lines)) +
                " lines.")
            self._logger.error(
                str(new_line_number) + " exceeds the file limit. [" +
                self.get_file_name() + "] only has " + str(len(lines)) +
                " lines.")
            raise StoragePastLineException(
                str(new_line_number) + " exceeds the file limit. [" +
                self.get_file_name() + "] only has " + str(len(lines)) +
                " lines.")
        else:
            try:
                result = lines[self._last_read_line:new_line_number]
                assert len(result) == params['num_line']
                result = [line.strip() for line in result]
                self._last_read_line = new_line_number
                return result
            except Exception as err:
                self._SYS_LOGGER.error("Read file [" + self._file_name +
                                       "] got exception: " + str(err) + '.')
                self._logger.error("Read file [" + self._file_name +
                                   "] got exception: " + str(err) + '.')
                raise StorageReadException("Read file [" + self._file_name +
                                           "] got exception: " + str(err) +
                                           '.')
예제 #2
0
 def test_lined_txt_to_file(self):
     data = ['123', '234']
     FileUtil.write_lined_txt_to_file(data=data, file_name=self.TEST_DATA_PATH_1)
     self.assertEqual(data, FileUtil.read_lined_txt_from_file(file_name=self.TEST_DATA_PATH_1))