Skip to content

Commit

Permalink
style: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
DeppWang committed Jun 13, 2024
1 parent 517916d commit 13d065a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
11 changes: 1 addition & 10 deletions core/api.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
13 changes: 13 additions & 0 deletions core/common.py
Original file line number Diff line number Diff line change
@@ -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 "."
12 changes: 2 additions & 10 deletions core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 2 additions & 11 deletions pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 封装
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 13d065a

Please sign in to comment.