pyqt-todolist/utils/Sync.py

73 lines
3.3 KiB
Python
Raw Normal View History

2022-04-18 20:22:42 +08:00
import os
from webdav4.fsspec import WebdavFileSystem
2022-04-18 20:22:42 +08:00
import configparser
2022-04-26 22:41:33 +08:00
from PyQt5.QtCore import Qt, pyqtSignal, QThread
2022-04-28 17:07:42 +08:00
from utils.BasicUtils import read_init_file, return_work_dir, return_config_dir
from webdav4.client import HTTPError, Client
2022-04-17 17:25:35 +08:00
2022-04-26 22:41:33 +08:00
class Sync(QThread):
sync_signal = pyqtSignal()
2022-04-17 17:25:35 +08:00
2022-04-18 20:22:42 +08:00
def __init__(self):
2022-04-17 17:25:35 +08:00
super(Sync, self).__init__()
2022-04-26 22:41:33 +08:00
self.sync_path = return_work_dir()
self.config_info = read_init_file()
2022-04-28 17:07:42 +08:00
# print(self.config_info[0], self.config_info[1], self.config_info[2])
2022-04-26 22:41:33 +08:00
def run(self):
2022-04-28 17:07:42 +08:00
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]))
2022-04-28 17:07:42 +08:00
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/')
2022-04-28 17:07:42 +08:00
print('第一次向服务器备份,创建文件。。。')
# 如果有同步目录没有Todo目录就创建Todo目录
remote_todo_path = client.exists('PyQtToDoList/ToDoList')
if not remote_todo_path:
client.mkdir('PyQtToDoList/ToDoList/')
# 检查本地配置文件
if os.path.exists(conf_file):
client.upload(conf_file, 'PyQtToDoList/PyQtToDoList.conf')
2022-04-28 17:07:42 +08:00
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)
2022-04-28 17:07:42 +08:00
print('上传Todo:', file)
self.sync_signal.emit()
# print(status)
2022-04-18 20:22:42 +08:00
except Exception as e:
print('<Sync>', e)
2022-04-26 22:41:33 +08:00
else:
2022-04-28 17:07:42 +08:00
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('<Sync>', e)