FilesTreeIco_Tool/config.py
2024-07-31 22:30:23 +08:00

77 lines
2.5 KiB
Python

# -*- coding: UTF-8 -*-
import os
import chardet
log = 0 #是否打印日志
workPath = os.getcwd() #工作路径
# 读取配置文件
def read_config_file():
confList = [] #配置文件路径列表
# 遍历目录
for root, dirs, files in os.walk(workPath):
for file in files:
# 检查文件是否以.conf结尾
if file.endswith(".conf"):
if log:print(os.path.join(root, file))
confList.append(os.path.join(root, file))
return confList
# 处理目录结构
def config_dir_structure(confPath):
# 自动检测文件编码
with open(confPath, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
# 读取配置文件
with open(confPath, 'r', encoding=encoding) as f:
lines = f.readlines()
if lines:
# 处理目录结构
for line in lines:
root = {}
stack = [(root, -1)] # 栈存储元组 (当前节点, 当前缩进级别)
for line in lines:
if not line.strip():
continue
# 计算当前行的缩进级别
indent_level = len(line) - len(line.lstrip())
# 获取当前目录名称
directory_name = line.strip()
# 在栈中找到合适的父节点
while stack and stack[-1][1] >= indent_level:
stack.pop()
# 父节点是栈中的最后一个元素
parent, _ = stack[-1]
# 将当前目录添加到父节点的字典中
parent[directory_name] = {}
# 将当前目录和其缩进级别推送到栈中
stack.append((parent[directory_name], indent_level))
return root
else:
return None
# 创建目录结构
def create_folders(base_path, structure):
for folder, substructure in structure.items():
current_path = os.path.join(base_path, folder)
os.makedirs(current_path, exist_ok=True)
if isinstance(substructure, dict):
create_folders(current_path, substructure)
# 配置选择器
def run_config_selector(confList):
# 根据配置文件列表创建input选择器
print("请选择配置文件:")
for i, conf in enumerate(confList):
print(f"{i+1}. {conf}")
temp = input("请输入选项:")
# 整数
if temp.isdigit():
choice = int(temp)
if choice in range(1, len(confList)+1):
return confList[choice-1]
else:
return None