def test_readForString(): file = "/tmp/test_readForString" assert fileu.readForString("") is None assert fileu.readForString("/") is None content = "" with open(file, "w") as f: f.write(content) assert fileu.readForString(file) == content content = " " with open(file, "w") as f: f.write(content) assert fileu.readForString(file) == content with open(file, "w") as f: f.write(" ") f.write("\n") f.write(" a 框架 ") assert fileu.readForString(file) == " \n a 框架 "
def test_size(): filepath = "/tmp/xxx" fileu.write(filepath, "ftext\r\n") assert fileu.size(filepath) == 7 assert fileu.size(filepath, readable=True) == f"{7 / 1024:.2f} KB" ftext = "" n_line = 1024 # 生成的文件的行数 for i in range(1024): ftext += f"{i:10d}\n" # 每行11个字符 text_size = 11 * n_line fileu.write(filepath, ftext) assert fileu.size(filepath) == text_size assert fileu.size(filepath, readable=True) == f"{text_size / 1024:.2f} KB"
def load_conf(filename: str): """ 读取 fdconfig 下的配置文件。 :param filename: 配置文件名(不需要路径) :return: 配置文件内容的字典。如果配置文件不存在,返回空字典对象 """ conf_file = fileu.cat_path(_HRS_FDCONFIG_ROOT, filename) if not os.path.isfile(conf_file): logger.warning(f"config file [{conf_file}] not exist !") return MappingProxyType(dict()) with open(conf_file, "r", encoding="utf8") as f: data = yaml.safe_load(f) return MappingProxyType(data)
def test_write(): filepath = "/tmp/xxx1" fileu.write(filepath, "") assert os.path.exists(filepath) fileu.write(filepath, "1\n ", append=True) fileu.write(filepath, " \n", append=True) with open(filepath) as f: lines = f.readlines() assert len(lines) == 2 assert lines[0] == "1\n" assert lines[1] == " \n"
import os from types import MappingProxyType import yaml from feedwork.utils import logger import feedwork.utils.FileHelper as fileu import feedwork.utils.System as sysu HRS_RESOURCES_ROOT = sysu.env("HRS_RESOURCES_ROOT", str) # 所有资源根目录 logger.info(f"[load config] HRS_RESOURCES_ROOT={HRS_RESOURCES_ROOT}") _HRS_FDCONFIG_ROOT = fileu.cat_path(HRS_RESOURCES_ROOT, "fdconfig", check_exist='dir') def load_conf(filename: str): """ 读取 fdconfig 下的配置文件。 :param filename: 配置文件名(不需要路径) :return: 配置文件内容的字典。如果配置文件不存在,返回空字典对象 """ conf_file = fileu.cat_path(_HRS_FDCONFIG_ROOT, filename) if not os.path.isfile(conf_file): logger.warning(f"config file [{conf_file}] not exist !") return MappingProxyType(dict()) with open(conf_file, "r", encoding="utf8") as f: data = yaml.safe_load(f) return MappingProxyType(data)
def test_isfile(): assert fileu.isfile("/etc/passwd") assert not fileu.isfile("/etc") assert not fileu.isfile("")
def test_linecount(): filepath = "/tmp/xxx" ftext = f"\nlksdjf\n88888888\n " fileu.write(filepath, ftext) assert fileu.linecount(filepath) == 4
def test_cat_path(): drive, path = os.path.splitdrive(fileu.cat_path("/abc/", "abc")) assert path == "{0}abc{0}abc".format(os.path.sep) drive, path = os.path.splitdrive(fileu.cat_path("/", "path")) assert path == "{0}path".format(os.path.sep) drive, path = os.path.splitdrive(fileu.cat_path("/", "")) assert path == os.path.sep drive, path = os.path.splitdrive(fileu.cat_path("/abc", "file.txt")) assert path == "{0}abc{0}file.txt".format(os.path.sep) drive, path = os.path.splitdrive( fileu.cat_path("/", "path", "sub", "file.md")) assert path == "{0}path{0}sub{0}file.md".format(os.path.sep) with pytest.raises(Exception): fileu.cat_path("/abc/", "../def") drive, path = os.path.splitdrive( fileu.cat_path("/", "usr", "local", check_exist='dir')) assert path == "{0}usr{0}local".format(os.path.sep) with pytest.raises(Exception): fileu.cat_path("/", "usr", "local", check_exist='file') drive, path = os.path.splitdrive( fileu.cat_path("/", "etc", "passwd", check_exist='file')) assert path == "{0}etc{0}passwd".format(os.path.sep) with pytest.raises(Exception): fileu.cat_path("/", "etc", "passwd", check_exist='dir')