Skip to content

Commit

Permalink
Merge pull request #13 from moluwole/1.0.0
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
moluwole committed Aug 23, 2018
2 parents dd2432f + c3d3699 commit 52ccc03
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 87 deletions.
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BAST Framework



|travis| |circleci| |python| |license| |coverall| |status| |issues| |contributors| |downloads|
|travis| |circleci| |pversion| |license| |coverall| |status| |issues| |contributors| |downloads|


About Bast
Expand Down Expand Up @@ -108,7 +108,6 @@ Eloquent Object Relation Mapping is achieved using `Orator ORM`_
.. _Orator ORM: https://orator-orm.com
.. |travis| image:: https://travis-ci.org/moluwole/Bast.svg?branch=master
.. |circleci| image:: https://circleci.com/gh/moluwole/Bast.svg?style=svg
.. |python| image:: https://img.shields.io/badge/python-3.4+-blue.svg
.. |license| image:: https://img.shields.io/github/license/moluwole/bast.svg
.. |pversion| image:: https://img.shields.io/pypi/pyversions/Bast.svg
.. |status| image:: https://img.shields.io/pypi/status/Bast.svg
Expand Down
26 changes: 15 additions & 11 deletions bast/bast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from tornado.options import define, options, parse_command_line
from tornado.web import Application, StaticFileHandler
from .environment import load_env
from .session import MemorySession
from .session import FileSession
from colorama import init, Fore

__author__ = "Majiyagbe Oluwole"
Expand All @@ -27,10 +29,14 @@ class Bast(Application):
css_folder = ""
template_folder = ""

providers = {}
session = {}
host = None
port = None
debug = None

def __init__(self, route, **settings):
providers = {}
session = {}

def __init__(self, route):
"""
Bast Server Class. Runs on Tornado HTTP Server (http://www.tornadoweb.org/en/stable/)
Expand All @@ -41,17 +47,17 @@ def __init__(self, route, **settings):
Appropriate configurations are loaded from the config file into the os environment for use
:param route:
"""
# self.settings = settings
super().__init__(**settings)

super(Bast, self).__init__()
init()
self.host = '127.0.0.1'
self.port = 2000
self.debug = True

# self.load_config()
load_env()
self.config()

self.host = os.getenv("HOST", "127.0.0.1")
self.port = os.getenv("PORT", 2000)
self.debug = os.getenv("DEBUG", True)

self.handler = route.all().url
self.handler.append((r'/css/(.*)', StaticFileHandler, {"path": self.css_folder}))
self.handler.append((r'/script/(.*)', StaticFileHandler, {"path": self.script_folder}))
Expand Down Expand Up @@ -87,10 +93,8 @@ def config(self):

static_files = config.STATIC_FILES
if config.SESSION_DRIVER is 'memory':
from bast import MemorySession
self.session.update({"session": MemorySession()})
else:
from bast import FileSession
self.session.update({'session': FileSession()})

# providers = provider.providers
Expand Down
4 changes: 2 additions & 2 deletions bast/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import click
import re
from subprocess import call
from secrets import token_hex
from base64 import b64encode
from colorama import init, Fore, Back

""" Handles the CLI commands and their respective Arguments """
Expand Down Expand Up @@ -107,7 +107,7 @@ def make_key(path):
click.echo(Fore.RED + ".env file not found. Scaffold a project to generate a key")
return

key = token_hex(16)
key = b64encode(os.urandom(32)).decode('utf-8')
with open(env_path, 'r') as file:
env_data = file.readlines()

Expand Down
108 changes: 51 additions & 57 deletions bast/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@


class Controller(RequestHandler, TemplateRendering):
method = None
middleware = None
providers = {}
method = None
middleware = None
providers = {}
request_type = None

def __init__(self, application, request, **kwargs):
super().__init__(application, request, **kwargs)
super(Controller, self).__init__(application, request, **kwargs)
self.request = request
self.application = application
self.session_driver = os.getenv("SESSION")
Expand Down Expand Up @@ -94,13 +95,14 @@ def __run_middleware__(self, middleware_list):

return return_value

def initialize(self, method, middleware):
def initialize(self, method, middleware, request_type):
"""
Overridden initialize method from Tornado. Assigns the controller method and middleware attached to the route being executed
to global variables to be used
"""
self.method = method
self.middleware = middleware
self.request_type = request_type

def only(self, arguments):
"""
Expand Down Expand Up @@ -186,67 +188,59 @@ def json(self, data):

@coroutine
def get(self, *args, **kwargs):
try:
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Not Found")
except AttributeError as e:
logging.error(str(e))
raise BastException(500, "Controller Function not found")
if self.request_type is not 'GET':
raise BastException(405, "Wrong Method. Expected Request Method: %s" % self.request_type)
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Controller Function Not Found")

@coroutine
def post(self, *args, **kwargs):
try:
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Not Found")
except AttributeError as e:
logging.error(str(e))
raise BastException(500, "Controller Function not found")
if self.request_type is not 'POST':
raise BastException(405, "Wrong Method. Expected Request Method: %s" % self.request_type)
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Controller Function Not Found")

@coroutine
def put(self, *args, **kwargs):
try:
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Not Found")
except AttributeError as e:
logging.error(str(e))
raise BastException(500, "Controller Function not found")
if self.request_type is not 'PUT':
raise BastException(405, "Wrong Method. Expected Request Method: %s" % self.request_type)
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Controller Function Not Found")

@coroutine
def delete(self, *args, **kwargs):
try:
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Not Found")
except AttributeError as e:
logging.error(str(e))
raise BastException(500, "Controller Function not found")
if self.request_type is not 'DELETE':
raise BastException(405, "Wrong Method. Expected Request Method: %s" % self.request_type)
if self.middleware is not None and len(self.middleware) > 0:
value = self.__run_middleware__(self.middleware)
if not value:
return
func = getattr(self, self.method)
if func:
func()
else:
raise BastException(404, "Controller Function Not Found")

def get_argument(self, name, default=None, strip=True):
"""
Expand Down
19 changes: 13 additions & 6 deletions bast/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Route(object):
method = None
controller_location = 'controller'
url = []
request_type = ""

# def prefix(self, pref):
# return self
Expand All @@ -27,9 +28,9 @@ 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, middleware=None)):
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, middleware=args)))
self.url.append((self.url_, self.controller, dict(method=self.method, request_type=self.request_type, middleware=args)))
return self

def __return_controller__(self, controller):
Expand Down Expand Up @@ -62,52 +63,58 @@ 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, middleware=None)))
self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None)))
return self

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, middleware=None)))
self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None)))
return self

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, middleware=None)))
self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None)))
return self

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, middleware=None)))
self.url.append((url, controller_class, dict(method=controller_method, request_type=self.request_type, middleware=None)))
return self

def all(self):
Expand Down
11 changes: 7 additions & 4 deletions bast/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ class TemplateRendering:
Base class to load the template directory from the OS Environment TEMPLATE_FOLDER variable
"""

dict_object = {}

def __init__(self):
self.dictionary = {}
self.dict_object = {}

def add_(self, key, _dict_):
self.dictionary[key] = _dict_
self.dict_object[key] = _dict_
return self

def render_template(self, template_name, **kwargs):
Expand All @@ -70,18 +72,19 @@ def render_template(self, template_name, **kwargs):
env.globals['css'] = css
env.globals['script'] = script

self.dictionary.update(**kwargs)
self.dict_object.update(**kwargs)

try:
template = env.get_template(template_name)
except TemplateNotFound:
raise TemplateNotFound(template_name)
content = template.render(self.dictionary)
content = template.render(self.dict_object)
return content

@classmethod
def render_exception(cls, **kwargs):
template_dir = os.path.dirname(os.path.realpath(__file__)) + "/exception"
print(template_dir)
env = Environment(loader=FileSystemLoader(template_dir))

try:
Expand Down
8 changes: 3 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def readme():


setup(name='Bast',
version='0.0.1.1.5',
version='1.0.1',
description='Simple yet Elegant Web Framework with MVC Patterns',
long_description=readme(),
classifiers=[
Expand All @@ -17,11 +17,9 @@ def readme():
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.0',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
Expand Down

0 comments on commit 52ccc03

Please sign in to comment.