完善同步功能!

This commit is contained in:
liyp 2022-04-28 17:07:42 +08:00
parent d2888a944b
commit 3959ec1904
8 changed files with 65 additions and 50 deletions

View file

@ -62,6 +62,7 @@ class MainWidget(QWidget):
self.system_listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex) self.system_listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex)
self.setLayout(layout) self.setLayout(layout)
self.user_label.sync_signal.connect(self.initUI)
self.initUI() self.initUI()
def initUI(self): def initUI(self):

View file

@ -1,12 +1,9 @@
import os import base64
work_path = os.path.expandvars('$HOME') + '/.config/PyQtToDoList/ToDoList' password = '123456lyp'
# sync_path = os.walk(work_path) later = base64.b64encode(password.encode('utf-8'))
# print(sync_path) print(later)
later1 = str(later, 'utf-8')
print(later1)
for root, dirs, files in os.walk(work_path, topdown=False): print(str(base64.b64decode(later1), 'utf-8'))
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])

View file

@ -1,3 +1,4 @@
import base64
import json import json
import os import os
import platform import platform
@ -11,6 +12,19 @@ def return_work_dir():
if not os.path.exists(work_path): if not os.path.exists(work_path):
os.mkdir(work_path) os.mkdir(work_path)
return os.path.expandvars('$HOME') + '/.config/PyQtToDoList/' 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': elif platform.system() == 'Windows':
work_path = os.getcwd() + '/../config/' work_path = os.getcwd() + '/../config/'
if not os.path.exists(work_path): if not os.path.exists(work_path):
@ -26,10 +40,13 @@ def read_init_file():
try: try:
config.read(return_work_dir() + 'PyQtToDoList.ini') config.read(return_work_dir() + 'PyQtToDoList.ini')
account = config['Account'] account = config['Account']
password = str(base64.b64decode(account['webdav_password']), 'utf-8')
if account: 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: except KeyError as e:
return False return False
except UnicodeError as e:
return False
def change_myday_important_conf(change_type, key, value): def change_myday_important_conf(change_type, key, value):

View file

@ -1,5 +1,6 @@
import configparser import configparser
import os import os
import base64
from utils.BasicUtils import return_work_dir from utils.BasicUtils import return_work_dir
@ -35,6 +36,8 @@ class CreateConfigure:
config_file = return_work_dir() + 'PyQtToDoList.ini' config_file = return_work_dir() + 'PyQtToDoList.ini'
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_file) config.read(config_file)
# 避免密码明文显示使用base64编码
webdav_password = str(base64.b64encode(webdav_password.encode('utf-8')), 'utf-8')
# if not os.path.exists(config_file): # if not os.path.exists(config_file):
print(config['Account']['type']) print(config['Account']['type'])
if login_type == 'NextCloud': if login_type == 'NextCloud':
@ -65,5 +68,3 @@ class CreateConfigure:
with open(config_file, 'w') as f: with open(config_file, 'w') as f:
config.write(f) config.write(f)
print('create complete!', config_file) print('create complete!', config_file)

View file

@ -13,10 +13,11 @@ class LinkTest(QThread):
super(LinkTest, self).__init__() super(LinkTest, self).__init__()
def check(self, address, name, password): def check(self, address, name, password):
# print(address, name, password)
try: try:
client = Client(base_url=address, client = Client(base_url=address,
auth=(name, password), timeout=5000) auth=(name, password), timeout=5000)
# print(client.info('/')) ls = client.info('/')
return 200 return 200
except HTTPError as e: except HTTPError as e:
print('HttpError') print('HttpError')

View file

@ -2,7 +2,7 @@ import os
from webdav4.fsspec import WebdavFileSystem from webdav4.fsspec import WebdavFileSystem
import configparser import configparser
from PyQt5.QtCore import Qt, pyqtSignal, QThread 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 from webdav4.client import HTTPError, Client
@ -13,24 +13,24 @@ class Sync(QThread):
super(Sync, self).__init__() super(Sync, self).__init__()
self.sync_path = return_work_dir() self.sync_path = return_work_dir()
self.config_info = read_init_file() self.config_info = read_init_file()
# print(self.config_info[0], self.config_info[1], self.config_info[2])
def run(self): def run(self):
# types = ['NextCloud', 'JianGuoYun', 'WebDav']
# 根据不同的账号类型调用不同的方式
# config = configparser.ConfigParser()
conf_file = self.sync_path + 'PyQtToDoList.conf' conf_file = self.sync_path + 'PyQtToDoList.conf'
todo_path = self.sync_path + 'ToDoList/' todo_path = self.sync_path + 'ToDoList/'
if os.path.exists(conf_file) or os.path.exists(todo_path): if os.path.exists(conf_file) or os.path.exists(todo_path):
try: try:
client = WebdavFileSystem(base_url=self.config_info[0], client = WebdavFileSystem(base_url=self.config_info[0],
auth=(self.config_info[1], self.config_info[2])) auth=(self.config_info[1], self.config_info[2]))
# ls = client.ls('/PyQtToDoList', detail=False) ls = client.ls('/', detail=False)
# print(ls) print(ls)
work_path_exist = client.exists('PyQtToDoList') work_path_exist = client.exists('PyQtToDoList')
# 如果WebDav服务器的同步目录没有就创建同步目录和Todo目录 # 如果WebDav服务器的同步目录没有就创建同步目录和Todo目录
if not work_path_exist: if not work_path_exist:
client.mkdir('PyQtToDoList') client.mkdir('PyQtToDoList')
client.mkdir('PyQtToDoList/ToDoList/') client.mkdir('PyQtToDoList/ToDoList/')
print('第一次向服务器备份,创建文件。。。')
# 如果有同步目录没有Todo目录就创建Todo目录 # 如果有同步目录没有Todo目录就创建Todo目录
remote_todo_path = client.exists('PyQtToDoList/ToDoList') remote_todo_path = client.exists('PyQtToDoList/ToDoList')
if not remote_todo_path: if not remote_todo_path:
@ -38,10 +38,13 @@ class Sync(QThread):
# 检查本地配置文件 # 检查本地配置文件
if os.path.exists(conf_file): if os.path.exists(conf_file):
client.upload(conf_file, 'PyQtToDoList/PyQtToDoList.conf') client.upload(conf_file, 'PyQtToDoList/PyQtToDoList.conf')
print('上传配置文件。。。')
if os.path.exists(todo_path): if os.path.exists(todo_path):
todo_files = os.listdir(todo_path) todo_files = os.listdir(todo_path)
for file in todo_files: for file in todo_files:
client.upload(todo_path + file, 'PyQtToDoList/ToDoList/' + file) client.upload(todo_path + file, 'PyQtToDoList/ToDoList/' + file)
print('上传Todo:', file)
self.sync_signal.emit() self.sync_signal.emit()
# print(status) # print(status)
@ -49,27 +52,21 @@ class Sync(QThread):
print('<Sync>', e) print('<Sync>', e)
else: else:
client = WebdavFileSystem(base_url=self.config_info[0], try:
auth=(self.config_info[1], self.config_info[2])) client = WebdavFileSystem(base_url=self.config_info[0],
work_path_exist = client.exists('PyQtToDoList/') auth=(self.config_info[1], self.config_info[2]))
if work_path_exist: work_path_exist = client.exists('PyQtToDoList/')
if client.exists('PyQtToDoList/PyQtToDoList.conf'): if work_path_exist:
client.download('PyQtToDoList/PyQtToDoList.conf', conf_file) if client.exists('PyQtToDoList/PyQtToDoList.conf') and not os.path.exists(conf_file):
if client.exists('PyQtToDoList/ToDoList'): client.download('PyQtToDoList/PyQtToDoList.conf', conf_file)
os.mkdir(todo_path) if client.exists('PyQtToDoList/ToDoList'):
todo_list = client.ls('PyQtToDoList/ToDoList') if not os.path.exists(todo_path):
print('todo_list:', todo_list) os.mkdir(todo_path)
for item in todo_list: todo_list = client.ls('PyQtToDoList/ToDoList', detail=False)
client.download('PyQtToDoList/ToDoList' + item, todo_path + item) # print('todo_list:', todo_list)
self.sync_signal.emit() for item in todo_list:
print('下载文件:', return_config_dir() + item)
# @staticmethod client.download(item, return_config_dir() + item)
# def test_link(self, address, name, password): self.sync_signal.emit()
# try: except Exception as e:
# client = Client(base_url=address, print('<Sync>', e)
# auth=(name, password), timeout=5000)
# # print(client.info('/'))
# return 200
# except HTTPError as e:
# print('HttpError')
# return 500

View file

@ -1,7 +1,6 @@
from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
import configparser
from utils.BasicUtils import * from utils.BasicUtils import *
from utils.CreateConfigure import CreateConfigure from utils.CreateConfigure import CreateConfigure
from utils.LinkTest import LinkTest from utils.LinkTest import LinkTest
@ -11,7 +10,7 @@ from view.Tip_UI import TipUi
class LoginWidget(QWidget): class LoginWidget(QWidget):
# todo : 添加一个信号,等点击保存后在 main 里面更新未登录的信息 # 登录成功的信号
login_signal = pyqtSignal() login_signal = pyqtSignal()
def __init__(self): def __init__(self):

View file

@ -83,17 +83,19 @@ class User(QLabel):
def sync(self): def sync(self):
account = read_init_file() account = read_init_file()
if account and account[0] != 'None': if account and account[0] != 'None':
sync = Sync() self.sync_action = Sync()
sync.start() 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: else:
self.login_account() self.login_account()
pass pass
def sync_status(self): def sync_status(self):
TipUi.show_tip('同步成功!') TipUi.show_tip('同步成功!')
self.sync_signal.emit()
print('Success!') print('Success!')
def exit_account(self): def exit_account(self):