Source code for bast.route

"""
    Bast Web Framework
    (c) Majiyagbe Oluwole <oluwole564@gmail.com>

    For full copyright and license information, view the LICENSE distributed with the Source Code
"""
import importlib


[docs]class Route(object): """ Route Class. Appends the URL, Controller Instance and Method to a list instance to be passed on to the server instance """ url_ = None controller = None method = None controller_location = 'controller' url = [] request_type = "" # def prefix(self, pref): # return self
[docs] def middleware(self, args): """ Appends a Middleware to the route which is to be executed before the route runs """ if self.url[(len(self.url) - 1)] == (self.url_, self.controller, dict(method=self.method, request_type=self.request_type, middleware=None)): self.url.pop() self.url.append((self.url_, self.controller, dict(method=self.method, request_type=self.request_type, middleware=args))) return self
[docs] def __return_controller__(self, controller): if isinstance(controller, str): ctr = controller.split('.') if ctr[0].startswith('/'): self.controller_location = '.'.join(ctr[0].replace('/', '').split('.')[0:-1]) else: raise Exception get_controller = ctr[0].split('.')[-1] try: # Import the module if isinstance(controller, str): controller_name = importlib.import_module('{0}.'.format(self.controller_location) + get_controller) else: controller_name = importlib.import_module('{0}'.format(self.controller_location)) # Get the controller from the module controller_class = getattr(controller_name, get_controller) # Get the controller method from the class which in this case is the string controller_method = ctr[1] return controller_class, controller_method except Exception as e: print('\033[93mError in your routes/link.py!', e, '\033[0m')
[docs] def get(self, url, controller): """ Gets the Controller and adds the route, controller and method to the url list for GET request """ self.request_type = 'GET' controller_class, controller_method = self.__return_controller__(controller) self.controller = controller_class self.method = controller_method self.url_ = url self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None))) return self
[docs] def post(self, url, controller): """ Gets the Controller and adds the route, controller and method to the url list for the POST request """ self.request_type = "POST" controller_class, controller_method = self.__return_controller__(controller) self.controller = controller_class self.method = controller_method self.url_ = url self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None))) return self
[docs] def put(self, url, controller): """ Gets the Controller and adds the route, controller and method to the url list for PUT request """ self.request_type = "PUT" controller_class, controller_method = self.__return_controller__(controller) self.controller = controller_class self.method = controller_method self.url_ = url self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None))) return self
[docs] def delete(self, url, controller): """ Gets the Controller and adds the route, controller and method to the url list for the DELETE request """ self.request_type = "DELETE" controller_class, controller_method = self.__return_controller__(controller) self.controller = controller_class self.method = controller_method self.url_ = url self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None))) return self
[docs] def all(self): """ Returns the list of URL. Used by Server to get the list of URLS. This is passed to the Bast HTTP Server """ return self