This repository has been archived on 2023-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
ib-clearance/ib-clearance.py

123 lines
3.4 KiB
Python
Raw Normal View History

2018-10-09 10:01:01 +02:00
#!/usr/bin/env python3
2018-10-23 17:10:40 +02:00
# Copyright 2018 Abdulkadir Furkan Şanlı
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2018-10-09 10:01:01 +02:00
import sqlite3
import time
def connect_database(file):
"""
2018-11-04 11:33:43 +01:00
Creates and returns Connection object.
2018-10-09 10:01:01 +02:00
:param file: database file
"""
conn = sqlite3.connect(file)
return conn
def get_data(conn):
2018-10-09 10:01:01 +02:00
"""
Requests student ID and returns integer tuple (id, class) with valid
student id and corresponding class no.
2018-10-09 10:01:01 +02:00
:param conn: Connection object
"""
2018-12-30 09:28:16 +01:00
try:
id = int(input("Please enter student ID: "))
2018-10-09 10:01:01 +02:00
2018-12-30 09:28:16 +01:00
cur = conn.cursor()
cur.execute("SELECT id, student_class FROM students")
2018-12-30 09:28:16 +01:00
ids = cur.fetchall()
for x in ids:
# ids is list with single value tuples
2018-12-30 09:28:16 +01:00
if id == x[0]:
# return tuple x with valid id and corresponding class no.
return x
2018-12-30 09:28:16 +01:00
else:
raise ValueError
except ValueError:
print("Incorrect ID, try again.")
return get_data(conn)
2018-10-09 10:01:01 +02:00
def select_final_time(conn, day, stud):
2018-10-09 10:01:01 +02:00
"""
2018-11-04 11:33:43 +01:00
Returns ending time of final lesson student must attend, or None.
2018-10-09 10:01:01 +02:00
:param conn: Connection object
:param day: current weekday, lowercase str
:param stud: tuple with student ID no. and class no., int
2018-10-09 10:01:01 +02:00
"""
cur = conn.cursor()
cur.execute("""
SELECT end_time
FROM timetable
INNER JOIN students ON g1 = lesson_id
OR g2 = lesson_id
OR g3 = lesson_id
OR g4 = lesson_id
OR g5 = lesson_id
OR g6 = lesson_id
OR lesson_id = 'gu'
2018-12-01 08:35:07 +01:00
OR lesson_id = 'tok'
WHERE id = ?
AND day = ?
AND lesson_class = ?
2018-10-09 10:01:01 +02:00
ORDER BY end_time DESC
LIMIT 1
""", (stud[0], day, stud[1]))
2018-10-09 10:01:01 +02:00
finish = cur.fetchone()
if str(type(finish)) == "<class 'tuple'>":
return finish[0]
else:
return finish
def parse_time_string(timestring):
"""
2018-12-30 09:28:16 +01:00
Parses given 24h time string of format "HH:MM" into struct_time.
:param timestring: time string "HH:MM"
2018-10-09 10:01:01 +02:00
"""
structtime = time.strptime(timestring, "%H:%M")
return structtime
2018-10-09 10:01:01 +02:00
def main():
print("ib-clearance")
print("============")
2018-12-30 09:28:16 +01:00
conn = connect_database("database.db")
while True:
id = get_data(conn)
2018-12-30 09:28:16 +01:00
day = time.strftime("%A").lower()
finish_time = select_final_time(conn, day, id)
current_time = time.strftime("%H:%M")
if finish_time == None:
print("Student has no lessons today, clear to leave.")
elif parse_time_string(finish_time) < parse_time_string(current_time):
print("Student has finished for today, clear to leave.")
else:
print("Student still has lessons, clearance not granted.")
if input("Enter y to run again.") != "y":
break
2018-10-09 10:01:01 +02:00
2018-12-30 09:28:16 +01:00
# Run main() if script is run standalone.
2018-10-09 10:01:01 +02:00
if __name__ == "__main__":
main()