代码拉取完成,页面将自动刷新
import service
from Entry import *
from common import cmdParser, fileSelector, showImage
from wordbank import wordbank, printWord
def printNote(entry: Entry, query: bool = False):
print("===============NOTE===============")
print("编号:", entry.entryId)
print("类型:", EntryTypes[entry.type])
if entry.type == 1:
print("单词:", entry.title)
print("释义:")
elif entry.type == 2 or entry.type == 4:
print("标题:", entry.title)
print("内容:")
elif entry.type == 3:
print("内容:")
print(entry.content)
if entry.extra:
hasExtra = "有"
else:
hasExtra = "无"
print("附加图片:", hasExtra)
print("==================================")
if query and entry.type == 1:
print("词典查询:")
printWord(entry.title)
print("==================================")
def init():
print("[init] Connecting to database...")
service.initNotebook()
service.initWordbank()
print("[init] Done!")
def addNote():
print("笔记类型:")
print("""
1. 单词
2. 语法
3. 词句积累
4. 其他
""")
entry = Entry()
while True:
try:
entry.type = int(input("请选择笔记类型:"))
if entry.type not in (1, 2, 3, 4):
raise ValueError
except ValueError:
print("输入错误!")
continue
break
if entry.type == 1:
entry.title = input("请输入单词:")
entry.content = input("请输入释义:")
elif entry.type == 2 or entry.type == 4:
entry.title = input("请输入标题:")
entry.content = input("请输入内容:")
elif entry.type == 3:
entry.content = input("请输入内容:")
result = service.addNote(entry)
if result > 0:
print("[addNote] Success!")
else:
print("[addNote] Error!")
def showNote(query: bool = False):
result: list[Entry] = service.getAllNotes()
for entry in result:
printNote(entry, query)
def deleteNote():
while True:
try:
entryId = int(input("请输入笔记编号:"))
except ValueError:
print("输入错误!")
continue
break
result = service.deleteNoteById(entryId)
if result > 0:
print("[deleteNote] Success!")
else:
print("[deleteNote] Error!")
def editNote():
while True:
try:
entryId = int(input("请输入笔记编号:"))
except ValueError:
print("输入错误!")
continue
break
entry = service.getNoteById(entryId)
if not entry:
print("ID错误!")
return
printNote(entry)
while True:
try:
entry.type = int(input("请选择笔记类型:"))
if entry.type not in (1, 2, 3, 4):
raise ValueError
except ValueError:
print("输入错误!")
continue
break
if entry.type == 1:
entry.title = input("请输入单词:")
entry.content = input("请输入释义:")
elif entry.type == 2 or entry.type == 4:
entry.title = input("请输入标题:")
entry.content = input("请输入内容:")
elif entry.type == 3:
entry.content = input("请输入内容:")
result = service.updateNoteById(entry)
if result > 0:
print("[editNote] Success!")
else:
print("[editNote] Error!")
def attachNote(remote: bool = False):
while True:
try:
entryId = int(input("请输入笔记编号:"))
except ValueError:
print("输入错误!")
continue
break
entry = service.getNoteById(entryId)
if not entry:
print("ID错误!")
return
entry.extra = fileSelector(remote)
result = service.updateNoteById(entry)
if result > 0:
print("[attachNote] Success!")
else:
print("[attachNote] Error!")
def viewImage(remote: bool = False):
while True:
try:
entryId = int(input("请输入笔记编号:"))
except ValueError:
print("输入错误!")
continue
break
entry = service.getNoteById(entryId)
if not entry:
print("ID错误!")
return
if not entry.extra:
print("该笔记无附加图片!")
return
showImage(entry.extra, remote)
def detachNote():
while True:
try:
entryId = int(input("请输入笔记编号:"))
except ValueError:
print("输入错误!")
continue
break
entry = service.getNoteById(entryId)
if not entry:
print("ID错误!")
return
entry.extra = b''
result = service.updateNoteById(entry)
if result > 0:
print("[detachNote] Success!")
else:
print("[detachNote] Error!")
def menu() -> int:
print("===============MENU===============")
print("1. 添加笔记")
print("2. 查看笔记")
print("3. 删除笔记")
print("4. 修改笔记")
print("5. 附加图片")
print("6. 查看图片")
print("7. 删除图片")
print("8. 进入命令行模式")
print("9. 进入单词库")
print("0. 退出")
print("==================================")
while True:
try:
choice = int(input("请选择操作:"))
if choice not in (1, 2, 3, 4, 5, 6, 7, 8, 9, 0):
raise ValueError
except ValueError:
print("输入错误!")
continue
break
return choice
def cmdline(cmd: str = None, remote: bool = False):
flag = False
if cmd:
flag = True
while True:
if not flag:
cmd = input("noteMgr>")
cmds = cmdParser(cmd)
if cmds[0] == "add":
try:
entry = Entry()
if cmds[1] == "word":
entry.type = 1
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
else:
raise IndexError
elif cmds[1] == "grammar":
entry.type = 2
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
else:
raise IndexError
elif cmds[1] == "accumulation":
entry.type = 3
for i in range(2, len(cmds), 2):
if cmds[i] == "--content":
entry.content = cmds[i + 1]
else:
raise IndexError
elif cmds[1] == "other":
entry.type = 4
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
else:
raise IndexError
else:
print("指令错误!")
continue
result = service.addNote(entry)
if result > 0:
print("[addNote] Success!")
else:
print("[addNote] Error!")
except IndexError:
print("参数错误!")
elif cmds[0] == "show":
query = False
showAll = True
for i in range(1, len(cmds)):
try:
if cmds[i] == "--all":
showAll = True
elif cmds[i] == "--id":
showAll = False
entry = service.getNoteById(int(cmds[i + 1]))
elif cmds[i] == "-q" or cmds[i] == "--query":
query = True
except IndexError:
print("[show] Not specific or wrong options. Assumed --all.")
showAll = True
if showAll:
showNote(query)
else:
if entry:
printNote(entry, query)
else:
print("ID错误!")
elif cmds[0] == "delete":
try:
if cmds[1] == "--id":
try:
result = service.deleteNoteById(int(cmds[2]))
if result > 0:
print("[deleteNote] Success!")
else:
print("[deleteNote] Error!")
except:
print("ID错误!")
else:
raise IndexError
except IndexError:
print("参数错误!")
elif cmds[0] == "edit":
try:
entry = service.getNoteById(int(cmds[cmds.index("--id") + 1]))
if cmds[1] == "word":
entry.type = 1
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
elif cmds[i] == "--id":
pass
else:
raise IndexError
elif cmds[1] == "grammar":
entry.type = 2
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
elif cmds[i] == "--id":
pass
else:
raise IndexError
elif cmds[1] == "accumulation":
entry.type = 3
for i in range(2, len(cmds), 2):
if cmds[i] == "--content":
entry.content = cmds[i + 1]
elif cmds[i] == "--id":
pass
else:
raise IndexError
elif cmds[1] == "other":
entry.type = 4
for i in range(2, len(cmds), 2):
if cmds[i] == "--title":
entry.title = cmds[i + 1]
elif cmds[i] == "--content":
entry.content = cmds[i + 1]
elif cmds[i] == "--id":
pass
else:
raise IndexError
else:
print("指令错误!")
continue
result = service.updateNoteById(entry)
if result > 0:
print("[editNote] Success!")
else:
print("[editNote] Error!")
except IndexError:
print("参数错误!")
except ValueError:
print("ID错误!")
elif cmds[0] == "exit":
exit(0)
elif cmds[0] == "menu":
break
elif cmds[0] == "bank":
wordbank()
elif cmds[0] == "attach":
try:
entry = service.getNoteById(int(cmds[cmds.index("--id") + 1]))
if not entry:
print("ID错误!")
continue
entry.extra = fileSelector(remote)
result = service.updateNoteById(entry)
if result > 0:
print("[attachNote] Success!")
else:
print("[attachNote] Error!")
except IndexError:
print("参数错误!")
elif cmds[0] == "view":
try:
entry = service.getNoteById(int(cmds[cmds.index("--id") + 1]))
if not entry:
print("ID错误!")
continue
if not entry.extra:
print("该笔记无附加图片!")
continue
showImage(entry.extra, remote)
except IndexError:
print("参数错误!")
elif cmds[0] == "detach":
try:
entry = service.getNoteById(int(cmds[cmds.index("--id") + 1]))
if not entry:
print("ID错误!")
continue
entry.extra = b''
result = service.updateNoteById(entry)
if result > 0:
print("[detachNote] Success!")
else:
print("[detachNote] Error!")
except IndexError:
print("参数错误!")
elif cmds[0] == "help":
print("""
Usage: <command> [options...]
Commands:
add <instructions> 添加笔记
show 查看笔记
delete 删除笔记
edit <instructions> 修改笔记
menu 进入菜单模式
bank 进入单词库
attach 附加图片
view 查看附加的图片
detach 删除附加的图片
help 显示本帮助命令
exit 退出程序
Instructions:
word 指定操作笔记类型为单词
grammar 指定操作笔记类型为语法
accumulation 指定操作笔记类型为词句积累
other 指定操作笔记类型为其他
Options:
--title <title> 指定笔记标题或单词
--content <content> 指定笔记内容或单词释义
--all 指定显示全部笔记(适用于show,为默认行为)
--id 指定笔记ID(即编号,适用于show, delete, edit)
-q, --query 对于单词执行查询操作
""")
else:
print("Unknown command! Type 'help' for usage.")
if flag:
break
def main(remote: bool = False):
print("""
★★★★★★★★★★★★★★★★★★★★
欢迎使用笔记管理工具!
©20242115叶抒宇
★★★★★★★★★★★★★★★★★★★★
""")
init()
while True:
choice = menu()
if choice == 0:
break
elif choice == 1:
addNote()
elif choice == 2:
query = input("对于单词是否查询词典?(Y/n)")
if query == 'Y' or query == 'y':
showNote(True)
else:
showNote()
elif choice == 3:
deleteNote()
elif choice == 4:
editNote()
elif choice == 5:
attachNote(remote)
elif choice == 6:
viewImage(remote)
elif choice == 7:
detachNote()
elif choice == 8:
cmdline(remote=remote)
elif choice == 9:
wordbank()
if __name__ == "__main__":
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。