Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Initial commit of cleaned-up base site
Browse files Browse the repository at this point in the history
  • Loading branch information
bricef committed Apr 23, 2012
1 parent 0dfbe02 commit 45d876a
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 99 deletions.
110 changes: 59 additions & 51 deletions app.py
@@ -1,79 +1,87 @@

#!/usr/bin/env python

import sys
sys.path.append("./modules/")

import os
import ConfigParser
from functools import *
mypath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0,mypath)
sys.path.insert(0,os.path.join(mypath, "modules"))
os.chdir(mypath)import os

import pystache
# Standard lib modules
import argparse

# Third party modules
import bottle
from bottle import route, run, request, response, get, post, redirect, abort, static_file, error, mount

# My modules
from sitetools import *
from config import config

from bottle import route, run, request, response, get, post, redirect, abort, static_file, error


#########################
#
# Configuration
#
config = ConfigParser.SafeConfigParser()
config.read("config.ini")
#########################

bottle.debug(True)

def renderfile(name, data=None):
#This would be more readable in lisp...Coulda shoulda woulda
fnames = filter(
os.path.isfile,
map(
partial(os.path.join, config.get("app", "templatepath")),
["%s.mustache"%name, "%s.html"%name, name]))
for name in fnames:
return pystache.render(open(name, "r").read(), data, path=config.get("app", "templatepath"))




def show_flowDiagram():
return "<h3>flow diagram placeholder</h3>"



pages = {
"overview" : {
"name" : "Overview",
"href" : "/overview",
"template" : "overview",
"tabs" : [
{ "name" : "Flow Diagram",
"content" : show_flowDiagram }
]
},
}

def genpage(name):
if name in pages:
return renderfile(pages[name]["template"])
else:
abort(404, "This page does not exist")

###############################
#
# Route Handlers
#
###############################

@route("/")
@route("/home")
def home():
return renderfile("home")
return renderfile("root", {})

@route("/<name>")
def home(name):
return genpage(name)

@route("/fail/<report>/<graph>")
def show_fragment(report, graph):
abort(404, "This page does not exist")

@route("/static/<filepath:path>")
def static(filepath):
return static_file(filepath, root="./static/")


###############################
#
# Mount API on /api/
#
###############################
from jsonapi import app as api_app
mount("/api/", api_app)


###############################
#
# Handle Errors
#
###############################
@error(404)
def error404(error):
return renderfile("404")

run(host="0.0.0.0", port="8009", debug=True, reloader=True)

#
# Main. To run in developer mode simply pass --run
#
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run appliation')
parser.add_argument(
"--run",
action='store_true',
help='Run the webapp in developer mode'
)
args = parser.parse_args()

if args.run:
run(host="0.0.0.0", port="8009", debug=True, reloader=True)

else:
application = bottle.default_app()

7 changes: 6 additions & 1 deletion config.ini
@@ -1,4 +1,9 @@

[app]
templatepath = ./templates/
debug = True
cache = False
secret = bbda8uasi7782337bbdbo63o62b2b82d86d63d225nd926db02d6

[db]
host = localhost
port = 27017
16 changes: 16 additions & 0 deletions config.py
@@ -0,0 +1,16 @@


import ConfigParser

config = ConfigParser.SafeConfigParser({
"debug":"False",
"production":"True",
"templatepath":"./templates/",
"cache":"False"})

config.read("config.ini")

DEBUG = config.getboolean("app", "debug")
PRODUCTION = config.getboolean("app", "production")
TEMPLATEPATH = config.get("app", "templatepath")

14 changes: 14 additions & 0 deletions database.py
@@ -0,0 +1,14 @@
#!/usr/bin/env python
from functools import *

import pymongo

from sitetools import *
from config import config

conn = pymongo.Connection("localhost", 27017)
db = conn.bookdb
db.users.ensure_index("name")
users = db.users
books = db.books

54 changes: 54 additions & 0 deletions jsonapi.py
@@ -0,0 +1,54 @@
import os
import sys
mypath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0,mypath)
sys.path.insert(0,os.path.join(mypath, "modules"))
os.chdir(mypath)

#stdlib modules
import json
import time

# third party modules
from bottle import *

# my modules
from config import config
from database import books
from sitetools import *
import users

app = Bottle()

def document(func):
func.document = True
return func

#
# API Functions
#
@app.route("/books")
@users.CheckPermission("READ")
def json_books():
response.set_header('Content-type', 'application/json')
return json.dumps(books.find())


#
# Automagic utilities
#
@document
@app.route('/status')
def api_status():
"""returns the server status and server time."""
return {'status':'online', 'servertime':time.time()}

@app.route('/docs')
def autodoc():
#the idea is to extract the route information out of the functions
return {"functions": [ {"route": None, "doc":v.__doc__} for k,v in globals().items() if hasattr(v, "document") and v.document == True]}

@app.error(500)
def error(error):
response.set_header("Content-Type", "application/json")
return json.dumps({"error": str(error.output)})

0 comments on commit 45d876a

Please sign in to comment.