-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
59 lines (44 loc) · 1.35 KB
/
utils.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
from pathlib import Path
import json
def extract_route(request):
lines = request.split('\n')
first_line = lines[0]
parts = first_line.split()
route = parts[1][1:]
return route
def read_file(file_path: Path) -> bytes:
with open(file_path, 'rb') as file:
content = file.read()
return content
def load_data(filename):
filepath = "data/"+filename
with open(filepath, 'r') as file:
data = json.load(file)
return data
def load_template(filename):
filepath = "templates/"+filename
with open(filepath, 'r') as file:
content = file.read()
return content
def add(titulo, detalhes):
"""
Adiciona uma anotação ao arquivo notes.json.
Args:
titulo: O título da anotação.
detalhes: A descrição da anotação.
Returns:
None.
"""
with open("data/notes.json", "r") as f:
notes = json.load(f)
notes.append({"titulo": titulo, "detalhes": detalhes})
with open("data/notes.json", "w") as f:
json.dump(notes, f, indent=2)
def build_response(body='', code=200, reason='OK', headers=''):
if body:
response = "HTTP/1.1 {} {}\n\n{}".format(code, reason, body)
elif headers:
response = "HTTP/1.1 {} {}\n{}\n\n".format(code, reason, headers)
else:
response = "HTTP/1.1 {} {}\n\n".format(code, reason)
return response.encode()