diff --git a/Dockerfile b/Dockerfile index 0495e5f..75f028b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,4 @@ WORKDIR /patterns #COPY . . -CMD [ "python", "./teacher_controller.py" ] \ No newline at end of file +CMD [ "python", "./information_handler.py" ] \ No newline at end of file diff --git a/README.md b/README.md index c9ae150..a7702c2 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ - Singleton - Factory - Mediator +- Information Expert +- Bridge +- High Cohesion +- Composite ##### About the code @@ -26,14 +30,14 @@ As this code will be later plugged into other code so the error handling is done ##### How to run this code -- The driver class for this code is - `teacher_controller.py` +- The driver file for this code is + `information_handler.py` - This code requires some configurations to run, so they need to be added correctly in `config.json` our code provides a sample file with the sample required values named as `config_sample.json` - You need to do the above step according to the values in the database but sample config file currently contains correct data according to current database file and for now the db file name can only be changed by changing code file - This code can be run in two ways. First by [`python >= 3`](https://www.python.org/downloads/) (needs to be installed) by using following command in the same directory as the code - `python ./teacher_controller.py` + `python ./information_handler.py` - The other way this code can be run is by using the [`docker`](https://www.docker.com/) install by clicking on it if not - First build the image by using following command in the same directory as code diff --git a/config.json b/config.json index bc3636a..4d49ac1 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,4 @@ { "email": "teacher1@pucit.edu.pk", - "message": "Bidlksfklsdfion" + "message": "New Update he he again" } \ No newline at end of file diff --git a/information_handler.py b/information_handler.py new file mode 100644 index 0000000..c144374 --- /dev/null +++ b/information_handler.py @@ -0,0 +1,23 @@ +import json +from json import JSONDecodeError + +from teacher_controller import TeacherController + +if __name__ == "__main__": + + try: + with open("config.json", mode="r") as file: + raw_data = file.read() + try: + config_data: dict = json.loads(raw_data) + except JSONDecodeError as e: + raise Exception("The added configuration file has some errors. Consult from sample config file") + except FileNotFoundError: + raise Exception("The provided config file does not exist. Please create a config file using the sample config " + "file") + + if len(config_data) == 2 and config_data.keys().__contains__("email") and config_data.keys().__contains__("message"): + TeacherController(config_data['email']).notify(config_data['message']) + else: + raise Exception("Provided config file either does not contains required data and has extra or wrong data. " + "Please consult from sample config file and provide correct data") \ No newline at end of file diff --git a/notifications.py b/notifications.py index c6a2845..91853f7 100644 --- a/notifications.py +++ b/notifications.py @@ -70,6 +70,7 @@ def get_notification(cls, notification_type: NotificationType): else: raise Exception("Adapter Type Undefined") + # Mediator Pattern implementation class NotificationMediator: @@ -77,3 +78,12 @@ class NotificationMediator: def notify(self, sender, receivers, message): for receiver in receivers: receiver.get_notified(message, sender) + + +class NotificationAPIBridge: + + def __init__(self): + self.notification_mediator = NotificationMediator() + + def send_notification(self, sender, receivers, message): + self.notification_mediator.notify(sender, receivers, message) diff --git a/person.py b/person.py new file mode 100644 index 0000000..7b3062a --- /dev/null +++ b/person.py @@ -0,0 +1,25 @@ +from abc import ABC, abstractmethod + + +class Person(ABC): + + def __init__(self, id=None, email=None, name=None): + self.id = id + self.name = name + self.email = email + + @abstractmethod + def update(self): + pass + + @abstractmethod + def create(self): + pass + + @abstractmethod + def delete(self): + pass + + @abstractmethod + def view(self): + pass diff --git a/student.py b/student.py index addaff2..685ea50 100644 --- a/student.py +++ b/student.py @@ -2,14 +2,14 @@ # Data class for student +from person import Person -class Student: + +class Student(Person): def __init__(self, id=None, email=None, name=None, roll_number=None, batch=None, section=None, cgpa=None, semester=None, prefered_notification=None): - self.id = id - self.name = name - self.email = email + super().__init__(id=id, email=name, name=email) self.preferred_notification = prefered_notification self.batch = batch self.semester = semester diff --git a/teacher.py b/teacher.py index 7fbed4d..3873828 100644 --- a/teacher.py +++ b/teacher.py @@ -1,11 +1,12 @@ -class Teacher: +from person import Person + + +class Teacher(Person): # Data class for Teacher def __init__(self, id=None, name=None, email=None, semester=None, preferred_notification=None): - self.id = id - self.name = name - self.email = email + super().__init__(id=id, email=email, name=name) self.semester = semester self.preferred_notification = preferred_notification diff --git a/teacher_controller.py b/teacher_controller.py index db32f27..f12e0a9 100644 --- a/teacher_controller.py +++ b/teacher_controller.py @@ -1,7 +1,7 @@ from json import JSONDecodeError from database import Database -from notifications import NotificationMediator +from notifications import NotificationAPIBridge from teacher import Teacher from student import Student import json @@ -23,7 +23,7 @@ def __init__(self, email): else: raise Exception("Teacher not found in the database") - self.notification_mediator = NotificationMediator() + self.notification_api_bridge = NotificationAPIBridge() def get_students(self): return Student.from_list(Database().get_generic_data("student", "semester", self.teacher.semester)) @@ -31,28 +31,11 @@ def get_students(self): def notify(self, message): if self.teacher is not None: if len(self.students) != 0: - self.notification_mediator.notify(self.teacher, self.students, message) + self.notification_api_bridge.send_notification(self.teacher, self.students, message) else: raise Exception("This teacher does not have any students to send the notification to") else: raise Exception("Teacher not found in the database") -if __name__ == "__main__": - - try: - with open("config.json", mode="r") as file: - raw_data = file.read() - try: - config_data: dict = json.loads(raw_data) - except JSONDecodeError as e: - raise Exception("The added configuration file has some errors. Consult from sample config file") - except FileNotFoundError: - raise Exception("The provided config file does not exist. Please create a config file using the sample config " - "file") - - if len(config_data) == 2 and config_data.keys().__contains__("email") and config_data.keys().__contains__("message"): - TeacherController(config_data['email']).notify(config_data['message']) - else: - raise Exception("Provided config file either does not contains required data and has extra or wrong data. " - "Please consult from sample config file and provide correct data") +