Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Inversion #3

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
@@ -1,6 +1,3 @@
*.py[co]

settings_local.py

node_modules/
docs/_build
docs/_build
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

65 changes: 65 additions & 0 deletions README.rst
Expand Up @@ -2,3 +2,68 @@ Büchner
=======

A boilerplate for larger, modular Flask applications.

:code: https://github.com/rehandalal/buchner/
:issues: https://github.com/rehandalal/buchner/issues
:license: BSD 3-clause
:documentation: http://buchner.rtfd.org/


Requirements
============

* Python 2.7


Install
=======

Install it with pip::

$ pip install buchner


Install it from source::

$ git clone git://github.com/rehandalal/buchner
$ cd buchner
$ python setup.py install


Install it for hacking on::

$ git clone git://github.com/rehandalal/buchner
$ cd buchner
$ python setup.py develop


Create a project
================

Once you have it installed, you can create a new project::

$ buchner-cmd createproject <PROJECTNAME>


It'll create the project skeleton in the current working directory. You
can run your project immediately::

$ cd <PROJECTMODULE>
$ mkvirtualenv
$ pip install -r requirements.txt
$ python manage.py runserver

Open browser and view http://127.0.0.1:5000/


Run tests
=========

You need to install the development requirements::

$ pip install -r requirements-dev.txt


After that, you can run tests with::

nosetests
10 changes: 10 additions & 0 deletions bin/buchner-cmd
@@ -0,0 +1,10 @@
#!/usr/bin/env python

import os
import sys

from buchner.cmdline import cmdline_handler


if __name__ == '__main__':
sys.exit(cmdline_handler("buchner-cmd", sys.argv[1:]))
2 changes: 2 additions & 0 deletions buchner/__init__.py
@@ -0,0 +1,2 @@
__version__ = '0.1.dev'
__releasedate__ = ''
9 changes: 0 additions & 9 deletions buchner/apps/sample/views.py

This file was deleted.

149 changes: 149 additions & 0 deletions buchner/cmdline.py
@@ -0,0 +1,149 @@
import os
import os.path
import string
from optparse import OptionParser


from buchner import __version__


USAGE = '%prog [options] [command] [command-options]'
VERSION = '%prog ' + __version__


def build_parser(usage):
parser = OptionParser(usage=usage, version=VERSION)

return parser


DIGIT_TO_WORD = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine'
}


def clean_project_module(s):
s = ''.join([char for char in s
if char in string.ascii_letters + string.digits])

if s[0] in string.digits:
s = DIGIT_TO_WORD[s[0]] + s[1:]

return s


def perror(s):
print s


def create_project(command, argv):
parser = build_parser('%prog createproject <PROJECTNAME>')
(options, args) = parser.parse_args()

if not argv:
perror('ERROR: You must provide a project name.')
return 1

project_name = args[1]
project_module = clean_project_module(project_name.lower())

# Ask them for project module name and then double-check it's
# valid.
new_project_module = raw_input(
'Python module name for your project: [{0}] '.format(project_module))
new_project_module = new_project_module.strip()
if not new_project_module:
new_project_module = project_module

if new_project_module != clean_project_module(new_project_module):
perror(
'ERROR: "{0}" is not a valid Python module name.'.format(
new_project_module))
return 1

project_module = new_project_module
project_dir = os.path.abspath(project_module)

if os.path.exists(project_dir):
perror(
'ERROR: Cannot create "{0}"--something is in the way.'.format(
project_dir))
return 1

# Walk the project-template and create all files and directories
# replacing:
#
# * PROJECTMODULE -> project_module

project_template_dir = os.path.join(os.path.dirname(__file__),
'project-template')

for root, dirs, files in os.walk(project_template_dir):
rel_root = root[len(project_template_dir)+1:]

for f in files:
source = os.path.join(root, f)
dest = os.path.join(project_dir, rel_root, f)
dest = dest.replace('PROJECTMODULE', project_module)

if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))

fp = open(source, 'rb')
data = fp.read()
fp.close()

data = data.replace('PROJECTMODULE', project_module)

fp = open(dest, 'wb')
fp.write(data)
fp.close()

print 'create file: {0}'.format(dest)

print 'Done!'
return 0


HANDLERS = (
('createproject', create_project, 'Creates a new buchner project.'),
)


def cmdline_handler(scriptname, argv):
print '%s version %s' % (scriptname, __version__)

# TODO: Rewrite using subparsers.
handlers = HANDLERS

if not argv or '-h' in argv or '--help' in argv:
parser = build_parser("%prog [command]")
parser.print_help()
print ''
print 'Commands:'
for command_str, _, command_help in handlers:
print ' %-14s %s' % (command_str, command_help)
return 0

if '--version' in argv:
# We've already printed the version, so we can just exit.
return 0

command = argv.pop(0)
for (cmd, fun, hlp) in handlers:
if cmd == command:
return fun(command, argv)

perror('Command "{0}" does not exist.'.format(command))
for cmd, fun, hlp in handlers:
perror(' %-14s %s' % (cmd, hlp))
return 1
5 changes: 0 additions & 5 deletions buchner/database/__init__.py

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions buchner/project-template/CONTRIBUTORS
@@ -0,0 +1,3 @@
Contributors
============

1 change: 1 addition & 0 deletions buchner/project-template/LICENSE
@@ -0,0 +1 @@
TODO: Pick a license and copy and paste text here.
@@ -1,6 +1,7 @@
from buchner.database.classes import Model
from sqlalchemy import Column, Integer

from PROJECTMODULE.database.classes import Model


class Sample(Model):
__tablename__ = 'sample'
Expand Down
@@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block title %}Sample Home{% endblock title %}

{% block content %}
<h1>Sample home</h1>
<p>
This is the index.html page of the sample app.
</p>
{% endblock content %}
10 changes: 10 additions & 0 deletions buchner/project-template/PROJECTMODULE/apps/sample/views.py
@@ -0,0 +1,10 @@
from flask import Blueprint, render_template


blueprint = Blueprint('sample', __name__,
template_folder='templates')


@blueprint.route('/')
def index():
return render_template('sample/index.html')
5 changes: 5 additions & 0 deletions buchner/project-template/PROJECTMODULE/database/__init__.py
@@ -0,0 +1,5 @@
from PROJECTMODULE.database import classes, helpers
from PROJECTMODULE.database.helpers import get_session


__all__ = ['classes', 'helpers', 'get_session']
25 changes: 5 additions & 20 deletions buchner/main.py → ...er/project-template/PROJECTMODULE/main.py
Expand Up @@ -4,21 +4,7 @@
from flask.ext.funnel import Funnel
from flask.ext.mobility import Mobility

from buchner.errors import register_error_handlers


def _get_apps_full_names(apps):
names = []
for app in apps:
parts = []
if not __name__ == '__main__':
parts = __name__.split('.')
parts.pop()
parts.append('apps')
parts.append(app)

names.append('.'.join(parts))
return names
from PROJECTMODULE.errors import register_error_handlers


def create_app(settings):
Expand All @@ -34,18 +20,17 @@ def create_app(settings):

# Bootstrapping
if 'INSTALLED_APPS' in app.config:
app.installed_apps = _get_apps_full_names(
app.config.get('INSTALLED_APPS'))
app.installed_apps = app.config.get('INSTALLED_APPS', [])

# Extensions
Funnel(app)
Mobility(app)

# Register blueprints
for a in app.installed_apps:
# Register blueprints
for app_path in app.installed_apps:
app.register_blueprint(
getattr(__import__('%s.views' % a, fromlist=['blueprint']),
getattr(__import__('{0}.views'.format(app_path),
fromlist=['blueprint']),
'blueprint'))

# Register error handlers
Expand Down
@@ -1,7 +1,7 @@
[db_settings]
# Used to identify which repository this database is versioned under.
# You can use the name of your project.
repository_id=buchner
repository_id=PROJECTMODULE

# The name of the database table used to track the schema version.
# This name shouldn't already be used by your project.
Expand Down
Expand Up @@ -9,9 +9,11 @@ def abspath(path):

DEBUG = truthiness(os.environ.get('DEBUG', False))

DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///buchner_app.db')
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///PROJECTMODULE_app.db')

INSTALLED_APPS = []
INSTALLED_APPS = [
'PROJECTMODULE.apps.sample'
]

# Flask-Funnel
JAVA_BIN = os.environ.get('JAVA_BIN', 'java')
Expand Down
@@ -1,4 +1,4 @@
from buchner.settings import *
from PROJECTMODULE.settings import *

TESTING = True

Expand Down
10 changes: 10 additions & 0 deletions buchner/project-template/PROJECTMODULE/templates/base.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock title %}</title>
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
@@ -0,0 +1,5 @@
{% extends "errors/base.html" %}
{% block title %}HTTP 404{% endblock title %}
{% block content %}
<h1>HTTP 404: Page does not exist</h1>
{% endblock content %}
@@ -0,0 +1,5 @@
{% extends "errors/500.html" %}
{% block title %}HTTP 500{% endblock title %}
{% block content %}
<h1>HTTP 500</h1>
{% endblock content %}