完成文件的过滤删除和排序

This commit is contained in:
estom
2024-01-18 22:48:33 +08:00
parent 5793ae5bd0
commit 468bbc3c3e
2 changed files with 68 additions and 168 deletions

View File

@@ -2,73 +2,68 @@ from configparser import ConfigParser
from os.path import splitext, basename, join, isdir, relpath, abspath
from os import listdir
base_dir = None
start_with = None
show_file = None
ignore_file_name = None
include_start_with = None
out_file_list = []
create_depth = -1
# docsify根目录
root_dir=/root/gitee/notes/blog
# 要处理的文件或文件夹
exclude_start_with=['_','*','.']
exclude_file = ['readme.md']
exclude_dir = ['.vscode','.git']
# 想要在几级目录生成文件,默认"0"表示在根目录生成,可以配合侧边栏折叠插件使用
create_depth=1
def read_config():
global base_dir, show_file, start_with, ignore_file_name, ReadmeFile, _sidebarFile, out_file_list, create_depth,include_start_with
cf = ConfigParser()
cf.read("config.ini", encoding='utf-8')
base_dir = cf.get("config", "base_dir")
start_with = cf.get("config", "ignore_start_with").split("|")
show_file = cf.get("config", "show_file").split('|')
ignore_file_name = cf.get("config", "ignore_file_name").split("|")
include_start_with = cf.get("config", "include_start_with").split("|")
out_file_list = cf.get("outFile", "eachFile").split("|")
create_depth = int(cf.get("outFile", "create_depth"))
def check_file_extension(file_path):
def good_file(base_path):
"""
检查文件后缀是否为指定的后缀
:param file_path: 文件路径
:return: 如果文件后缀为指定的后缀返回True否则返回False
"""
file_extension = splitext(file_path)[1]
if file_extension in show_file:
return True
else:
return False
def check_file_name_satified(file_path):
"""
获取文件名(不包括扩展名)
是否需要生成文件
1. 扩展名不是md的不生成
2. 不是README.md _sidebar.md。不生成
3. 在跳过列表里的不生成
:param file_path: 文件路径
:return: 文件名(不包括扩展名)
"""
file_name_with_extension = basename(file_path)
file_name = splitext(file_name_with_extension)[0]
if file_name[0] in start_with or file_name in ignore_file_name:
file_extension = splitext(base_path)[1]
if file_extension != '.md':
return False
if file_name[0] not in include_start_with:
base_name = os.path.basename(base_path)
if base_name.lower() in exclude_file:
return False
for item in exclude_start_with:
if base_name.starts_with(item):
return False
rel_path = relpath(root_dir, base_path)
for item in exclude_dir:
if rel_path.startswith(item):
return False
return True
def good_dir(base_path):
rel_path = relpath(root_dir, base_path)
for item in exclude_dir:
if rel_path.startswith(item):
return False
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
abspath = os.path.join(dirpath):
if good_file(abspath):
return True
return False
def build_next_level(base_path):
'''
创建下一级节点的目录_sidebar.md
todo:排除子目录下没有md文件的子目录
'''
print("build next level:"+root_path)
items = os.listdir(base_path)
print("build next level:"+base_path)
items = os.listdir(base_path).sort()
result = "\n"
for item in items:
abspath = os.path.join(base_path,item)
if isdir(abspath):
rel_path = relpath(root_dir, base_dir)
if isdir(abspath) and good_dir(abspath):
rel_path = relpath(root_dir, abspath)
readme_path = os.path.join(rel_path,"README.md")
result += "- [" + item + "](" + readme_path + ')\n'
@@ -83,8 +78,8 @@ def build_full_level(base_path):
'''
创建所有子节点的目录_sidebar.md
'''
print("build full path"+root_path)
result = deep_traverse(base_dir,'')
print("build full path:"+base_path)
result = deep_traverse(base_path,'')
if '' == result:
return
basename = os.path.basename(base_path)
@@ -93,19 +88,17 @@ def build_full_level(base_path):
f.write('## '+ basename + '\n')
f.write(result)
def deep_traverse(base_dir,prefix):
def deep_traverse(base_path,prefix):
'''
深度递归遍历
'''
if os.path.isfile(base_dir)
if base_dir.endwith('.md')
return build_md_item(prefix,base_dir)
else:
return ''
title = prefix + '- ' + os.path.basename(base_dir) + '\n'
if os.path.isfile(base_path):
return build_md_item(prefix,base_path)
title = prefix + '- ' + os.path.basename(base_path) + '\n'
result = ''
for items in os.listdir(base_dir):
result += deep_traverse(base_dir,prefix+' ')
for item in os.listdir(base_path).sort():
abspath = os.path.join(base_path,item)
result += deep_traverse(abspath,prefix+' ')
if '' == result:
return ''
return title + result
@@ -115,7 +108,9 @@ def build_readme_now(base_path):
创建当前节点的readme文件指向当前目录下的md文件。
深度小于depth+1 就要生成readme文件
'''
print("build readme now:"+base_dir)
if not good_dir(base_path):
return
print("build readme now:"+base_path)
readme_path = os.path.join(base_path, 'README.md')
basename = os.path.basename(base_path)
@@ -126,6 +121,7 @@ def build_readme_now(base_path):
f.write(build_md_items('',base_path))
def build_md_items(prefix,base_path):
'''
todo:排除前缀不符合需求的文件
@@ -133,112 +129,38 @@ def build_md_items(prefix,base_path):
items = os.listdir(base_path)
result = "\n"
for item in items:
abspath = join(root, item)
if abspath is not dir:
abspath = join(base_path, item)
if os.path.isfile(abspath):
result += build_md_item(prefix,item)
return result
def build_md_item(prefix,file_path):
if not good_file(file_path):
return ''
base_name = os.path.basename(file_path)
title = os.path.splitext(base_name)[0]
return prefix + "- [" + title + "](" + relpath(root_dir, base_dir) + ')\n'
return prefix + "- [" + title + "](" + relpath(root_dir, file_path) + ')\n'
def layer_traverse(root,now,depth):
build_readme_now(root)
def layer_traverse(base_path,now,depth):
build_readme_now(base_path)
'''
now=depth创建递归目录不再按层遍历
'''
if now >= depth:
build_full_level(root)
build_full_level(base_path)
return
'''
now<depth创建下层目录并继续递归
'''
build_next_level(root)
items = listdir(root)
build_next_level(base_path)
items = listdir(base_path)
for item in items:
node = join(root, item)
node = join(base_path, item)
if isdir(node):
layer_traverse(node,now+1,depth)
def save_structure(root_dir, base_dir=base_dir, depth=0):
"""
遍历指定目录及其所有子目录,生成并保存目录结构。
遍历方案按层遍历当前目录如果层数小于depth则只生成下一层的目录。如果等于depth则生成所有子节点的目录。
:param root_dir: 要处理的根目录路径
:param base_dir: 用来获得root_dir对base_dir的相对路径
:param depth: 递归深度文件夹深度。0表示根目录只在最开始生辰_sidebar1表示两级目录
"""
root = root_dir
dirs = []
files = []
i = 0
for item in listdir(root):
if isdir(join(root, item)):
dirs.append(item)
else:
files.append(item)
subdir_structure = ''
subdir_name = basename(root)
if depth != 0:
if create_depth == 0:
subdir_structure += "- " + subdir_name + '\n'
else:
subdir_structure += "- [" + subdir_name + "](" + relpath(root, base_dir) + ')\n'
else:
if create_depth == 0:
subdir_structure += "- " + "首页" + '\n'
else:
subdir_structure += "- [" + "首页" + "](" + relpath(root, base_dir) + ')\n'
for file in files:
if check_file_name_satified(join(root, file)):
if check_file_extension(file):
subdir_structure += " " + "- [" + file + "](" + relpath(join(root, file),
base_dir) + ')\n'
for subdir in dirs:
subdir_path = join(root, subdir)
if check_file_name_satified(subdir_path):
next_struct = save_structure(subdir_path, base_dir, depth + 1)
next_struct = next_struct[:-1] if next_struct.endswith("\n") else next_struct
next_struct = next_struct.replace("\n", "\n ") + "\n"
subdir_structure += " " + next_struct
back_struct = subdir_structure
if depth == 1:
subdir_structure = "- [" + "返回首页" + "](" + "" + '?id=main)\n' + subdir_structure
elif depth != 0:
abs_pre_path = abspath(join(root, ".."))
rel_pre_path = relpath(abs_pre_path, base_dir)
subdir_structure = "- [" + "返回上一级" + "](" + rel_pre_path + ')\n' + subdir_structure
subdir_structure = subdir_structure.replace('\\', '/')
print("%s : finished" % root_dir)
if create_depth == -1:
for file_name in out_file_list:
with open(join(root, file_name), 'w', encoding="utf-8") as f:
f.write(subdir_structure)
else:
if depth == 0 :
for file_name in out_file_list:
with open(join(root, file_name), 'w', encoding="utf-8") as f:
f.write(subdir_structure)
return back_struct
class Node:
pass
if __name__ == "__main__":
@@ -247,9 +169,4 @@ if __name__ == "__main__":
第n-1层会生成n层目录的_sidebar
第n层生成之后所有目录的_sidebar
'''
import os
read_config()
os.chdir(base_dir)
print("cwd:"+os.getcwd())
layer_traverse(base_dir,0,create_depth)
# save_structure(base_dir, base_dir, 0)
layer_traverse(root_dir,0,create_depth)

View File

@@ -1,17 +0,0 @@
[config]
# docsify根目录
base_dir=/root/gitee/notes/blog
# 忽略以“_”,"."开头的文件,如果要添加新文件,用“|”分隔
ignore_start_with=_|.
# 只读取".md"格式问价,如果添加新格式,用“|”分隔
show_file=.md
# 要忽略的文件名,要添加新文件,用“|”分隔
ignore_file_name=README
include_start_with=docker|java
[outFile]
# 想要在几级目录生成文件,默认"-1"表示所有文件夹生成,"0"表示在根目录生成,可以配合侧边栏折叠插件使用
create_depth=1
# 每个文件夹下主页文件名称和侧边栏文件名,默认README.md和_sidebar.md文件想生成其他名称可修改文字或者添加用“|”分隔
eachFile=_sidebar.md