pyqt-todolist/utils/BasicUtils.py

252 lines
8.2 KiB
Python

import base64
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/PyQtToDoList/'
if not os.path.exists(work_path):
os.mkdir(work_path)
return work_path
def return_config_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/'
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']
password = str(base64.b64decode(account['webdav_password']), 'utf-8')
if account:
return [account['webdav_hostname'], account['webdav_login'], password, account['type']]
except KeyError as e:
return False
except UnicodeError 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/'
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', encoding='utf-8') as f:
# print(f.read())
json_file = json.load(f)
if json_file['itemType'] == 'ToDoList':
return_todo_list.append(
[json_file['title'], json_file['uid'], json_file['updatedAt'], json_file['icon']])
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', encoding='utf-8') 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', encoding='utf-8') 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:
json_file = json.load(f)
json_file[key] = value
json_file['updatedAt'] = current_time
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', encoding='utf-8') as f:
json_file = json.load(f)
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:
with open(os.path.join(root, name), 'r') as f:
json_file = json.load(f)
if json_file['itemType'] == 'Todo' and json_file['todoListUid'] == uid:
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('<BasicUtils>', 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('<BasicUtils>', 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
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())