87 lines
3.4 KiB
Python
87 lines
3.4 KiB
Python
import os
|
|
import platform
|
|
from webdav3.exceptions import LocalResourceNotFound
|
|
from webdav3.client import Client
|
|
import configparser
|
|
|
|
|
|
# todo : 暂时只有手动同步,没有做自动同步,暂时不需要传递参数
|
|
class Sync:
|
|
|
|
def __init__(self):
|
|
super(Sync, self).__init__()
|
|
types = ['NextCloud', 'JianGuoYun', 'WebDav']
|
|
# 根据不同的账号类型调用不同的方式
|
|
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'
|