Exemple #1
0
    def delete_complete_file(self, succ: bool, data: OutputData):
        """
        回调函数,在文件被读完后删除
        :param filename:
        :return:
        """
        res = True
        try:
            if not hasattr(data, "isdeleteable") or not hasattr(
                    data, "filepath_telegram"):
                return res
            if not data.isdeleteable:
                return res
            if data.filepath_telegram is None:
                self._logger.error("Telegram deleteable filepath is None")
                return res

            stm = data.get_stream()
            if stm is not None and not stm.closed:
                stm.close()
            if not data.filepath_telegram.exists():
                self._logger.error(
                    "Telegram deletable file is not found: {}".format(
                        data.filepath_telegram))
                return res

            data.filepath_telegram.unlink()
            # filename.unlink()
        except:
            res = False
        return res
Exemple #2
0
    def output_to_file(cls, data: OutputData, datastd: OutputDataConfig,
                       targetdir: str) -> bool:
        """输出数据到指定目录"""
        res: bool = False
        try:
            if not isinstance(data, OutputData):
                cls._logger.error("Invalid OutputData object: {}".format(data))
                return res

            if not isinstance(datastd, OutputDataConfig):
                cls._logger.error(
                    "Invalid OutputDataStandard: {}".format(datastd))
                return res

            stm = data.get_stream()

            if not isinstance(stm, io.IOBase) or not stm.readable():
                succ: bool = True
                for b in cls._get_mutiple_bs(data, datastd):
                    if not cls._output_to_file(data, b, datastd, targetdir):
                        succ = False
                        break
                res = succ
            else:
                b: bytes = cls._get_single_bs(data, datastd, stm)
                return cls._output_to_file(data, b, datastd, targetdir, stm)

        except Exception:
            cls._logger.error("Output data error: {} {}\n{}".format(
                data._platform, datastd._uniquename, traceback.format_exc()))
        return res
Exemple #3
0
    def output(self, data: OutputData, datastd: OutputDataConfig) -> bool:
        """异步输出。
        根据标准检验数据字段是否符合规范,并输出数据,返回bool指示是否输出成功\n
        data: 要输出的数据对象\n
        datastd: 此数据对应的数据标准"""
        res: bool = False
        try:
            if not isinstance(data, OutputData):
                self._logger.error(
                    "Invalid OutputData object: {}".format(data))
                return res

            if not isinstance(datastd, OutputDataConfig):
                self._logger.error(
                    "Invalid OutputDataStandard: {}".format(datastd))
                return res

            stm = data.get_stream()

            if not issubclass(type(stm), io.IOBase) or not stm.readable():
                succ: bool = True
                for b in self._get_mutiple_bs(data, datastd):
                    if not self._output_sub(data, b, datastd):
                        succ = False
                res = succ
            else:
                b: bytes = self._get_single_bs(data, datastd, stm)
                res = self._output_sub(data, b, datastd, stm)

            if callable(data.on_complete):
                data.on_complete(res, data)

        except Exception:
            self._logger.error("Output data error: {} {}\n{}".format(
                data._platform, datastd._uniquename, traceback.format_exc()))
        return res