88
99import sys
1010import requests
11- import validators
11+ import re
1212
1313from datetime import date , datetime
1414
3434 Codes ,
3535)
3636
37+ url_regex = re .compile (r"^(https?|ftp)://[^\s/$.?#].[^\s]*$" )
38+
3739
3840def fetch_json_data (json_url ):
3941 response = requests .get (json_url )
@@ -63,7 +65,10 @@ def insert_courses_from_json(session, courses_data):
6365 if existing_course .name != course_name :
6466 existing_course .name = course_name
6567 else :
66- new_courses .append (Courses (code = course_code , name = course_name ))
68+ new_course = Courses ()
69+ new_course .code = course_code
70+ new_course .name = course_name
71+ new_courses .append (new_course )
6772
6873 if new_courses :
6974 session .add_all (new_courses )
@@ -91,9 +96,10 @@ def insert_schools_and_departments(session, schools_data):
9196 if school .description != school_description :
9297 school .description = school_description
9398 else :
94- new_schools .append (
95- RPISchools (name = school_name , description = school_description )
96- )
99+ new_school = RPISchools ()
100+ new_school .name = school_name
101+ new_school .description = school_description
102+ new_schools .append (new_school )
97103
98104 for department_data in school_data .get ("depts" , []):
99105 department_id = department_data .get ("code" )
@@ -110,27 +116,24 @@ def insert_schools_and_departments(session, schools_data):
110116 if department .school_id != school_name :
111117 department .school_id = school_name
112118 else :
113- new_depts .append (
114- RPIDepartments (
115- id = department_id ,
116- name = department_name ,
117- description = department_description ,
118- school_id = school_name ,
119- )
120- )
119+ new_department = RPIDepartments ()
120+ new_department .id = department_id
121+ new_department .name = department_name
122+ new_department .description = department_description
123+ new_department .school_id = school_name
124+ new_depts .append (new_department )
121125
122126 if new_schools or new_depts :
123127 session .add_all (new_schools + new_depts )
124128 session .commit ()
125129
126130
127- def main ():
128- app = create_app ()
129-
131+ def main () -> None :
130132 if len (sys .argv ) < 2 :
131133 sys .exit ("No argument or existing argument found" )
132134
133135 if sys .argv [1 ] == "start" :
136+ app = create_app ()
134137 with app .app_context ():
135138 if db .inspect (db .engine ).get_table_names ():
136139 print ("Tables already exist." )
@@ -141,6 +144,7 @@ def main():
141144 db .create_all ()
142145
143146 elif sys .argv [1 ] == "clear" :
147+ app = create_app ()
144148 with app .app_context ():
145149 db .drop_all ()
146150
@@ -151,9 +155,10 @@ def main():
151155 j_url = sys .argv [2 ]
152156
153157 # Validate that j_url is a valid URL
154- if not validators . url (j_url ):
158+ if not url_regex . match (j_url ):
155159 sys .exit ("Error: Invalid URL provided." )
156160
161+ app = create_app ()
157162 with app .app_context ():
158163 db .create_all ()
159164
@@ -172,9 +177,10 @@ def main():
172177 j_url = sys .argv [2 ]
173178
174179 # Validate that j_url is a valid URL
175- if not validators . url (j_url ):
180+ if not url_regex . match (j_url ):
176181 sys .exit ("Error: Invalid URL provided." )
177182
183+ app = create_app ()
178184 with app .app_context ():
179185 db .create_all ()
180186
@@ -187,6 +193,7 @@ def main():
187193 db .session .close ()
188194
189195 elif sys .argv [1 ] == "create" :
196+ app = create_app ()
190197 with app .app_context ():
191198 db .create_all ()
192199
0 commit comments