pyqt-todolist/utils/Sync.py

88 lines
3.4 KiB
Python
Raw Normal View History

2022-04-18 20:22:42 +08:00
import os
import platform
2022-04-26 22:41:33 +08:00
2022-04-18 20:22:42 +08:00
from webdav3.exceptions import LocalResourceNotFound
2022-04-17 17:25:35 +08:00
from webdav3.client import Client
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 PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from utils.BasicUtils import read_init_file, return_work_dir
2022-04-17 17:25:35 +08:00
2022-04-18 20:22:42 +08:00
# todo : 暂时只有手动同步,没有做自动同步,暂时不需要传递参数
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()
def run(self):
2022-04-17 17:25:35 +08:00
types = ['NextCloud', 'JianGuoYun', 'WebDav']
# 根据不同的账号类型调用不同的方式
2022-04-18 20:22:42 +08:00
config = configparser.ConfigParser()
2022-04-26 22:41:33 +08:00
# try:
# config.read(read_init_file())
self.config_info = read_init_file()
# webdav_hostname = config['Account']['webdav_hostname']
# webdav_login = config['Account']['webdav_login']
# webdav_password = config['Account']['webdav_password']
options = {
'webdav_hostname': self.config_info[0],
'webdav_login': self.config_info[1],
'webdav_password': self.config_info[2],
'disable_check': True
}
self.upload_file(options)
# except FileNotFoundError as e:
# print('<Sync>', e)
2022-04-18 20:22:42 +08:00
def upload_file(self, options):
try:
2022-04-26 22:41:33 +08:00
client = Client(options)
path = client.check('PyQtToDoList')
print(path)
# list1 = client.list('/')
# print('/ ', list1)
client.mkdir('PyQtToDoList/')
list1 = client.list('/')
print('/ ', list1)
client.upload_file('PyQtToDoList/' + 'PyQtToDoList.conf', self.sync_path + 'PyQtToDoList.conf')
client.upload_directory('PyQtToDoList/' + 'ToDoList/', self.sync_path + 'ToDoList/')
if not path:
client.mkdir('PyQtToDoList/PyQtToDoList.conf')
list1 = client.list('/')
print('/ ', list1)
conf_file = client.check('PyQtToDoList/PyQtToDoList.conf')
print('conf_file:', conf_file)
if not conf_file:
try:
client.push('PyQtToDoList/' + 'PyQtToDoList.conf', self.sync_path + 'PyQtToDoList.conf')
client.push('PyQtToDoList/', self.sync_path + 'ToDoList')
self.sync_signal.emit()
except LocalResourceNotFound as e:
print('An error happen: LocalResourceNotFound ---')
except Exception as e:
print('<Sync>', e)
# config_file, upload_files, upload_dirs =
# client.upload('OpenTodoList/', config_file)
# for upload_dir in upload_dirs:
# upload_dir_name = upload_dir.split('/')[-1]
# client.mkdir('OpenTodoList/' + upload_dir_name)
# for upload_file in upload_files:
# upload_dir = upload_file.split('/')[-2]
# upload_file_name = upload_file.split('/')[-1]
# client.upload('OpenTodoList/' + upload_dir + upload_file_name, upload_file)
2022-04-18 20:22:42 +08:00
# client.pull('OpenTodoList', '')
# client.push('OpenTodoList', self.get_sync_path())
"""
程序的配置文件和数据根据不同系统保存在不同文件夹
Linux在 ~/.config/PyQtToDoList下
Windows在 程序目录/config/
"""