From e7edce456a6d8925ad4fdf6a996742463c1eaf50 Mon Sep 17 00:00:00 2001 From: horsesword Date: Thu, 21 Oct 2021 23:19:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=B3=E9=94=AE=E6=96=B0=E5=BB=BA=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B9=E4=BE=BF=E5=9C=B0=E9=80=89=E6=8B=A9=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E7=B1=BB=E5=9E=8B=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- my_scripts/markdown/MarkdownRel.py | 140 +++++++++++++++++++++++++++++ tagdox.py | 86 ++++++++++++------ 2 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 my_scripts/markdown/MarkdownRel.py diff --git a/my_scripts/markdown/MarkdownRel.py b/my_scripts/markdown/MarkdownRel.py new file mode 100644 index 0000000..e27f1b3 --- /dev/null +++ b/my_scripts/markdown/MarkdownRel.py @@ -0,0 +1,140 @@ +""" +本文件用于处理markdown文件的移动问题,尤其是对于相对路径的移动有一定的自动化处理能力。 +思路是:如果有md文件需要移动/复制的话, +- 检索md文件中的绝对链接和相对链接; +- 将这些相对链接的文件放在新的目标位置; + - 如果目标位置已经有同名文件+同文件大小,跳过;否则提醒是跳过、保留且修改还是怎样。 +""" +import re +import os +import shutil + + +def find_links(filepath: str): + """ + 查找md文件中的超链接,并返回绝对链接和相对链接(列表)。 + 其中的相对路径开头没有斜杠,所以要手动补充斜杠获取绝对路径。 + + :param filepath: 文件完整路径 + + :return: Tuple(res_abs,res_rel): 绝对链接(列表),相对链接(列表) + + """ + + pattern1 = re.compile(r'= 0: + if str(pattern_head.findall(p)[0]).find(':') >= 0: + res_abs.append(p) + else: + res_rel.append(p) + else: + res_rel.append(p) + except Exception as e: + print(p) + print(e) + + return res_abs, res_rel + + +def copy_md_linked_files(file_path_old: str, new_path, mode='copy'): + """ + + :param file_path_old: md文件所在完整路径(包括文件名) + :param new_path: md文件目标位置(文件夹) + :param mode: 移动或者复制 + :return: + """ + lst_links_abs, lst_links_rel = find_links(file_path_old) + fpath, fname = os.path.split(file_path_old) + + for p in lst_links_rel: + old_pth = fpath.replace('\\', '/') + '/' + p # 原位置 + print(old_pth) + tar_pth = new_path.replace('\\', '/') + if tar_pth.endswith('/'): + tar_pth = tar_pth + p + else: + tar_pth = tar_pth + '/' + p + print(tar_pth) + tarp, tarfn = os.path.split(tar_pth) + if not os.path.exists(tarp): + os.makedirs(tarp) + try: + shutil.copyfile(old_pth, tar_pth) # 目前,附件永远是复制,因为移动可能存在其他问题 + + except Exception as e: + print(e) + + +def copy_md(file_path_old: str, file_path_new: str, mode='copy'): + """ + 复制md文件到指定位置。 + + :param file_path_old: + :param file_path_new: 目标位置(文件夹 + 文件名) + :param mode: 移动或者复制 + :return: 无 + """ + new_path, new_filename = os.path.split(file_path_new) + copy_md_linked_files(file_path_old, new_path) + + if len(new_filename) == 0: + fpath, fname = os.path.split(file_path_old) + new_full_path = new_path + '/' + fname + else: + new_full_path = file_path_new + + try: + if mode == 'copy': + shutil.copyfile(file_path_old, new_full_path) + elif mode == 'move': + shutil.move(file_path_old, new_full_path) + except Exception as e: + print('复制文件出错:') + print(e) + + +if __name__ == '__main__': + file_old = r"D:\MaJian\Desktop\@短文剪辑\Python 图像库 PIL 的类 Image 及其方法介绍_leemboy 的博客 - CSDN 博客_pil.md" + file_new = r'd:/to_delete/a/b/c/d\\e/PIL.md' + # pth_abs, pth_rel = find_links(fip) + # print(pth_abs) + # print(pth_rel) + # + # copy_md_linked_files(fip, 'd:/to_delete') + copy_md(file_old, file_new) diff --git a/tagdox.py b/tagdox.py index b66d633..02afb36 100644 --- a/tagdox.py +++ b/tagdox.py @@ -40,6 +40,8 @@ from my_scripts.widgets.my_tk_widgets import my_progress_window from my_scripts.widgets.my_tk_widgets import my_input_window from my_scripts.widgets.my_tk_widgets import my_space_window +from my_scripts.markdown import MarkdownRel # 对 markdown 的特殊处理 + # import my_logger # import send2trash # 回收站(目前作废) @@ -47,10 +49,13 @@ URL_HELP = 'https://gitee.com/horse_sword/my-local-library' # 帮助的超链 URL_ADV = 'https://gitee.com/horse_sword/my-local-library/issues' # 提建议的位置 URL_CHK_UPDATE = 'https://gitee.com/horse_sword/my-local-library/releases' # 检查更新的位置 TAR = 'Tagdox / 标签文库' # 程序名称 -VER = 'v0.21.1.2' # 版本号 +VER = 'v0.21.2.0' # 版本号 """ ## 近期更新说明 +#### v0.21.2.0 2021年10月21日 +右键新建笔记的时候,可以更方便地选择笔记类型了。 + #### v0.21.1.2 2021年10月6日 修复了在非NTFS磁盘上的兼容性bug。 文件夹区域也增加了鼠标指向效果。 @@ -4493,6 +4498,14 @@ def show_popup_menu_file(event): # menu_tags_to_drop = tk.Menu(window, tearoff=0) menu_tags_to_add = tk.Menu(window, tearoff=0) + menu_create_note = tk.Menu(window, tearoff=0) # 新建笔记 + + menu_create_note.add_command(label='.docx', command=lambda x=1: exec_create_note(None, '.docx')) + menu_create_note.add_command(label='.md', command=lambda x=1: exec_create_note(None, '.md')) + menu_create_note.add_command(label='.txt', command=lambda x=1: exec_create_note(None, '.txt')) + menu_create_note.add_command(label='.rtf', command=lambda x=1: exec_create_note(None, '.rtf')) + + # if len(QUICK_TAGS) > 0: for i in QUICK_TAGS: menu_tags_to_add.add_command(label=i, command=lambda x=i: exec_fast_add_tag(x)) @@ -4505,6 +4518,7 @@ def show_popup_menu_file(event): menu_file.add_separator() if len(lst_my_path_long_selected) == 1: menu_file.add_command(label="新建笔记", command=exec_create_note, accelerator='Ctrl+N') + menu_file.add_cascade(label="新建更多格式的笔记", menu=menu_create_note) else: menu_file.add_command(label="新建笔记", state=tk.DISABLED, command=exec_create_note, accelerator='Ctrl+N') menu_file.add_separator() @@ -4549,6 +4563,7 @@ def show_popup_menu_file(event): menu_file_no_selection.add_separator() if len(lst_my_path_long_selected) == 1: menu_file_no_selection.add_command(label="新建笔记", command=exec_create_note, accelerator='Ctrl+N') + menu_file_no_selection.add_cascade(label="新建更多格式的笔记", menu=menu_create_note) else: menu_file_no_selection.add_command(label="新建笔记", state=tk.DISABLED, command=exec_create_note, accelerator='Ctrl+N') @@ -5684,42 +5699,55 @@ class main_app: ########################################################### +########################################################### +# 主程序开始 +########################################################### +########################################################### -# 检查是否已经运行; -''' -import win32gui -import win32con -wd_name = TAR + ' ' + VER -pr_name = '我的文库.exe' -have_exe = 0 -try: - win =win32gui.FindWindow(wd_name,None) - print(win) - if win: - have_exe = 1 - win.ShowWindow(win32con.SW_SHOWNORMAL) - print('\n已经存在打开的实例\n') - else: - print('\n不存在打开的实例\n') -except Exception as e: - print(e) + +""" +def check_single_instance(): # 检查是否已经运行; + import win32gui + import win32com.client + import sys + + wd_name = TAR + ' ' + VER + pr_name = 'tagdox' have_exe = 0 + try: + # win32gui.GetWindow() + shell = win32com.client.Dispatch("WScript.Shell") # 未找到函数 + shell.AppActivate(wd_name) + # print('\n已经存在打开的实例\n') + have_exe = 1 + + win = win32gui.FindWindow(None,wd_name) + print(win) + if win: + have_exe = 1 + root = tk.Tk() + t = tk.messagebox.showerror(title='ERROR', + message='本程序已经在运行。') + root.destroy() + # win.ShowWindow(win32con.SW_SHOWNORMAL) + print('\n已经存在打开的实例\n') + else: + print('\n不存在打开的实例\n') + except Exception as e: + print(e) + have_exe = 0 + return have_exe +""" # from tendo import singleton # me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running -''' + +# have_exe = check_single_instance() +# if __name__ == '__main__' and have_exe==0: if __name__ == '__main__': - # if True: # 变量 ########################################################### - # from tendo import singleton - # import sys - # try: - # me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running - # except: - # t = tk.messagebox.showerror(title='ERROR', - # message='文件夹重命名失败,可能是有内部文件正在被访问,或没有操作权限。') - # sys.exit(-1) + # # q = queue.Queue() # -- GitLab