简单研究WebDav,明天解决!

This commit is contained in:
liyp 2022-04-26 22:41:33 +08:00
parent 95bc38cc39
commit f6ef0ea479
3 changed files with 104 additions and 83 deletions

View file

@ -1,7 +1,6 @@
import configparser
import os
# todo : 用来创建软件的配置信息
from utils.BasicUtils import return_work_dir
@ -19,7 +18,7 @@ class CreateConfigure:
}
config['Account'] = options
# todo 解决保存配置文件的问题
config['System'] = {'exitStatus': 'None'}
with open(config_file, 'w') as f:
@ -60,7 +59,6 @@ class CreateConfigure:
config['Account']['webdav_password'] = webdav_password
config['Account']['type'] = login_type
# todo 解决保存配置文件的问题
config['System'] = {'exitStatus': 'None'}
print(config['System']['exitStatus'])

View file

@ -1,87 +1,87 @@
import os
import platform
from webdav3.exceptions import LocalResourceNotFound
from webdav3.client import Client
import configparser
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
# todo : 暂时只有手动同步,没有做自动同步,暂时不需要传递参数
class Sync:
class Sync(QThread):
sync_signal = pyqtSignal()
def __init__(self):
super(Sync, self).__init__()
self.sync_path = return_work_dir()
def run(self):
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)
# 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)
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 = 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)
# 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'

View file

@ -3,12 +3,15 @@ from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import configparser
from utils.BasicUtils import *
from utils.CreateConfigure import CreateConfigure
from utils.Sync import Sync
from view.LoginWidget import LoginWidget
from view.SettingWidget import SettingWidget
class User(QLabel):
update_signal = pyqtSignal()
sync_signal = pyqtSignal()
# 自定义信号, 注意信号必须为类属性
# button_clicked_signal = pyqtSignal()
@ -31,29 +34,31 @@ class User(QLabel):
self.user_name = QLabel("本地账号")
self.user_name.setObjectName('user_name')
self.user_mail = QLabel('未同步')
# 读取文件内容
self.account = read_init_file()
if self.account:
self.user_name.setText(self.account[1])
self.user_mail.setText(self.account[3])
# self.user_mail.setObjectName('user_mail')
self.user_mail.setStyleSheet('font-size:14px')
self.widget.setLayout(layout)
# 设置右键菜单
self.context_menu = QMenu(self)
info_layout.addWidget(self.user_name)
info_layout.addWidget(self.user_mail)
layout.addLayout(info_layout, 4)
layout.addStretch(1)
self.update_signal.connect(self.iniUI)
self.iniUI()
self.widget.setLayout(layout)
# 设置右键菜单
self.context_menu = QMenu(self)
self.init_menu()
def iniUI(self):
self.context_menu.clear()
# 读取文件内容
account = read_init_file()
if account[0] != 'None':
self.user_name.setText(account[1])
self.user_mail.setText(account[3])
else:
self.user_name.setText("本地账号")
self.user_mail.setText('未同步')
# self.user_mail.setObjectName('user_mail')
def contextMenuEvent(self, event):
self.context_menu.exec_(event.globalPos())
def init_menu(self):
# 背景透明
self.context_menu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
@ -63,23 +68,41 @@ class User(QLabel):
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/setting.svg'), '设置', self.setting)
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/sync.svg'), '同步', self.sync)
if self.account:
if account[0] != 'None':
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/exit.svg'), '登出', self.exit_account)
else:
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/login.svg'), '登录', self.login_account)
def contextMenuEvent(self, event):
self.context_menu.exec_(event.globalPos())
# todo 设置右键点击事件
def setting(self):
self.setting_widget = SettingWidget()
self.setting_widget.show()
def sync(self):
sync = Sync()
sync.sync_signal.connect(self.sync_status)
sync.run()
self.sync_signal.emit()
pass
def sync(self):
pass
def sync_status(self):
print('Success!')
def exit_account(self):
config_path = return_work_dir() + 'PyQtToDoList.ini'
# print(config_path)
try:
if os.path.exists(config_path):
os.remove(config_path)
CreateConfigure()
except Exception as e:
print(e)
self.update_signal.emit()
pass
def login_account(self):