python [重构] PyCopy,来自:https://www.reddit.com/r/Python/comments/66q7xl/pycopy_a_python_script_need_fe
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [重构] PyCopy,来自:https://www.reddit.com/r/Python/comments/66q7xl/pycopy_a_python_script_need_fe相关的知识,希望对你有一定的参考价值。
media_path: "C:\\TEMP\\PyCopy - Output"
file_types:
music:
extensions: ["mp3", "wav"]
directory: "Music"
movie:
extensions: ["mp4", "mkv", "avi", "mpg", "mpeg"]
directory: "Movies"
image:
extensions: ["jpg", "jpeg", "gif", "png"]
directory: "Images"
import os
import shutil
import sys
import yaml
def main():
settings = load_yaml("settings.yaml")
copied_files, existing_files, invalid_filetypes, invalid_files = ([], [], [], [])
for file in sys.argv[1:]:
directory, file_base = os.path.split(file)
try:
validate_file(file, file_base)
copy_file_to_filetype_directory(settings, file, file_base)
copied_files.append(file)
except File_Exists_Error: existing_files.append(file)
except Invalid_FileType_Error: invalid_filetypes.append(file)
except Invalid_File_Error: invalid_files.append(file)
print_status_report(copied_files, existing_files, invalid_filetypes, invalid_files)
class Invalid_File_Error(Exception): pass
class Invalid_FileType_Error(Exception): pass
class File_Exists_Error(Exception): pass
def load_yaml(file):
with open(file, "r") as file_text:
return yaml.load(file_text)
def validate_file(file, file_base):
if not os.path.exists(file) \
or not os.path.isfile(file) \
or not os.extsep in file_base:
raise Invalid_File_Error
def copy_file_to_filetype_directory(settings, file, file_base):
extension = file_base.split(os.extsep)[-1]
for file_type in settings["file_types"]:
if extension.lower() in settings["file_types"][file_type]["extensions"]:
new_path = get_filetype_path(settings, file_type)
copy_file(file, file_base, new_path)
break
else:
raise Invalid_FileType_Error
def get_filetype_path(settings, file_type):
media_path = settings["media_path"]
directory = settings["file_types"][file_type]["directory"]
new_path = os.path.join(media_path, directory)
os.makedirs(new_path, exist_ok=True)
return(new_path)
def copy_file(file, file_base, new_path):
new_file = os.path.join(new_path, file_base)
if os.path.exists(new_file):
raise File_Exists_Error
else:
shutil.copy(file, new_path)
def print_status_report(copied_files, existing_files, invalid_filetypes, invalid_files):
def print_section(header, files):
print(
f"\n{header}",
"\n".join([f"\t{x}" for x in files]),
sep="\n",
)
if(copied_files ): print_section("Successfully Copied:", copied_files )
if(existing_files ): print_section("ERROR: EXISTING FILES:", existing_files )
if(invalid_filetypes): print_section("ERROR: INVALID FILETYPES:", invalid_filetypes)
if(invalid_files ): print_section("ERROR: INVALID FILES:", invalid_files )
main()
以上是关于python [重构] PyCopy,来自:https://www.reddit.com/r/Python/comments/66q7xl/pycopy_a_python_script_need_fe的主要内容,如果未能解决你的问题,请参考以下文章
php Laravel服务类,来自Controller的重构逻辑
代码重构帮助 - 如何重新组织验证
谈谈代码重构
译重构:这个类太庞大了
高光谱图像重构常用评价指标及其Python实现
python类继承与重构