diff --git a/core/api.py b/core/api.py index ad1a82e..29751ee 100644 --- a/core/api.py +++ b/core/api.py @@ -1,18 +1,9 @@ import json import os -import sys import requests - -def get_script_directory(): - """获取脚本所在的目录""" - if getattr(sys, "frozen", False): - # 如果是打包后的可执行文件 - return os.path.dirname(sys.executable) - else: - # 如果是普通脚本 - return "." +from core.common import get_script_directory class YoudaoNoteApi(object): diff --git a/core/common.py b/core/common.py new file mode 100644 index 0000000..c4a382d --- /dev/null +++ b/core/common.py @@ -0,0 +1,13 @@ +import os +import sys + + +def get_script_directory(): + """获取脚本所在的目录""" + + if getattr(sys, "frozen", False): + # 如果是打包后的可执行文件 + return os.path.dirname(sys.executable) + else: + # 如果是普通脚本 + return "." diff --git a/core/log.py b/core/log.py index fa12fa7..47789ce 100644 --- a/core/log.py +++ b/core/log.py @@ -3,20 +3,12 @@ import sys from datetime import datetime +from core.common import get_script_directory + LOG_FORMAT = "%(asctime)s %(levelname)s %(processName)s-%(threadName)s-%(thread)d %(filename)s:%(lineno)d %(funcName)-10s : %(message)s" DATE_FORMAT = "%Y/%m/%d %H:%M:%S " -def get_script_directory(): - """获取脚本所在的目录""" - if getattr(sys, "frozen", False): - # 如果是打包后的可执行文件 - return os.path.dirname(sys.executable) - else: - # 如果是普通脚本 - return "." - - def init_logging(): log_dir = os.path.join(get_script_directory(), "logs") os.makedirs(log_dir, exist_ok=True) diff --git a/pull.py b/pull.py index 74316ca..a56437c 100644 --- a/pull.py +++ b/pull.py @@ -18,6 +18,7 @@ from core import log from core.api import YoudaoNoteApi +from core.common import get_script_directory from core.covert import YoudaoNoteConvert from core.image import ImagePull @@ -41,16 +42,6 @@ class FileActionEnum(Enum): UPDATE = "更新" -def get_script_directory(): - """获取脚本所在的目录""" - if getattr(sys, "frozen", False): - # 如果是打包后的可执行文件 - return os.path.dirname(sys.executable) - else: - # 如果是普通脚本 - return os.path.dirname(os.path.abspath(__file__)) - - class YoudaoNotePull(object): """ 有道云笔记 Pull 封装 @@ -103,7 +94,7 @@ def _check_local_dir(self, local_dir, test_default_dir=None) -> Tuple[str, str]: if not local_dir: add_dir = test_default_dir if test_default_dir else "youdaonote" # 兼容 Windows 系统,将路径分隔符(\\)替换为 / - local_dir = os.path.join(os.getcwd(), add_dir).replace("\\", "/") + local_dir = os.path.join(get_script_directory(), add_dir).replace("\\", "/") # 如果指定的本地文件夹不存在,创建文件夹 if not os.path.exists(local_dir):