Skip to content

Commit 0820eda

Browse files
committed
Migrating to using factories and blueprints
1 parent 04c1cd7 commit 0820eda

File tree

5 files changed

+72
-44
lines changed

5 files changed

+72
-44
lines changed

tests/unuo_tests.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import unittest
22
import json
3+
import logging
34

4-
from unuo.app import app
5+
from unuo.factories import default_factory
6+
7+
8+
logger = logging.getLogger('test')
9+
logging.basicConfig(level=logging.INFO)
510

611

712
class UnuoTests(unittest.TestCase):
813

914
def setUp(self):
15+
app = default_factory()
1016
self.app = app.test_client()
1117

1218
def test_profiles(self):
1319
rv = self.app.get('/profile')
1420
j = json.loads(rv.data)
21+
self.assertEquals({u'builds': [u'mynewbuild']}, j)

unuo/app.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
#!/usr/bin/env python
22

3-
from flask import Flask, jsonify, request
3+
from unuo.factories import default_factory
44

5-
from unuo.filebackend import post_build, run_build, get_build_profile, \
6-
get_all_profiles
7-
from unuo.errors import ApiError
8-
9-
10-
app = Flask(__name__)
11-
12-
13-
@app.errorhandler(500)
14-
def error_ise(error):
15-
return '{"status_code":500,"description":"oh noes!"}', 500
16-
17-
18-
@app.errorhandler(404)
19-
@app.errorhandler(400)
20-
@app.errorhandler(405)
21-
@app.errorhandler(ApiError)
22-
def error_json(error):
23-
return '{"status_code":"%s","description":"%s"}' % (
24-
error.code, error.description), error.code
25-
26-
27-
@app.route('/build/<name>', methods=['POST'])
28-
def build(name):
29-
"""Run given build."""
30-
return run_build(name)
31-
32-
33-
@app.route('/profile', methods=['GET'])
34-
def list_build_profiles():
35-
"""Responsible for listing known builds."""
36-
builds = get_all_profiles()
37-
return jsonify({"builds": builds})
38-
39-
40-
@app.route('/profile/<name>', methods=['GET', 'POST'])
41-
def build_container(name):
42-
"""Responsible for creating/updating builds and launching them."""
43-
if request.method == 'POST':
44-
return post_build(name, request.json)
45-
return jsonify(**get_build_profile(name))
5+
app = None
466

477

488
if __name__ == '__main__':
49-
app.config.from_object('unuo.config.DefaultConfig')
9+
app = default_factory()
5010
app.run()

unuo/blueprints/__init__.py

Whitespace-only changes.

unuo/blueprints/docker.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Blueprint for Docker related functions.
2+
3+
Contains API methods for kicking off a build profile, adding a build profile,
4+
getting a listing of build profiles.
5+
"""
6+
from flask import Blueprint
7+
from flask import jsonify, request
8+
from unuo.errors import ApiError
9+
10+
from unuo.filebackend import post_build, run_build, get_build_profile, \
11+
get_all_profiles
12+
13+
docker_bp = Blueprint('docker', __name__)
14+
15+
16+
#@docker_bp.errorhandler(500)
17+
#def error_ise(error):
18+
# return '{"status_code":500,"description":"oh noes!"}', 500
19+
#
20+
21+
@docker_bp.errorhandler(404)
22+
@docker_bp.errorhandler(400)
23+
@docker_bp.errorhandler(405)
24+
@docker_bp.errorhandler(ApiError)
25+
def error_json(error):
26+
return '{"status_code":"%s","description":"%s"}' % (
27+
error.code, error.description), error.code
28+
29+
30+
@docker_bp.route('/build/<name>', methods=['POST'])
31+
def build(name):
32+
"""Run given build."""
33+
return run_build(name)
34+
35+
36+
@docker_bp.route('/profile', methods=['GET'])
37+
def list_build_profiles():
38+
"""Responsible for listing known builds."""
39+
builds = get_all_profiles()
40+
return jsonify({"builds": builds})
41+
42+
43+
@docker_bp.route('/profile/<name>', methods=['GET', 'POST'])
44+
def build_container(name):
45+
"""Responsible for creating/updating builds and launching them."""
46+
if request.method == 'POST':
47+
return post_build(name, request.json)
48+
return jsonify(**get_build_profile(name))

unuo/factories.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Flask factories for running the app and testing it.
2+
"""
3+
4+
from flask import Flask
5+
6+
from unuo.blueprints.docker import docker_bp
7+
8+
9+
def default_factory(config=None):
10+
app = Flask(__name__)
11+
app.config.from_object('unuo.config.DefaultConfig')
12+
app.register_blueprint(docker_bp)
13+
return app

0 commit comments

Comments
 (0)