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
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-17 17:25:35 +08:00
2022-04-18 20:22:42 +08:00
# todo : 暂时只有手动同步,没有做自动同步,暂时不需要传递参数
2022-04-17 17:25:35 +08:00
class Sync:
2022-04-18 20:22:42 +08:00
def __init__(self):
2022-04-17 17:25:35 +08:00
super(Sync, self).__init__()
types = ['NextCloud', 'JianGuoYun', 'WebDav']
# 根据不同的账号类型调用不同的方式
2022-04-18 20:22:42 +08:00
config = configparser.ConfigParser()
try:
config.read(self.read_config_file())
webdav_hostname = config['Account']['webdav_hostname']
webdav_login = config['Account']['webdav_login']
webdav_password = config['Account']['webdav_password']
options = {
'webdav_hostname': webdav_hostname,
'webdav_login': webdav_login,
'webdav_password': webdav_password,
'disable_check': True
}
self.upload_file(options)
except FileNotFoundError as e:
print(e)
def upload_file(self, options):
client = Client(options)
list1 = client.list('/')
print(list1)
exist = client.check('OpenTodoList')
print(exist)
if not exist:
client.mkdir('OpenTodoList')
try:
# todo : 需要完善 Windows 上传的分片
config_file, upload_files, upload_dirs = self.get_sync_path()
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)
# client.pull('OpenTodoList', '')
# client.push('OpenTodoList', self.get_sync_path())
except LocalResourceNotFound as e:
print('An error happen: LocalResourceNotFound ---')
"""
程序的配置文件和数据根据不同系统保存在不同文件夹
Linux在 ~/.config/PyQtToDoList下
Windows在 程序目录/config/
"""
def get_sync_path(self):
work_path = ''
if platform.system() == 'Linux':
work_path = os.path.expandvars('$HOME') + '/.config/PyQtToDoList'
elif platform.system() == 'Windows':
work_path = os.getcwd() + 'config'
upload_files = []
upload_dirs = []
if not os.path.exists(work_path):
os.mkdir(work_path)
for root, dirs, files in os.walk(work_path, topdown=False):
for name in files:
upload_files.append(os.path.join(root, name))
print(upload_files)
for name in dirs:
upload_dirs.append(os.path.join(root, name))
print(upload_dirs)
return work_path + 'PyQtToDoList.ini', upload_files, upload_dirs
def read_config_file(self):
if platform.system() == 'Linux':
return os.path.expandvars('$HOME') + '/.config/PyQtToDoList/PyQtToDoList.ini'
elif platform.system() == 'Windows':
config_path = os.getcwd() + 'config'
if not os.path.exists(config_path):
os.mkdir(config_path)
return config_path + 'PyQtToDoList.ini'