pyqt-todolist/utils/Sync.py

76 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
from utils.BasicUtils import read_init_file, return_work_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-26 22:41:33 +08:00
def run(self):
# types = ['NextCloud', 'JianGuoYun', 'WebDav']
2022-04-17 17:25:35 +08:00
# 根据不同的账号类型调用不同的方式
# 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)
work_path_exist = client.exists('PyQtToDoList')
# 如果WebDav服务器的同步目录没有就创建同步目录和Todo目录
if not work_path_exist:
client.mkdir('PyQtToDoList')
client.mkdir('PyQtToDoList/ToDoList/')
# 如果有同步目录没有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')
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)
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:
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()
2022-04-26 22:41:33 +08:00
2022-04-27 21:58:00 +08:00
# @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