forked from bkdwei/kdssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdconfig.py
76 lines (66 loc) · 2.55 KB
/
kdconfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: utf-8
import os
import json
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QDialog
from PyQt5.uic import loadUi
from fileutil import check_and_create
class sshconfig(QDialog):
def __init__(self):
super(sshconfig, self).__init__()
loadUi("kdconfig.ui", self)
self.config_file = os.environ["HOME"] + "/.config/kdssh/conf.json"
self.init_confs()
def init_confs(self):
check_and_create(self.config_file)
with open(self.config_file, "r") as f:
content = f.read()
if content.strip() != "":
json_content = json.loads(content)
self.confs = json_content
else:
self.confs = []
@pyqtSlot()
def on_buttonBox_accepted(self):
conf = {}
conf["solution"] = self.le_solution.text()
conf["host"] = self.le_host.text()
conf["port"] = self.le_port.text()
conf["name"] = self.le_name.text()
conf["password"] = self.le_password.text()
if self.new_item_flag:
self.confs.append(conf)
# 更新界面的列表
self.lw_confs.addItem(conf["solution"])
else:
for item in self.confs:
if self.temp_solution == item["solution"]:
self.confs.remove(item)
self.confs.append(conf)
# 更新界面的列表
if self.temp_solution != conf["solution"]:
self.lw_confs.removeItemWidget(
self.lw_confs.takeItem(self.lw_confs.currentRow())
)
self.lw_confs.addItem(conf["solution"])
self.update_confs()
def edit_conf(self, solution):
for conf in self.confs:
if conf["solution"] == solution:
self.le_solution.setText(conf["solution"])
self.le_host.setText(conf["host"])
self.le_port.setText(conf["port"])
self.le_name.setText(conf["name"])
self.le_password.setText(conf["password"])
self.temp_solution = solution
def del_conf(self, conf_solution):
for item in self.confs:
if conf_solution == item["solution"]:
self.confs.remove(item)
print("删除元素")
self.update_confs()
def update_confs(self):
with open(self.config_file, "w+") as f:
f.write(json.dumps(self.confs))
f.flush()