diff --git a/test/SyncTest.py b/test/SyncTest.py new file mode 100644 index 0000000..0f05319 --- /dev/null +++ b/test/SyncTest.py @@ -0,0 +1,12 @@ +import os + +work_path = os.path.expandvars('$HOME') + '/.config/PyQtToDoList' +# sync_path = os.walk(work_path) +# print(sync_path) + +for root, dirs, files in os.walk(work_path, topdown=False): + for name in files: + print(os.path.join(root, name).split('/')[-1]) + for name in dirs: + # print(os.path.join(root, name)) + print(os.path.join(root, name).split('/')[-1]) diff --git a/test/WatchDog.py b/test/WatchDog.py new file mode 100644 index 0000000..6afcbb3 --- /dev/null +++ b/test/WatchDog.py @@ -0,0 +1,46 @@ +from watchdog.observers import Observer +from watchdog.events import * +import time + + +# todo : 实时检测文件变化,来同步文件 +class FileEventHandler(FileSystemEventHandler): + def __init__(self): + FileSystemEventHandler.__init__(self) + + def on_moved(self, event): + if event.is_directory: + print("directory moved from {0} to {1}".format(event.src_path, event.dest_path)) + else: + print("file moved from {0} to {1}".format(event.src_path, event.dest_path)) + + def on_created(self, event): + if event.is_directory: + print("directory created:{0}".format(event.src_path)) + else: + print("file created:{0}".format(event.src_path)) + + def on_deleted(self, event): + if event.is_directory: + print("directory deleted:{0}".format(event.src_path)) + else: + print("file deleted:{0}".format(event.src_path)) + + def on_modified(self, event): + if event.is_directory: + print("directory modified:{0}".format(event.src_path)) + else: + print("file modified:{0}".format(event.src_path)) + + +if __name__ == "__main__": + observer = Observer() + event_handler = FileEventHandler() + observer.schedule(event_handler, os.path.expandvars('$HOME') + '/.config/PyQtToDoList/', True) + observer.start() + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join() diff --git a/test/__init__.py b/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/test/threadTest.py b/test/threadTest.py new file mode 100644 index 0000000..44c08e8 --- /dev/null +++ b/test/threadTest.py @@ -0,0 +1,41 @@ +# import os +# +# for root, dir, files in os.walk(os.path.expandvars('$HOME') + '/.config/PyQtToDoList/'): +# for i in files: +# print(root, dir, files) +# # print(root) +# # print(dir) +import os + + +# 直接读取配置文件获取ToDo列表来选择备份文件,不再选择同时获取文件和文件夹了 + +def file_name_walk(file_dir): + for root, dirs, files in os.walk(file_dir): + # print("root", root) # 当前目录路径 + # print("dirs", dirs) # 当前路径下所有子目录 + # print("files", files) # 当前路径下所有非目录子文件 + print(len(files)) + if dirs and files: + # print(True) + print(str(files[0])) + elif dirs and not files: + print(dirs[i] for i in range(len(dirs))) + else: + print(str(files[j] for j in range(len(files)))) + + # print(False) + # if + # if files : + # print(root + str(dirs[0]) + '/' + str(files[0])) + # else: + # print(root + str(files[0])) + + +file_name_walk(os.path.expandvars('$HOME') + '/.config/PyQtToDoList/') +# root ./ +# dirs ['test'] +# files ['200-2000(1).txt', '200-2000(2).txt', '200-2000(3).txt', 'getFileName.py'] +# root ./test +# dirs [] +# files ['test.txt'] diff --git a/utils/CreateConfigure.py b/utils/CreateConfigure.py index c3bbfb1..b1adbc9 100644 --- a/utils/CreateConfigure.py +++ b/utils/CreateConfigure.py @@ -1,3 +1,64 @@ +import configparser +import platform +import os -# todo : 用来创建软件的配置信息 \ No newline at end of file +# todo : 用来创建软件的配置信息 +class CreateConfigure: + """ + :param login_type : 登录方式 有 NextCloud、JianGuoYun、WebDav + :param webdav_login : 登录用户名 + :param webdav_password : 登录密码 + :param webdav_hostname : 服务地址 + """ + + def __init__(self, login_type, webdav_login, webdav_password, webdav_hostname=None): + config_file = self.return_config_file() + config = configparser.ConfigParser() + if not os.path.exists(config_file): + # print(config_file) + options = {} + if login_type == 'NextCloud': + webdav_hostname = webdav_hostname + '/' if webdav_hostname[-1] != '/' else webdav_hostname + options = { + 'webdav_hostname': webdav_hostname + "remote.php/dav/files/admin/", + 'webdav_login': webdav_login, + 'webdav_password': webdav_password, + 'type': login_type + } + + elif login_type == 'JianGuoYun': + options = { + 'webdav_hostname': "https://dav.jianguoyun.com/dav/", + 'webdav_login': webdav_login, + 'webdav_password': webdav_password, + 'type': login_type + + } + elif login_type == 'WebDav': + webdav_hostname = webdav_hostname + '/' if webdav_hostname[-1] != '/' else webdav_hostname + options = { + 'webdav_hostname': webdav_hostname, + 'webdav_login': webdav_login, + 'webdav_password': webdav_password, + 'type': login_type + + } + config['Account'] = options + config['ToDoList'] = {} + + with open(config_file, 'w') as f: + config.write(f) + print('create complete!', config_file) + + def return_config_file(self): + if platform.system() == 'Linux': + work_path = os.path.expandvars('$HOME') + '/.config/PyQtToDoList' + if not os.path.exists(work_path): + os.mkdir(work_path) + return os.path.expandvars('$HOME') + '/.config/PyQtToDoList/PyQtToDoList.ini' + elif platform.system() == 'Windows': + return os.getcwd() + '/PyQtToDoList.ini' + + +CreateConfigure('NextCloud', 'admin', '19990903@lyp', 'https://cloud.liyp.cc') diff --git a/utils/LoginAction.py b/utils/LoginAction.py index b68ebd8..d019fbf 100644 --- a/utils/LoginAction.py +++ b/utils/LoginAction.py @@ -1,4 +1,5 @@ from webdav3.client import Client +import configparser class LoginAction: @@ -10,28 +11,11 @@ class LoginAction: """ def __init__(self, login_type, webdav_login, webdav_password, webdav_hostname=None): - options = {} - if login_type == 'NextCloud': - webdav_hostname = webdav_hostname + '/' if webdav_hostname[-1] == '/' else webdav_hostname - options = { - 'webdav_hostname': webdav_hostname + "remote.php/dav/files/admin/", - 'webdav_login': webdav_login, - 'webdav_password': webdav_password - } - elif login_type == 'JianGuoYun': - options = { - 'webdav_hostname': "https://dav.jianguoyun.com/dav/", - 'webdav_login': webdav_login, - 'webdav_password': webdav_password - } - elif login_type == 'WebDav': - webdav_hostname = webdav_hostname + '/' if webdav_hostname[-1] == '/' else webdav_hostname - options = { - 'webdav_hostname': webdav_hostname, - 'webdav_login': webdav_login, - 'webdav_password': webdav_password - } - + options = { + 'webdav_hostname': webdav_hostname + "remote.php/dav/files/admin/", + 'webdav_login': webdav_login, + 'webdav_password': webdav_password, + } client = Client(options) # client.execute_request('list') list1 = client.list('/') diff --git a/utils/Sync.py b/utils/Sync.py index 6c28b00..94ad787 100644 --- a/utils/Sync.py +++ b/utils/Sync.py @@ -1,13 +1,88 @@ +import os +import platform +from webdav3.exceptions import LocalResourceNotFound from webdav3.client import Client +import configparser -# todo : 需要的参数有同步类型 +# todo : 暂时只有手动同步,没有做自动同步,暂时不需要传递参数 class Sync: - """ - :param type 同步的账号类型 - """ - def __init__(self, type): + 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) + # client.execute_request('list') + 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'