Skip to content

Commit

Permalink
Merge pull request #21 from moluwole/issue-20
Browse files Browse the repository at this point in the history
Resolve Issue #20
  • Loading branch information
moluwole committed Dec 30, 2019
2 parents eecf55a + b5cbe63 commit 231d3b5
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 157 deletions.
59 changes: 55 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
venv
# EDITOR Configs
# PyCharm
.idea
# VS Code
.vscode

# Python specific files
*.pyc
*.pyo

# Generic files to ignore
*~
._*
*.lock
*.DS_Store
*.swp
*.out
*.patch
*.diff
*.empty
*.orig
*.noseids
*.cprof
.venv
venv
bastenv
.idea
bask.egg-info
.coverage
.pip
coverage.xml
htmlcov
nosetests_*.xml
tags

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# Distribution / packaging
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
build
dist
bast.egg-info
bast.egg-info

*.egg-info

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
50 changes: 24 additions & 26 deletions bast/bast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
For full copyright and license information, view the LICENSE distributed with the Source Code
"""

import sys
import os
import sys

from colorama import init, Fore
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
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
from .session import MemorySession

__author__ = "Majiyagbe Oluwole"
__copyright__ = ""
Expand All @@ -24,17 +26,17 @@


class Bast(Application):
image_folder = ""
script_folder = ""
css_folder = ""
image_folder = ""
script_folder = ""
css_folder = ""
template_folder = ""

host = None
port = None
debug = None
host = None
port = None
debug = None

providers = {}
session = {}
providers = {}
session = {}

def __init__(self, route):
"""
Expand Down Expand Up @@ -64,7 +66,8 @@ def __init__(self, route):
self.handler.append((r'/images/(.*)', StaticFileHandler, {"path": self.image_folder}))

# append the URL for static files to exception
self.handler.append((r'/exp/(.*)', StaticFileHandler, {'path': os.path.join(os.path.dirname(os.path.realpath(__file__)), "exception")}))
self.handler.append((r'/exp/(.*)', StaticFileHandler,
{'path': os.path.join(os.path.dirname(os.path.realpath(__file__)), "exception")}))

def run(self):
"""
Expand All @@ -91,20 +94,15 @@ def config(self):
sys.path.extend([os.path.abspath('.')])
from config import config

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

# providers = provider.providers

# print(providers['session'])
static_files = config.STATIC_FILES
# Work in Progress
# if config.SESSION_DRIVER is 'memory':
# self.session.update({"session": MemorySession()})
# else:
# self.session.update({'session': FileSession()})

os.environ['TEMPLATE_FOLDER'] = os.path.join(os.path.abspath('.'), static_files['template'])

self.image_folder = os.path.join(os.path.abspath('.'), static_files['images'])
self.css_folder = os.path.join(os.path.abspath('.'), static_files['css'])
self.script_folder = os.path.join(os.path.abspath('.'), static_files['script'])
# self.providers.update(providers)

self.image_folder = os.path.join(os.path.abspath('.'), static_files['images'])
self.css_folder = os.path.join(os.path.abspath('.'), static_files['css'])
self.script_folder = os.path.join(os.path.abspath('.'), static_files['script'])
89 changes: 28 additions & 61 deletions bast/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@
"""

import os
from git import Repo
from .bast import __version__
from .migration import CreateMigration, Migration
import shutil
import click
import re
from subprocess import call
import shutil
from base64 import b64encode
from colorama import init, Fore, Back

""" Handles the CLI commands and their respective Arguments """

from subprocess import call

def check():
server = os.path.abspath('.') + "/server.py"
config = os.path.abspath('.') + "/config"
import click
from colorama import init, Fore, Back
from git import Repo

if os.path.exists(server) and os.path.exists(config):
return True
from .bast import __version__
from .environment import check_environment
from .migration import CreateMigration, Migration

return False
""" Handles the CLI commands and their respective Arguments """


@click.group()
Expand All @@ -38,11 +31,9 @@ def main():

@main.command('create:controller', short_help='Creates a Controller File')
@click.argument('filename', required=1)
@check_environment
def controller_creatr(filename):
"""Name of the controller file to be created"""
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the create:controller command')
return

path = os.path.abspath('.') + '/controller'
if not os.path.exists(path):
Expand All @@ -51,7 +42,7 @@ def controller_creatr(filename):
# if os.path.isfile(path + )

file_name = str(filename + '.py')
if os.path.isfile(path+"/" + file_name):
if os.path.isfile(path + "/" + file_name):
click.echo(Fore.WHITE + Back.RED + "ERROR: Controller file exists")
return
controller_file = open(os.path.abspath('.') + '/controller/' + file_name, 'w+')
Expand All @@ -63,11 +54,8 @@ def controller_creatr(filename):

@main.command('create:middleware', short_help="Creates a Middleware")
@click.argument('filename', required=1)
@check_environment
def middleware_creatr(filename):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the create:middleware command')
return

path = os.path.abspath('.') + '/middleware'
if not os.path.exists(path):
os.makedirs(path)
Expand All @@ -82,11 +70,9 @@ def middleware_creatr(filename):

@main.command('create:view', short_help="Create a View File")
@click.argument('filename', required=1)
@check_environment
def view_creatr(filename):
"""Name of the View File to be created"""
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the create:view command')
return

path = os.path.abspath('.') + '/public/templates'
if not os.path.exists(path):
Expand All @@ -101,11 +87,9 @@ def view_creatr(filename):

@main.command('generate:key', short_help="Generate the APP KEY")
@click.argument('path', required=1)
@check_environment
def make_key(path):
env_path = os.path.join(path, '.env')
if not os.path.isfile(env_path):
click.echo(Fore.RED + ".env file not found. Scaffold a project to generate a key")
return

key = b64encode(os.urandom(32)).decode('utf-8')
with open(env_path, 'r') as file:
Expand All @@ -123,21 +107,18 @@ def make_key(path):


@main.command('run', short_help="Run your Bast Server")
@click.option('--serverfile', help="Name of the file to run", default='server.py')
def run(serverfile):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to use the "run" command')
return

call(['python', serverfile])
@click.option('--server_file', help="Name of the file to run", default='server.py')
@check_environment
def run(server_file):
call(['python', server_file])


@main.command('new', short_help="Create a new Bast Project")
@click.argument('projectname', required=1)
def create_new(projectname):
@click.argument('project_name', required=1)
def create_new(project_name):
"""Name of the project"""
git_url = "https://github.com/moluwole/Bast_skeleton"
path = os.path.abspath('.') + "/" + projectname
path = os.path.abspath('.') + "/" + project_name
if not os.path.exists(path):
os.makedirs(path)

Expand Down Expand Up @@ -172,11 +153,9 @@ def create_new(projectname):
@click.argument('migration_file', required=1)
@click.option('--create', default=True, help="Create the table. OPTIONAL")
@click.option('--table', default=None, help="Name of the table to be created. OPTIONAL")
def migration_creatr(migration_file, create, table):
@check_environment
def create_migration(migration_file, create, table):
"""Name of the migration file"""
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the create:migration command')
return

migration = CreateMigration()
if table is None:
Expand All @@ -187,35 +166,26 @@ def migration_creatr(migration_file, create, table):

@main.command('migration:run', short_help="Run Migration")
@click.option('--pretend', default=False, help="Simulates the Migration")
@check_environment
def migration_run(pretend):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the migration:run command')
return

migration = Migration()
migration.run_(pretend)
click.echo(Fore.GREEN + 'Migration Run successful')


@main.command('migration:rollback', short_help="Roll Back last Migration")
@click.option('--pretend', default=False, help="Simulates the Migration")
@check_environment
def migration_rollback(pretend):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the migration:rollback command')
return

migration = Migration()
count = migration.rollback_(pretend)
click.echo(Fore.GREEN + 'Roll Back Executed. %s migrations rolled back' % count)


@main.command('migration:reset', short_help="Reset Migration")
@click.option('--pretend', default=False, help="Simulate the Migration")
@check_environment
def migration_reset(pretend):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the migration:rollback command')
return

migration = Migration()
count = migration.reset_(pretend)
click.echo(Fore.GREEN + 'Migration Reset successful. %s migrations has been reset' % count)
Expand All @@ -224,11 +194,8 @@ def migration_reset(pretend):
@main.command('create:model', short_help="Create Model File")
@click.argument('model_file', required=1)
@click.option('--migration', default=True, help="Generate Migration File Also")
def model_creatr(model_file, migration):
if not check():
click.echo(Fore.RED + 'ERROR: Ensure you are in a bast app to run the create:model command')
return

@check_environment
def create_model(model_file, migration):
filename = snake_case(model_file) + ".py"

directory_path = os.path.abspath('.') + '/models/'
Expand Down
23 changes: 11 additions & 12 deletions bast/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@
"""

import importlib
import logging
import os
import traceback

from tornado.web import RequestHandler
from tornado.gen import coroutine
from tornado.util import unicode_type
from tornado.web import RequestHandler

from bast import Bast
from .exception import BastException
from .json_ import Json as json_
from .jsonifier import Json as json_
from .view import TemplateRendering
import os
from tornado.gen import coroutine
from bast import Bast


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

def __init__(self, application, request, **kwargs):
super(Controller, self).__init__(application, request, **kwargs)
self.request = request
self.application = application
self.request = request
self.application = application
self.session_driver = os.getenv("SESSION")

self.session = Bast.session['session']
Expand Down

0 comments on commit 231d3b5

Please sign in to comment.