Exemplo n.º 1
0
    def load_yaml_file(yaml_file):
        '''
        功能:加载yaml文件
        :param yaml_file: yaml文件
        :return: 一个列表或字典
        '''
        # TODO(dongjun): 检查文件是否存在
        # TODO(dongjun): 检查文件是不是yaml文件

        with open(yaml_file, mode='r',
                  encoding=_DEFAULT_ENCODING_UTF8) as stream:
            try:
                yaml_content = yaml.load(stream, Loader=yaml.FullLoader)
            except AttributeError:
                yaml_content = yaml.load(stream)

            # 文件内容不能为空
            if not yaml_content:
                err_msg = "文件内容为空: {}".format(yaml_file)
                raise exceptions.FileException(err_msg)

            # 文件内容不是 列表或字典
            elif not isinstance(yaml_content, (list, dict)):
                err_msg = "文件格式不正确: {}".format(yaml_file)
                raise exceptions.FileException(err_msg)

            return yaml_content
Exemplo n.º 2
0
    def csv_info(self, csv_file=None, curr_file=None):
        """
        功能: 将csv文件转为列表
        :param csv_file: csv文件
        :param curr_file: 默认填写__file__
        :return:
        """

        if not csv_file:
            curr_dir = os.path.dirname(curr_file)
            csv_file_list = FileHelper.get_files_from_folderOrFolderlist(
                curr_dir, ".csv", recursive=False)
            if len(csv_file_list) == 1:
                csv_file = csv_file_list[0]
            elif len(csv_file_list) == 0:
                raise exceptions.FileException(
                    "当前目录下不存在csv文件:{}".format(curr_dir))
            else:
                raise exceptions.FileException(
                    "当前目录下存在多个csv文件,请明确指明csv路径,:{}".format(curr_dir))

        validator.check_file_isFile(csv_file)

        info_list = CsvHelper.load_csv_file(csv_file)

        return info_list
Exemplo n.º 3
0
    def base_yaml_info(self,
                       yaml_file=None,
                       module_key=None,
                       func_key=None,
                       curr_file=None):
        """
        功能:先将yaml文件转为字典,再在字典中通过key取value。
        :param yaml_file: yaml文件
        :param module_key: 字典的key
        :param curr_file: 默认填写 __file__
        :return:
        """
        # TODO(dongjun): module_key 如果只有一个key时,自动读取

        if not yaml_file:
            curr_dir = os.path.dirname(curr_file)
            yaml_file_list = FileHelper.get_files_from_folderOrFolderlist(
                curr_dir, ".yaml", recursive=False)
            if len(yaml_file_list) == 1:
                yaml_file = yaml_file_list[0]
            elif len(yaml_file_list) == 0:
                raise exceptions.FileException(
                    "当前目录下不存在yaml文件:{}".format(curr_dir))
            else:
                raise exceptions.FileException(
                    "当前目录下存在多个yaml文件,请明确指明yaml路径,:{}".format(curr_dir))

        if not module_key:
            module_key = ""
        validator.check_file_isFile(yaml_file)
        validator.check_paramType_str(module_key)

        api_dictInfo = YamlHelper.load_yaml_file(yaml_file)
        api_info = api_dictInfo.get(module_key)
        if module_key is "":
            return api_dictInfo
        elif api_info:
            if func_key:
                api_info = api_info.get(func_key)
                if api_info:
                    return api_info
                raise exceptions.DictException(
                    "yaml中func_key不存在。yaml_path:{},目标key:{}".format(
                        yaml_file, func_key))

            return api_info
        else:
            raise exceptions.DictException(
                "yaml中module_key不存在。yaml_path:{},目标key:{}".format(
                    yaml_file, module_key))
Exemplo n.º 4
0
    def create_filepath(filepath):
        """
        功能:如果文件路径不存在就创建
        :param filepath: 文件路径,需要是绝对路径
        :return:
        """

        if not os.path.isabs(filepath):
            raise exceptions.FileException("参数必须是绝对路径")

        path = filepath[0:filepath.rfind(os.sep)]

        if not os.path.isdir(path):
            try:
                os.makedirs(path)
                log.log_info("创建文件夹成功:{}".format(path))
            except:
                log.log_error("创建文件夹失败:{}".format(path))

        if not os.path.isfile(filepath):
            with open(filepath, mode='w', encoding='utf-8'):
                log.log_info("创建文件成功:{}".format(filepath))
                pass
        else:
            pass
Exemplo n.º 5
0
def create_filepath(filepath):

    validator.check_paramType_str(filepath)
    if not os.path.isabs(filepath):
        raise exceptions.FileException("参数必须是绝对路径")

    filedir = filepath[0:filepath.rfind(os.sep)]
    if not os.path.isdir(filedir):
        try:
            os.makedirs(filedir)
            # log.log_info("创建文件夹成功:{}".format(path))
        except Exception as e:
            raise exceptions.FileException("执行os.makedirs失败,异常:{}".format(e))

    if not os.path.isfile(filepath):
        with open(filepath, mode='w', encoding='utf-8'):
            pass
    else:
        pass
Exemplo n.º 6
0
    def get_file_from_dir(file_dir, file_index=None):
        """
        功能:从一个文件夹内返回一个文件路径
        :param file_dir: 文件夹
        :param file_index: 文件索引(默认索引随机值,也可以指定索引)
        :return: 返回一个文件路径(绝对路径)
        """
        # TODO(dongjun): 如果随机从文件夹获取的还是文件夹,该如何返回?

        validator.check_file_isDir(file_dir)

        # 获取文件夹下内容列表
        dir_list = os.listdir(file_dir)

        if not dir_list:
            raise exceptions.FileException("文件夹内无文件:{}".format(file_dir))

        # 文件夹下的文件数量
        file_counts = len(dir_list)

        index_max = file_counts
        if file_index or file_index == 0:
            # 校验 file_index:必须是正整数
            validator.check_int_gt_zero(file_index)
            # 校验:file_index不能超过文件列表的最大长度
            if file_index > index_max:
                raise exceptions.FileException(
                    "文件索引溢出,期望最大索引:{},实际入参索引:{}".format(index_max, file_index))
            tmp_pic_name = dir_list[file_index - 1]

            if file_dir.endswith(os.sep):
                tmp_pic_path = file_dir + tmp_pic_name
            else:
                tmp_pic_path = file_dir + os.sep + tmp_pic_name
            return tmp_pic_path

        tmp_pic_name = random.choice(dir_list)
        if file_dir.endswith(os.sep):
            tmp_pic_path = file_dir + tmp_pic_name
        else:
            tmp_pic_path = file_dir + os.sep + tmp_pic_name
        return tmp_pic_path
Exemplo n.º 7
0
    def delete_file(filepath):
        """
        功能:如果文件存在就删除该文件
        注意:仅限于删除一个文件
        :param filepath: 文件路径
        :return:
        """

        if os.path.isfile(filepath):
            try:
                os.remove(filepath)
                log.log_info("删除文件成功:{}".format(filepath))
            except Exception as e:
                raise exceptions.FileException("无法删除此文件:{}".format(filepath))