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 webdav4.client import HTTPError, Client 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): # 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) 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) except Exception as e: 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