import json import os import platform from datetime import datetime import configparser def return_work_dir(): if platform.system() == 'Linux': work_path = os.path.expandvars('$HOME') + '/.config/' if not os.path.exists(work_path): os.mkdir(work_path) return os.path.expandvars('$HOME') + '/.config/PyQtToDoList/' elif platform.system() == 'Windows': work_path = os.getcwd() + '/../config/' if not os.path.exists(work_path): os.mkdir(work_path) return work_path import configparser def read_init_file(): config = configparser.ConfigParser() try: config.read(return_work_dir() + 'PyQtToDoList.ini') account = config['Account'] if account: return [account['webdav_hostname'], account['webdav_login'], account['webdav_password'], account['type']] except KeyError as e: return False def change_myday_important_conf(change_type, key, value): value = str(value) config = configparser.ConfigParser() config_path = return_work_dir() todo_file = config_path + 'PyQtToDoList.conf' if not os.path.exists(todo_file): # print(True) myday = {"Theme": "0"} important = {"Theme": "0"} config['MyDay'] = myday config['Important'] = important with open(todo_file, 'w') as f: config.write(f) config.read(todo_file) config[change_type][key] = value with open(todo_file, 'w') as f: config.write(f) def get_myday_important_conf(get_type, key): config = configparser.ConfigParser() config_path = return_work_dir() try: todo_file = config_path + 'PyQtToDoList.conf' config.read(todo_file) return config[get_type][key] except KeyError as e: print('File Not Found!') return False def get_todo_list(): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' # print(todo_path) return_todo_list = [] for root, dirs, files in os.walk(todo_path, topdown=False): for name in files: # print(os.path.join(root, name)) with open(os.path.join(root, name), 'r') as f: # print(f.read()) json_file = json.load(f) # try: if json_file['itemType'] == 'ToDoList': # print(json_file['title'], json_file['uid'], '列表') return_todo_list.append( [json_file['title'], json_file['uid'], json_file['updatedAt'], json_file['icon']]) # print(return_todo_list) return_todo_list.sort(key=get_third) return return_todo_list def get_todo(todoListUid): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' # print(todo_path) # config = configparser.ConfigParser() return_todo = [] for root, dirs, files in os.walk(todo_path, topdown=False): for name in files: # print(os.path.join(root, name)) with open(os.path.join(root, name), 'r') as f: # print(f.read()) json_file = json.load(f) # print(json_file['itemType']) if json_file['itemType'] == 'Todo' and not json_file['done'] and json_file[ 'todoListUid'] == todoListUid: # print(json_file['title'], json_file['uid'], '待办事项') return_todo.append([json_file['title'], json_file['uid'], json_file['updatedAt']]) return_todo.sort(key=get_third) return return_todo # load_list = get_todo_list() def load_myday_important(item_type): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' # print(todo_path) # config = configparser.ConfigParser() return_todo = [] for root, dirs, files in os.walk(todo_path, topdown=False): for name in files: # print(os.path.join(root, name)) with open(os.path.join(root, name), 'r') as f: # print(f.read()) json_file = json.load(f) # print(json_file['itemType']) if item_type == 'MyDay': if json_file['itemType'] == 'Todo' and not json_file['done'] and json_file['isMyDay']: # print(json_file['title'], json_file['uid'], '待办事项') return_todo.append([json_file['title'], json_file['uid'], json_file['updatedAt']]) # return return_todo elif item_type == 'Important': if json_file['itemType'] == 'Todo' and not json_file['done'] and json_file['isImportant']: # print(json_file['title'], json_file['uid'], '待办事项') return_todo.append([json_file['title'], json_file['uid'], json_file['updatedAt']]) # print(return_todo) return_todo.sort(key=get_third) return return_todo def get_third(elem): return elem[2] # 用于修改某个键值对,比如更新修改时期,设置为重要,我的一天等 def change_value(uid, key, value): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' json_file = {} current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') with open(todo_path + '{' + uid + '}.otl', 'r') as f: # print(f.read()) json_file = json.load(f) # print(''+json_file) json_file[key] = value json_file['updatedAt'] = current_time # print(json_file[key]) with open(todo_path + '{' + uid + '}.otl', 'w') as f: json.dump(json_file, f, indent=4, ensure_ascii=False) def load_value(uid, key): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' with open(todo_path + '{' + uid + '}.otl', 'r') as f: # print(f.read()) json_file = json.load(f) # print(''+json_file) return json_file[key] def remove_todo_list(uid): config_path = return_work_dir() todo_path = config_path + 'ToDoList/' os.remove(todo_path + '{' + uid + '}.otl') for root, dirs, files in os.walk(todo_path, topdown=False): for name in files: # print(os.path.join(root, name)) with open(os.path.join(root, name), 'r') as f: # print(f.read()) json_file = json.load(f) # print(json_file['itemType']) if json_file['itemType'] == 'Todo' and json_file['todoListUid'] == uid: # print(os.path.join(root, name)) os.remove(os.path.join(root, name)) def read_ini(section, key): config_path = return_work_dir() try: ini_path = config_path + 'PyQtToDoList.ini' config = configparser.ConfigParser() config.read(ini_path) return config[section][key] except Exception as e: print('', e) return False def set_exit_status(status): config_path = return_work_dir() try: ini_path = config_path + 'PyQtToDoList.ini' config = configparser.ConfigParser() config.read(ini_path) config['System']['exitstatus'] = status # print(config['S']) with open(ini_path, 'w') as f: config.write(f) return True except Exception as e: print('', e) return False # 判断是否登录 def check_login_status(): try: # work_dir = return_work_dir() init_file_list = read_init_file() print(init_file_list) if init_file_list and 'None' not in init_file_list: return True else: return False # # for i in init_file_list: # if i == 'None': # return False # else: # return True except TypeError as e: return False # print(read_init_file()) # webdav_hostname = read_init_file() # print(webdav_hostname[2]) # change_value('4f52f02b-4450-405a-9fd1-2005a03006e0', 'isMyDay', False) # print(get_todo('3f1e033f-2051-4c04-b7f6-afd2eb1f54f4')) # print(load_myday_important('Important')) # change_myday_important_conf('Important', 'theme', 2) # print(get_myday_important_conf('Important','Theme')) # print(read_ini('System', 'exitstatus')) # print(set_exit_status('Min')) # print(check_login_status())