-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·200 lines (173 loc) · 5.87 KB
/
console.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/python3
"""the console"""
import cmd
from datetime import datetime
import models
from models.user import User
from models.garbage_type import Garbage_type
from models.garbage_collection_company import Garbage_collection_company
from models.base_model import BaseModel
import shlex # for spliting purposes except when there is double quotes
import argparse
import mysql.connector
import re
classes = {"User": User, "Garbage_type": Garbage_type, "Garbage_collection_company": Garbage_collection_company, "BaseModel": BaseModel}
class MAZINGIRABORACommand(cmd.Cmd):
"""MAZINGIRA comsole"""
prompt = '(mazingirabora)'
def do_EOF(self, arg):
"""exits console"""
return True
def emptyline(self):
"""overwrites an empty line"""
return False
def do_quit(self, arg):
"""quits command to the program"""
return True
def _key_value_parser(self, args):
"""creates dictionary from list of strings"""
new_dict = {}
for arg in args:
if "=" in arg:
kvp = arg.split('=', 1)
key = kvp[0]
value = kvp[1]
if value[0] == value[-1] == '"':
value = shlex.split(value)[0]
else:
try:
value = int(value)
except:
try:
value = float(value)
except:
continue
new_dict[key] = value
return new_dict
def do_create(self, arg):
"""creates new class instance"""
args = arg.split()
if len(args) == 0:
print("*class name missing*")
return False
if args[0] in classes:
new_dict = self._key_value_parser(args[1:])
instance = classes[args[0]](**new_dict)
else:
print("*class doesn't exist*")
return False
print(instance.id)
instance.save()
def do_show(self, arg):
""" prints instances based on class and id"""
args = shlex.split(arg)
if len(args) == 0:
print("*class name missing*")
return False
if args[0] in classes:
if len(args) > 1:
key = args[0] + "." + args[1]
if key in models.storage.all():
print(models.storage.all()[key])
else:
print("*no instance found*")
else:
print("*instance id missing*")
else:
print("*class doesn't exist*")
# establish a mysql connection
conn = mysql.connector.connect(
host="",
user="",
password="",
database=""
)
cursor = conn.cursor()
# create user table
create_table_query = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
phone VARCHAR(15),
password VARCHAR(255)
)
"""
create_table_query = """
CREATE TABLE IF NOT EXISTS garbage_collection_companies(
id INT AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(255),
garbage_type VARCHAR(255),
email VARCHAR(255),
phone VARCHAR(15),
location VARCHA(255)
)
"""
create_table_query = """
CREATE TABLE IF NOT EXISTS garbage_types(
id INT AUTO_INCREMENT PRIMARY KEY,
garbage_type VARCHAR(255),
price_sheet VARCHAR(255)
)
"""
cursor.execute(create_table_query)
conn.commit()
def do_register():
"""collecting user information"""
first_name = input("Enter your first name:")
last_name = input("Entee your last name:")
email = input("Enter valid email:")
phone = input("Enter phone number:")
password = input("Create password:")
if not re.match(
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
email):
print("Invalid email. Enter valid email.")
return
if len(password) < 8:
print("Password must be atleast 8 characters long.")
return
# insert user data into database
insert_query = """
INSERT INTO users (first_name, last_name, email, phone, pslassword)
VALUES (%s, %s, %s, %s, %s)
"""
user_data = (first_name, last_name, email, phone, password)
cursor.execute(insert_query, user_data)
conn.commit()
print("Registration succesful.")
do_register()
conn.close()
def do_update(self, args):
"""updating data set"""
args = shlex.split(arg)
integers = ["Amount_of_waste", "pick_up_date"]
floats = ["type_of_waste", "location"]
if len(args) == 0:
print("*class name missing*")
elif args[0] in classes:
if len(args) > 1:
k = args[0] + "." + args[1]
if k in models.storage.all():
if len(args) > 2:
if len(args) > 3:
if args[0] == "Location":
if args[2] in integers:
try:
args[3] = int[args[3]]
except:
args[3] = 0.0
setattr(
models.storage.all()[k], args[2], args[3])
models.storage.all()[k].save()
else:
print("*value missing*")
else:
print("*attribute missing*")
else:
print("*no instance found*")
else:
print("*instance id missing*")
if __name__ == '__main__':
MAZINGIRABORACommand().cmdloop()