pyqt-todolist/utils/BasicUtils.py
2022-04-21 22:42:43 +08:00

133 lines
4.7 KiB
Python

import json
import os
import platform
from datetime import datetime
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 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']])
# print(return_todo_list)
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']])
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']])
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']])
return return_todo
# 用于修改某个键值对,比如更新修改时期,设置为重要,我的一天等
def change_value(uid, key, value):
config_path = return_work_dir()
todo_path = config_path + 'ToDoList/'
# for root, dirs, files in os.walk(todo_path, topdown=False):
# for name in files:
# print(os.path.join(root, name))
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)
#
# 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'))