mirror of https://github.com/jumpserver/jumpserver
perf: 优化applet-navicat连接方式 (#9498)
* perf: 优化applet-navicat连接方式 * perf: 关闭许可证通知,不阻塞登录pull/9494/head^2
parent
e36506c3b8
commit
e647205c24
|
@ -7,9 +7,11 @@ if sys.platform == 'win32':
|
||||||
import win32api
|
import win32api
|
||||||
|
|
||||||
from pywinauto import Application
|
from pywinauto import Application
|
||||||
from pywinauto.controls.uia_controls import (
|
from pywinauto.controls.uia_controls import ButtonWrapper
|
||||||
EditWrapper, ComboBoxWrapper, ButtonWrapper
|
from pywinauto.keyboard import send_keys
|
||||||
)
|
|
||||||
|
import const as c
|
||||||
|
|
||||||
from common import wait_pid, BaseApplication
|
from common import wait_pid, BaseApplication
|
||||||
|
|
||||||
_default_path = r'C:\Program Files\PremiumSoft\Navicat Premium 16\navicat.exe'
|
_default_path = r'C:\Program Files\PremiumSoft\Navicat Premium 16\navicat.exe'
|
||||||
|
@ -30,17 +32,16 @@ class AppletApplication(BaseApplication):
|
||||||
self.app = None
|
self.app = None
|
||||||
|
|
||||||
def clean_up(self):
|
def clean_up(self):
|
||||||
protocol_mapping = {
|
protocols = (
|
||||||
'mariadb': 'NavicatMARIADB', 'mongodb': 'NavicatMONGODB',
|
'NavicatMARIADB', 'NavicatMONGODB', 'Navicat',
|
||||||
'mysql': 'Navicat', 'oracle': 'NavicatORA',
|
'NavicatORA', 'NavicatMSSQL', 'NavicatPG'
|
||||||
'sqlserver': 'NavicatMSSQL', 'postgresql': 'NavicatPG'
|
)
|
||||||
}
|
for p in protocols:
|
||||||
protocol_display = protocol_mapping.get(self.protocol, 'mysql')
|
sub_key = r'Software\PremiumSoft\%s\Servers' % p
|
||||||
sub_key = r'Software\PremiumSoft\%s\Servers' % protocol_display
|
try:
|
||||||
try:
|
win32api.RegDeleteTree(winreg.HKEY_CURRENT_USER, sub_key)
|
||||||
win32api.RegDeleteTree(winreg.HKEY_CURRENT_USER, sub_key)
|
except Exception:
|
||||||
except Exception as err:
|
pass
|
||||||
print('Error: %s' % err)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def launch():
|
def launch():
|
||||||
|
@ -51,135 +52,208 @@ class AppletApplication(BaseApplication):
|
||||||
winreg.SetValueEx(key, 'AlreadyShowNavicatV16WelcomeScreen', 0, winreg.REG_DWORD, 1)
|
winreg.SetValueEx(key, 'AlreadyShowNavicatV16WelcomeScreen', 0, winreg.REG_DWORD, 1)
|
||||||
# 禁止开启自动检查更新
|
# 禁止开启自动检查更新
|
||||||
winreg.SetValueEx(key, 'AutoCheckUpdate', 0, winreg.REG_DWORD, 0)
|
winreg.SetValueEx(key, 'AutoCheckUpdate', 0, winreg.REG_DWORD, 0)
|
||||||
|
# 禁止弹出初始化界面
|
||||||
winreg.SetValueEx(key, 'ShareUsageData', 0, winreg.REG_DWORD, 0)
|
winreg.SetValueEx(key, 'ShareUsageData', 0, winreg.REG_DWORD, 0)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print('Launch error: %s' % err)
|
print('Launch error: %s' % err)
|
||||||
|
|
||||||
def _fill_to_mysql(self, app, menu, protocol_display='MySQL'):
|
@staticmethod
|
||||||
menu.item_by_path('File->New Connection->%s' % protocol_display).click_input()
|
def _exec_commands(commands):
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
for command in commands:
|
||||||
|
if command['type'] == 'key':
|
||||||
|
time.sleep(0.5)
|
||||||
|
send_keys(' '.join(command['commands']))
|
||||||
|
elif command['type'] == 'action':
|
||||||
|
for f in command['commands']:
|
||||||
|
f()
|
||||||
|
|
||||||
name_ele = conn_window.child_window(best_match='Edit5')
|
def _action_not_remember_password(self):
|
||||||
EditWrapper(name_ele.element_info).set_edit_text(self.name)
|
conn_window = self.app.window(best_match='Dialog'). \
|
||||||
|
child_window(title_re='New Connection')
|
||||||
|
remember_checkbox = conn_window.child_window(best_match='Save password')
|
||||||
|
remember_checkbox.click()
|
||||||
|
|
||||||
host_ele = conn_window.child_window(best_match='Edit4')
|
def _get_mysql_commands(self):
|
||||||
EditWrapper(host_ele.element_info).set_edit_text(self.host)
|
commands = [
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
'%f', c.DOWN, c.RIGHT, c.ENTER
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
self.name, c.TAB, self.host, c.TAB,
|
||||||
|
str(self.port), c.TAB, self.username,
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'action',
|
||||||
|
'commands': [
|
||||||
|
self._action_not_remember_password
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return commands
|
||||||
|
|
||||||
port_ele = conn_window.child_window(best_match='Edit2')
|
def _get_mariadb_commands(self):
|
||||||
EditWrapper(port_ele.element_info).set_edit_text(self.port)
|
commands = [
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
'%f', c.DOWN, c.RIGHT, c.DOWN * 5, c.ENTER,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
self.name, c.TAB, self.host, c.TAB,
|
||||||
|
str(self.port), c.TAB, self.username
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'action',
|
||||||
|
'commands': [
|
||||||
|
self._action_not_remember_password
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return commands
|
||||||
|
|
||||||
username_ele = conn_window.child_window(best_match='Edit1')
|
def _get_mongodb_commands(self):
|
||||||
EditWrapper(username_ele.element_info).set_edit_text(self.username)
|
commands = [
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
'%f', c.DOWN, c.RIGHT, c.DOWN * 6, c.ENTER,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
self.name, c.TAB * 3, self.host, c.TAB, str(self.port),
|
||||||
|
c.TAB, c.DOWN, c.TAB, self.db, c.TAB, self.username,
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'action',
|
||||||
|
'commands': [
|
||||||
|
self._action_not_remember_password
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return commands
|
||||||
|
|
||||||
password_ele = conn_window.child_window(best_match='Edit3')
|
def _get_postgresql_commands(self):
|
||||||
EditWrapper(password_ele.element_info).set_edit_text(self.password)
|
commands = [
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
'%f', c.DOWN, c.RIGHT, c.DOWN, c.ENTER,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
self.name, c.TAB, self.host, c.TAB, str(self.port),
|
||||||
|
c.TAB, self.db, c.TAB, self.username
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'action',
|
||||||
|
'commands': [
|
||||||
|
self._action_not_remember_password
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return commands
|
||||||
|
|
||||||
def _fill_to_mariadb(self, app, menu):
|
def _get_sqlserver_commands(self):
|
||||||
self._fill_to_mysql(app, menu, 'MariaDB')
|
commands = [
|
||||||
|
{
|
||||||
def _fill_to_mongodb(self, app, menu):
|
'type': 'key',
|
||||||
menu.item_by_path('File->New Connection->MongoDB').click_input()
|
'commands': [
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
'%f', c.DOWN, c.RIGHT, c.DOWN * 4, c.ENTER,
|
||||||
|
],
|
||||||
auth_type_ele = conn_window.child_window(best_match='ComboBox2')
|
},
|
||||||
ComboBoxWrapper(auth_type_ele.element_info).select('Password')
|
{
|
||||||
|
'type': 'key',
|
||||||
name_ele = conn_window.child_window(best_match='Edit5')
|
'commands': [
|
||||||
EditWrapper(name_ele.element_info).set_edit_text(self.name)
|
self.name, c.TAB, '%s,%s' % (self.host, self.port),
|
||||||
|
c.TAB * 2, self.db, c.TAB * 2, self.username
|
||||||
host_ele = conn_window.child_window(best_match='Edit4')
|
]
|
||||||
EditWrapper(host_ele.element_info).set_edit_text(self.host)
|
},
|
||||||
|
{
|
||||||
port_ele = conn_window.child_window(best_match='Edit2')
|
'type': 'action',
|
||||||
EditWrapper(port_ele.element_info).set_edit_text(self.port)
|
'commands': [
|
||||||
|
self._action_not_remember_password
|
||||||
db_ele = conn_window.child_window(best_match='Edit6')
|
]
|
||||||
EditWrapper(db_ele.element_info).set_edit_text(self.db)
|
},
|
||||||
|
{
|
||||||
username_ele = conn_window.child_window(best_match='Edit1')
|
'type': 'key',
|
||||||
EditWrapper(username_ele.element_info).set_edit_text(self.username)
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
password_ele = conn_window.child_window(best_match='Edit3')
|
]
|
||||||
EditWrapper(password_ele.element_info).set_edit_text(self.password)
|
return commands
|
||||||
|
|
||||||
def _fill_to_postgresql(self, app, menu):
|
|
||||||
menu.item_by_path('File->New Connection->PostgreSQL').click_input()
|
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
|
||||||
|
|
||||||
name_ele = conn_window.child_window(best_match='Edit6')
|
|
||||||
EditWrapper(name_ele.element_info).set_edit_text(self.name)
|
|
||||||
|
|
||||||
host_ele = conn_window.child_window(best_match='Edit5')
|
|
||||||
EditWrapper(host_ele.element_info).set_edit_text(self.host)
|
|
||||||
|
|
||||||
port_ele = conn_window.child_window(best_match='Edit2')
|
|
||||||
EditWrapper(port_ele.element_info).set_edit_text(self.port)
|
|
||||||
|
|
||||||
db_ele = conn_window.child_window(best_match='Edit4')
|
|
||||||
EditWrapper(db_ele.element_info).set_edit_text(self.db)
|
|
||||||
|
|
||||||
username_ele = conn_window.child_window(best_match='Edit1')
|
|
||||||
EditWrapper(username_ele.element_info).set_edit_text(self.username)
|
|
||||||
|
|
||||||
password_ele = conn_window.child_window(best_match='Edit3')
|
|
||||||
EditWrapper(password_ele.element_info).set_edit_text(self.password)
|
|
||||||
|
|
||||||
def _fill_to_sqlserver(self, app, menu):
|
|
||||||
menu.item_by_path('File->New Connection->SQL Server').click_input()
|
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
|
||||||
|
|
||||||
name_ele = conn_window.child_window(best_match='Edit5')
|
|
||||||
EditWrapper(name_ele.element_info).set_edit_text(self.name)
|
|
||||||
|
|
||||||
host_ele = conn_window.child_window(best_match='Edit4')
|
|
||||||
EditWrapper(host_ele.element_info).set_edit_text('%s,%s' % (self.host, self.port))
|
|
||||||
|
|
||||||
db_ele = conn_window.child_window(best_match='Edit3')
|
|
||||||
EditWrapper(db_ele.element_info).set_edit_text(self.db)
|
|
||||||
|
|
||||||
username_ele = conn_window.child_window(best_match='Edit6')
|
|
||||||
EditWrapper(username_ele.element_info).set_edit_text(self.username)
|
|
||||||
|
|
||||||
password_ele = conn_window.child_window(best_match='Edit2')
|
|
||||||
EditWrapper(password_ele.element_info).set_edit_text(self.password)
|
|
||||||
|
|
||||||
def _fill_to_oracle(self, app, menu):
|
|
||||||
menu.item_by_path('File->New Connection->Oracle').click_input()
|
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
|
||||||
|
|
||||||
name_ele = conn_window.child_window(best_match='Edit6')
|
|
||||||
EditWrapper(name_ele.element_info).set_edit_text(self.name)
|
|
||||||
|
|
||||||
host_ele = conn_window.child_window(best_match='Edit5')
|
|
||||||
EditWrapper(host_ele.element_info).set_edit_text(self.host)
|
|
||||||
|
|
||||||
port_ele = conn_window.child_window(best_match='Edit3')
|
|
||||||
EditWrapper(port_ele.element_info).set_edit_text(self.port)
|
|
||||||
|
|
||||||
db_ele = conn_window.child_window(best_match='Edit2')
|
|
||||||
EditWrapper(db_ele.element_info).set_edit_text(self.db)
|
|
||||||
|
|
||||||
username_ele = conn_window.child_window(best_match='Edit')
|
|
||||||
EditWrapper(username_ele.element_info).set_edit_text(self.username)
|
|
||||||
|
|
||||||
password_ele = conn_window.child_window(best_match='Edit4')
|
|
||||||
EditWrapper(password_ele.element_info).set_edit_text(self.password)
|
|
||||||
|
|
||||||
|
def _get_oracle_commands(self):
|
||||||
|
commands = [
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
'%f', c.DOWN, c.RIGHT, c.DOWN * 2, c.ENTER,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [
|
||||||
|
self.name, c.TAB * 2, self.host, c.TAB,
|
||||||
|
str(self.port), c.TAB, self.db, c.TAB, c.TAB, self.username,
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'action',
|
||||||
|
'commands': (self._action_not_remember_password,)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': [c.ENTER]
|
||||||
|
}
|
||||||
|
]
|
||||||
if self.privileged:
|
if self.privileged:
|
||||||
conn_window.child_window(best_match='Advanced', control_type='TabItem').click_input()
|
commands.insert(3, {
|
||||||
role_ele = conn_window.child_window(best_match='ComboBox2')
|
'type': 'key',
|
||||||
ComboBoxWrapper(role_ele.element_info).select('SYSDBA')
|
'commands': (c.TAB * 4, c.RIGHT, c.TAB * 3, c.DOWN)
|
||||||
|
})
|
||||||
|
return commands
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.launch()
|
self.launch()
|
||||||
app = Application(backend='uia')
|
self.app = Application(backend='uia')
|
||||||
work_dir = os.path.dirname(self.path)
|
work_dir = os.path.dirname(self.path)
|
||||||
app.start(self.path, work_dir=work_dir)
|
self.app.start(self.path, work_dir=work_dir)
|
||||||
self.pid = app.process
|
self.pid = self.app.process
|
||||||
|
|
||||||
# 检测是否为试用版本
|
# 检测是否为试用版本
|
||||||
try:
|
try:
|
||||||
trial_btn = app.top_window().child_window(
|
trial_btn = self.app.top_window().child_window(
|
||||||
best_match='Trial', control_type='Button'
|
best_match='Trial', control_type='Button'
|
||||||
)
|
)
|
||||||
ButtonWrapper(trial_btn.element_info).click()
|
ButtonWrapper(trial_btn.element_info).click()
|
||||||
|
@ -187,26 +261,27 @@ class AppletApplication(BaseApplication):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
menubar = app.window(best_match='Navicat Premium', control_type='Window') \
|
# 根据协议获取相应操作命令
|
||||||
.child_window(best_match='Menu', control_type='MenuBar')
|
action = getattr(self, '_get_%s_commands' % self.protocol, None)
|
||||||
|
|
||||||
file = menubar.child_window(best_match='File', control_type='MenuItem')
|
|
||||||
file.click_input()
|
|
||||||
menubar.item_by_path('File->New Connection').click_input()
|
|
||||||
|
|
||||||
# 根据协议选择动作
|
|
||||||
action = getattr(self, '_fill_to_%s' % self.protocol, None)
|
|
||||||
if action is None:
|
if action is None:
|
||||||
raise ValueError('This protocol is not supported: %s' % self.protocol)
|
raise ValueError('This protocol is not supported: %s' % self.protocol)
|
||||||
action(app, menubar)
|
commands = action()
|
||||||
|
# 关闭掉桌面许可弹框
|
||||||
conn_window = app.window(best_match='Dialog').child_window(title_re='New Connection')
|
commands.insert(0, {'type': 'key', 'commands': (c.ENTER,)})
|
||||||
ok_btn = conn_window.child_window(best_match='OK', control_type='Button')
|
# 登录
|
||||||
ok_btn.click()
|
commands.extend([
|
||||||
|
{
|
||||||
file.click_input()
|
'type': 'key',
|
||||||
menubar.item_by_path('File->Open Connection').click_input()
|
'commands': (
|
||||||
self.app = app
|
'%f', c.DOWN * 5, c.ENTER
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'key',
|
||||||
|
'commands': (self.password, c.ENTER)
|
||||||
|
}
|
||||||
|
])
|
||||||
|
self._exec_commands(commands)
|
||||||
|
|
||||||
def wait(self):
|
def wait(self):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
UP = '{UP}'
|
||||||
|
LEFT = '{LEFT}'
|
||||||
|
DOWN = '{DOWN}'
|
||||||
|
RIGHT = '{RIGHT}'
|
||||||
|
TAB = '{VK_TAB}'
|
||||||
|
ENTER = '{VK_RETURN}'
|
Loading…
Reference in New Issue