Skip to content

Commit

Permalink
fix:wrong file path in mac environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Monomux committed Sep 11, 2022
1 parent b8fe6f5 commit c0a2b1c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
7 changes: 6 additions & 1 deletion KeymouseGo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def add_lib_path(libpaths):
sys.path.append(libpath)


add_lib_path([os.path.join(os.getcwd(), 'plugins')])
def to_abs_path(*args):
return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
*args)


add_lib_path([os.path.join(to_abs_path('plugins'))])


def resize_layout(ui, ratio_w, ratio_h):
Expand Down
5 changes: 3 additions & 2 deletions UIFileDialogFunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from UIFileManageDialogView import Ui_Dialog
from UIFunc import scripts, scripts_map

from KeymouseGo import to_abs_path

class FileDialog(Ui_Dialog):
def __init__(self):
Expand All @@ -20,7 +21,7 @@ def __init__(self):
self.main_window = QMainWindow()
self.filename = scripts[scripts_map['current_index']]
self.lineEdit.setText(self.filename)
self.path = os.path.join(os.getcwd(), "scripts")
self.path = os.path.join(to_abs_path("scripts"))
i18n_language = {
'简体中文': ['文件管理', '当前文件', '选择文件', '编辑脚本', '重命名', '文件没有被找到', '请输入新文件名: ', '更新成功', '文件名不能为空或空格'],
'English': ['File Manage', 'Current file', 'Choice', 'Edit', 'Rename', 'File not found', 'Please input new name', 'Success', 'File name cannot be empty or space']
Expand All @@ -35,7 +36,7 @@ def __init__(self):


def choice_file(self):
file = QFileDialog.getOpenFileName(self.main_window, "选择文件", dir='scripts', filter='*.txt')[0]
file = QFileDialog.getOpenFileName(self.main_window, "选择文件", dir=to_abs_path('scripts'), filter='*.txt')[0]
file_name = re.split(r'\\|\/', file)[-1]
if file_name.strip() != '' and file_name is not None:
self.lineEdit.setText(file_name)
Expand Down
26 changes: 14 additions & 12 deletions UIFunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from UIView import Ui_UIView
from assets.plugins.ProcessException import *

from KeymouseGo import to_abs_path

os.environ['QT_ENABLE_HIGHDPI_SCALING'] = "1"
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)

Expand All @@ -36,7 +38,7 @@
logger.remove()
logger.add(sys.stdout, backtrace=True, diagnose=True,
level='DEBUG')
logger.add('logs/{time}.log', rotation='20MB', backtrace=True, diagnose=True,
logger.add(to_abs_path('logs', '{time}.log'), rotation='20MB', backtrace=True, diagnose=True,
level='INFO')


Expand All @@ -56,9 +58,9 @@ def get_assets_path(*paths):
def get_script_list_from_dir():
global scripts

if not os.path.exists('scripts'):
os.mkdir('scripts')
scripts = os.listdir('scripts')[::-1]
if not os.path.exists(to_abs_path('scripts')):
os.mkdir(to_abs_path('scripts'))
scripts = os.listdir(to_abs_path('scripts'))[::-1]
scripts = list(filter(lambda s: s.endswith('.txt'), scripts))


Expand Down Expand Up @@ -102,9 +104,9 @@ def __init__(self, app):
self.choice_script.setCurrentIndex(0)

self.choice_extension.addItems(['Extension'])
if not os.path.exists('plugins'):
os.mkdir('plugins')
for i in os.listdir('plugins'):
if not os.path.exists(to_abs_path('plugins')):
os.mkdir(to_abs_path('plugins'))
for i in os.listdir(to_abs_path('plugins')):
if i[-3:] == '.py':
self.choice_extension.addItems([i[:-3]])

Expand Down Expand Up @@ -329,8 +331,8 @@ def closeEvent(self, event):
event.accept()

def loadconfig(self):
if not os.path.exists('config.ini'):
with open('config.ini', 'w', encoding='utf-8') as f:
if not os.path.exists(to_abs_path('config.ini')):
with open(to_abs_path('config.ini'), 'w', encoding='utf-8') as f:
f.write('[Config]\n'
'StartHotKeyIndex=3\n'
'StopHotKeyIndex=6\n'
Expand All @@ -341,14 +343,14 @@ def loadconfig(self):
'Language=zh-cn\n'
'Extension=Extension\n'
'Theme=light_cyan_500.xml\n')
return QSettings('config.ini', QSettings.IniFormat)
return QSettings(to_abs_path('config.ini'), QSettings.IniFormat)

def get_script_path(self):
i = self.choice_script.currentIndex()
if i < 0:
return ''
script = self.scripts[i]
path = os.path.join(os.getcwd(), 'scripts', script)
path = os.path.join(to_abs_path('scripts'), script)
logger.info('Script path: {0}'.format(path))
return path

Expand Down Expand Up @@ -554,7 +556,7 @@ def getextension(cls, module_name='Extension', runtimes=1, speed=100, thd=None,
module = SourceFileLoader(module_name, get_assets_path('plugins', 'Extension.py')).load_module()
else:
module = SourceFileLoader(module_name,
os.path.join(os.getcwd(), 'plugins', '%s.py' % module_name)).load_module()
os.path.join(to_abs_path('plugins', '%s.py' % module_name))).load_module()
module_cls = getattr(module, module_name)
logger.info('Load plugin class {0} in module {1}'.format(module_cls, module_name))
return module_cls(runtimes, speed, thd, swap)
Expand Down

0 comments on commit c0a2b1c

Please sign in to comment.