-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_crud.py
81 lines (68 loc) · 1.92 KB
/
file_crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from fastapi.encoders import jsonable_encoder
import secrets, os, json
from model import Memo, cats
from translate import translate
from classificate import classificate
def create_memo_file(memo:Memo): # Create
id = secrets.token_hex(nbytes=16)
fpath = "./memo/"+id
while os.path.isfile(fpath):
id = secrets.token_hex(nbytes=16)
fpath = "./memo/"+id
# 번역 및 분류
text_en = translate(memo.text)
category = classificate(text_en, cats)
contents = {
"id": id,
"author": memo.author,
"text": memo.text,
"text_en": text_en,
"category": category,
}
fout = open(fpath, 'w')
fout.write(json.dumps(contents, ensure_ascii=False))
fout.close()
return contents
def list_memo_file(): # Read All
dpath = './memo/'
flist = os.listdir(dpath)
memoList = []
for f in flist:
fpath = dpath+f
fin = open(fpath, 'r')
contents = json.load(fin)
memoList.append(contents)
fin.close()
return jsonable_encoder(memoList)
def read_memo_file(id:str): # Read One
fpath = './memo/'+id
if os.path.isfile(fpath):
fin = open(fpath, 'r')
contents = json.load(fin)
return jsonable_encoder(contents)
else:
return None
def update_memo_file(id:str, memo:Memo): # Update
fpath = "./memo/"+id
if os.path.isfile(fpath):
os.remove(fpath)
contents = {
"id": id,
"author": memo.author,
"text": memo.text,
"catogry": None
}
if not os.path.isfile(fpath):
fout = open(fpath, 'w')
fout.write(json.dumps(contents, ensure_ascii=False))
fout.close()
return contents
else:
return None
def delete_memo_file(id:str): # Delete
fpath = './memo/'+id
if os.path.isfile(fpath):
os.remove(fpath)
return {"msg" : "delete complete"}
else:
return None