pyqt-todolist/utils/Sync.py

69 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from webdav4.fsspec import WebdavFileSystem
from PyQt5.QtCore import Qt, pyqtSignal, QThread
from utils.BasicUtils import read_init_file, return_work_dir, return_config_dir
class Sync(QThread):
sync_signal = pyqtSignal()
def __init__(self):
super(Sync, self).__init__()
self.sync_path = return_work_dir()
self.config_info = read_init_file()
def run(self):
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('/', 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:
client.mkdir('PyQtToDoList/ToDoList/')
# 检查本地配置文件
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)
except Exception as e:
print('<Sync>', e)
else:
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)