From 3959ec190438f3a12cc8249cd18f3046cdde8515 Mon Sep 17 00:00:00 2001 From: liyp Date: Thu, 28 Apr 2022 17:07:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=90=8C=E6=AD=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.py | 1 + test/SyncTest.py | 17 +++++------- utils/BasicUtils.py | 19 +++++++++++++- utils/CreateConfigure.py | 5 ++-- utils/LinkTest.py | 3 ++- utils/Sync.py | 57 +++++++++++++++++++--------------------- view/LoginWidget.py | 3 +-- view/UserLabel.py | 10 ++++--- 8 files changed, 65 insertions(+), 50 deletions(-) diff --git a/main/main.py b/main/main.py index 32c2041..4fc77aa 100644 --- a/main/main.py +++ b/main/main.py @@ -62,6 +62,7 @@ class MainWidget(QWidget): self.system_listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex) self.setLayout(layout) + self.user_label.sync_signal.connect(self.initUI) self.initUI() def initUI(self): diff --git a/test/SyncTest.py b/test/SyncTest.py index 8251ecd..09673ac 100644 --- a/test/SyncTest.py +++ b/test/SyncTest.py @@ -1,12 +1,9 @@ -import os +import base64 -work_path = os.path.expandvars('$HOME') + '/.config/PyQtToDoList/ToDoList' -# sync_path = os.walk(work_path) -# print(sync_path) +password = '123456lyp' +later = base64.b64encode(password.encode('utf-8')) +print(later) +later1 = str(later, 'utf-8') +print(later1) -for root, dirs, files in os.walk(work_path, topdown=False): - for name in files: - print(os.path.join(root, name).split('/')[-1]) - # for name in dirs: - # # print(os.path.join(root, name)) - # print(os.path.join(root, name).split('/')[-1]) +print(str(base64.b64decode(later1), 'utf-8')) diff --git a/utils/BasicUtils.py b/utils/BasicUtils.py index df2513a..b28f077 100644 --- a/utils/BasicUtils.py +++ b/utils/BasicUtils.py @@ -1,3 +1,4 @@ +import base64 import json import os import platform @@ -11,6 +12,19 @@ def return_work_dir(): if not os.path.exists(work_path): os.mkdir(work_path) return os.path.expandvars('$HOME') + '/.config/PyQtToDoList/' + elif platform.system() == 'Windows': + work_path = os.getcwd() + '/../config/PyQtToDoList/' + if not os.path.exists(work_path): + os.mkdir(work_path) + return work_path + + +def return_config_dir(): + if platform.system() == 'Linux': + work_path = os.path.expandvars('$HOME') + '/.config/' + if not os.path.exists(work_path): + os.mkdir(work_path) + return os.path.expandvars('$HOME') + '/.config/' elif platform.system() == 'Windows': work_path = os.getcwd() + '/../config/' if not os.path.exists(work_path): @@ -26,10 +40,13 @@ def read_init_file(): try: config.read(return_work_dir() + 'PyQtToDoList.ini') account = config['Account'] + password = str(base64.b64decode(account['webdav_password']), 'utf-8') if account: - return [account['webdav_hostname'], account['webdav_login'], account['webdav_password'], account['type']] + return [account['webdav_hostname'], account['webdav_login'], password, account['type']] except KeyError as e: return False + except UnicodeError as e: + return False def change_myday_important_conf(change_type, key, value): diff --git a/utils/CreateConfigure.py b/utils/CreateConfigure.py index 586bde1..afe0e6c 100644 --- a/utils/CreateConfigure.py +++ b/utils/CreateConfigure.py @@ -1,5 +1,6 @@ import configparser import os +import base64 from utils.BasicUtils import return_work_dir @@ -35,6 +36,8 @@ class CreateConfigure: config_file = return_work_dir() + 'PyQtToDoList.ini' config = configparser.ConfigParser() config.read(config_file) + # 避免密码明文显示,使用base64编码 + webdav_password = str(base64.b64encode(webdav_password.encode('utf-8')), 'utf-8') # if not os.path.exists(config_file): print(config['Account']['type']) if login_type == 'NextCloud': @@ -65,5 +68,3 @@ class CreateConfigure: with open(config_file, 'w') as f: config.write(f) print('create complete!', config_file) - - diff --git a/utils/LinkTest.py b/utils/LinkTest.py index 6c49d75..92b9be1 100644 --- a/utils/LinkTest.py +++ b/utils/LinkTest.py @@ -13,10 +13,11 @@ class LinkTest(QThread): super(LinkTest, self).__init__() def check(self, address, name, password): + # print(address, name, password) try: client = Client(base_url=address, auth=(name, password), timeout=5000) - # print(client.info('/')) + ls = client.info('/') return 200 except HTTPError as e: print('HttpError') diff --git a/utils/Sync.py b/utils/Sync.py index 2c4f1e4..7f2bd24 100644 --- a/utils/Sync.py +++ b/utils/Sync.py @@ -2,7 +2,7 @@ import os from webdav4.fsspec import WebdavFileSystem import configparser from PyQt5.QtCore import Qt, pyqtSignal, QThread -from utils.BasicUtils import read_init_file, return_work_dir +from utils.BasicUtils import read_init_file, return_work_dir, return_config_dir from webdav4.client import HTTPError, Client @@ -13,24 +13,24 @@ class Sync(QThread): super(Sync, self).__init__() self.sync_path = return_work_dir() self.config_info = read_init_file() + # print(self.config_info[0], self.config_info[1], self.config_info[2]) def run(self): - # types = ['NextCloud', 'JianGuoYun', 'WebDav'] - # 根据不同的账号类型调用不同的方式 - # config = configparser.ConfigParser() + conf_file = self.sync_path + 'PyQtToDoList.conf' todo_path = self.sync_path + 'ToDoList/' if os.path.exists(conf_file) or os.path.exists(todo_path): try: client = WebdavFileSystem(base_url=self.config_info[0], auth=(self.config_info[1], self.config_info[2])) - # ls = client.ls('/PyQtToDoList', detail=False) - # print(ls) + ls = client.ls('/', detail=False) + print(ls) work_path_exist = client.exists('PyQtToDoList') # 如果WebDav服务器的同步目录没有,就创建同步目录和Todo目录 if not work_path_exist: client.mkdir('PyQtToDoList') client.mkdir('PyQtToDoList/ToDoList/') + print('第一次向服务器备份,创建文件。。。') # 如果有同步目录,没有Todo目录,就创建Todo目录 remote_todo_path = client.exists('PyQtToDoList/ToDoList') if not remote_todo_path: @@ -38,10 +38,13 @@ class Sync(QThread): # 检查本地配置文件 if os.path.exists(conf_file): client.upload(conf_file, 'PyQtToDoList/PyQtToDoList.conf') + print('上传配置文件。。。') + if os.path.exists(todo_path): todo_files = os.listdir(todo_path) for file in todo_files: client.upload(todo_path + file, 'PyQtToDoList/ToDoList/' + file) + print('上传Todo:', file) self.sync_signal.emit() # print(status) @@ -49,27 +52,21 @@ class Sync(QThread): print('', e) else: - client = WebdavFileSystem(base_url=self.config_info[0], - auth=(self.config_info[1], self.config_info[2])) - work_path_exist = client.exists('PyQtToDoList/') - if work_path_exist: - if client.exists('PyQtToDoList/PyQtToDoList.conf'): - client.download('PyQtToDoList/PyQtToDoList.conf', conf_file) - if client.exists('PyQtToDoList/ToDoList'): - os.mkdir(todo_path) - todo_list = client.ls('PyQtToDoList/ToDoList') - print('todo_list:', todo_list) - for item in todo_list: - client.download('PyQtToDoList/ToDoList' + item, todo_path + item) - self.sync_signal.emit() - - # @staticmethod - # def test_link(self, address, name, password): - # try: - # client = Client(base_url=address, - # auth=(name, password), timeout=5000) - # # print(client.info('/')) - # return 200 - # except HTTPError as e: - # print('HttpError') - # return 500 + try: + client = WebdavFileSystem(base_url=self.config_info[0], + auth=(self.config_info[1], self.config_info[2])) + work_path_exist = client.exists('PyQtToDoList/') + if work_path_exist: + if client.exists('PyQtToDoList/PyQtToDoList.conf') and not os.path.exists(conf_file): + client.download('PyQtToDoList/PyQtToDoList.conf', conf_file) + if client.exists('PyQtToDoList/ToDoList'): + if not os.path.exists(todo_path): + os.mkdir(todo_path) + todo_list = client.ls('PyQtToDoList/ToDoList', detail=False) + # print('todo_list:', todo_list) + for item in todo_list: + print('下载文件:', return_config_dir() + item) + client.download(item, return_config_dir() + item) + self.sync_signal.emit() + except Exception as e: + print('', e) diff --git a/view/LoginWidget.py b/view/LoginWidget.py index 751aa5b..fca7d03 100644 --- a/view/LoginWidget.py +++ b/view/LoginWidget.py @@ -1,7 +1,6 @@ from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtGui import * from PyQt5.QtWidgets import * -import configparser from utils.BasicUtils import * from utils.CreateConfigure import CreateConfigure from utils.LinkTest import LinkTest @@ -11,7 +10,7 @@ from view.Tip_UI import TipUi class LoginWidget(QWidget): - # todo : 添加一个信号,等点击保存后在 main 里面更新未登录的信息 + # 登录成功的信号 login_signal = pyqtSignal() def __init__(self): diff --git a/view/UserLabel.py b/view/UserLabel.py index 0b75d29..b0ce269 100644 --- a/view/UserLabel.py +++ b/view/UserLabel.py @@ -83,17 +83,19 @@ class User(QLabel): def sync(self): account = read_init_file() if account and account[0] != 'None': - sync = Sync() - sync.start() + self.sync_action = Sync() + TipUi.show_tip('同步中。。。') + self.sync_action.sync_signal.connect(self.sync_status) + + self.sync_action.start() - sync.sync_signal.connect(self.sync_status) - self.sync_signal.emit() else: self.login_account() pass def sync_status(self): TipUi.show_tip('同步成功!') + self.sync_signal.emit() print('Success!') def exit_account(self):