Skip to content

Commit 5d38ad0

Browse files
author
Tobias Laufkötter
committed
implement fetching of current biobox images. refs #1
1 parent 0b86862 commit 5d38ad0

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

app/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask import jsonify, abort, request, make_response
22

3-
from app import app
3+
from app import app, bioboxes
44

55
BAD_REQUEST = 400
66
NOT_IMPLEMENTED = 405
@@ -18,7 +18,7 @@ def not_implemented(error):
1818

1919
@app.route('/bioboxgui/api/bioboxes', methods=[GET_METHOD])
2020
def get_bioboxes():
21-
abort(NOT_IMPLEMENTED)
21+
return jsonify({'images': [box.get_dict() for box in bioboxes.get_current_bioboxes()]})
2222

2323

2424
@app.route('/bioboxgui/api/bioboxes', methods=[POST_METHOD])

app/bioboxes.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import requests
2+
import yaml
3+
4+
5+
class Yaml():
6+
"""
7+
is the parent of every class that can be generated from yaml files.
8+
"""
9+
MUST_HAVE = []
10+
11+
def __init__(self, yaml_dict):
12+
"""
13+
Forms an object from the given dictionary.
14+
15+
:param yaml_dict: a dictionary as it is parsed from yaml code.
16+
:return: Yaml base
17+
"""
18+
self.validate_yaml(yaml_dict)
19+
20+
def validate_yaml(self, yaml_dict):
21+
"""
22+
validates the given dictionary against the MUST_HAVE keys.
23+
24+
:param yaml_dict: a dictionary as it is parsed from yaml code.
25+
:return: None
26+
:raises: LookupError when key requirements aren't fulfilled.
27+
"""
28+
for key in self.MUST_HAVE:
29+
if yaml_dict.has_key(key) and yaml_dict.get(key):
30+
pass
31+
else:
32+
raise LookupError, 'error with the "%s" key' % key
33+
34+
35+
class Biobox(Yaml):
36+
"""
37+
represents a generic biobox container description.
38+
"""
39+
KEY_IMAGE = 'image'
40+
KEY_ID = 'pmid'
41+
KEY_HOME_PAGE = 'homepage'
42+
KEY_MAILING_LIST = 'mailing_list'
43+
KEY_DESCRIPTION = 'description'
44+
KEY_TASKS = 'tasks'
45+
KEY_TITLE = 'title'
46+
47+
MUST_HAVE = [
48+
KEY_IMAGE,
49+
KEY_ID,
50+
KEY_DESCRIPTION,
51+
KEY_TASKS,
52+
KEY_TITLE
53+
]
54+
55+
def __init__(self, yaml_dict):
56+
"""
57+
Forms an object from the given dictionary.
58+
59+
:param interface: the interface of the biobox.
60+
:param yaml_dict: a dictionary as it is parsed from yaml code.
61+
:return: Yaml base
62+
"""
63+
Yaml.__init__(self, yaml_dict)
64+
self.image = self.Image(yaml_dict.get(self.KEY_IMAGE))
65+
self.tasks = [self.Task(task) for task in yaml_dict.get(self.KEY_TASKS)]
66+
self.pmid = yaml_dict.get(self.KEY_ID)
67+
self.homepage = yaml_dict.get(self.KEY_HOME_PAGE)
68+
self.mailing_list = yaml_dict.get(self.KEY_MAILING_LIST)
69+
self.description = yaml_dict.get(self.KEY_DESCRIPTION)
70+
self.title = yaml_dict.get(self.KEY_TITLE)
71+
72+
def get_dict(self):
73+
dictionary = self.__dict__
74+
dictionary[self.KEY_IMAGE] = self.image.__dict__
75+
dictionary[self.KEY_TASKS] = [task.__dict__ for task in self.tasks]
76+
return dictionary
77+
78+
class Image(Yaml):
79+
"""
80+
is a helper class for :class:`Biobox`.
81+
"""
82+
KEY_CONTAINER_URI = 'dockerhub'
83+
KEY_REPO_URL = 'repo'
84+
KEY_SRC_URL = 'source'
85+
86+
MUST_HAVE = [
87+
KEY_CONTAINER_URI,
88+
KEY_REPO_URL
89+
]
90+
91+
def __init__(self, yaml_dict):
92+
"""
93+
Forms an object from the given dictionary.
94+
95+
:param yaml_dict: a dictionary as it is parsed from yaml code.
96+
:return: Yaml base
97+
"""
98+
Yaml.__init__(self, yaml_dict)
99+
self.dockerhub = yaml_dict.get(self.KEY_CONTAINER_URI)
100+
self.repo = yaml_dict.get(self.KEY_REPO_URL)
101+
self.source = yaml_dict.get(self.KEY_SRC_URL)
102+
103+
class Task(Yaml):
104+
KEY_NAME = 'name'
105+
KEY_INTERFACE = 'interface'
106+
107+
MUST_HAVE = [
108+
KEY_NAME,
109+
KEY_INTERFACE
110+
]
111+
112+
def __init__(self, yaml_dict):
113+
Yaml.__init__(self, yaml_dict)
114+
self.name = yaml_dict.get(self.KEY_NAME)
115+
self.interface = yaml_dict.get(self.KEY_INTERFACE)
116+
117+
118+
def get_current_bioboxes():
119+
response = requests.get('https://raw.githubusercontent.com/pbelmann/data/feature/new-image-list/images.yml')
120+
return from_list(yaml.load(response.text))
121+
122+
123+
def from_list(yaml_dict):
124+
"""
125+
creates bioboxes from an interface list.
126+
:param yaml_dict: the list with bioboxes as it is parsed from a yaml file.
127+
:return: a list with lists of :class:`Biobox`.
128+
"""
129+
interfaces = []
130+
for yaml_box in yaml_dict.get('images'):
131+
biobox = Biobox(yaml_box)
132+
interfaces.append(biobox)
133+
134+
return interfaces

requirements.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
flask
1+
Flask==0.10.1
2+
Flask-HTTPAuth==2.7.0
3+
Flask-SQLAlchemy==2.1
4+
google-common==0.0.1
5+
itsdangerous==0.24
6+
Jinja2==2.8
7+
MarkupSafe==0.23
8+
mesos==0.26.0
9+
mesos.interface==0.26.0
10+
protobuf==2.6.1
11+
PyYAML==3.11
12+
requests==2.9.1
13+
SQLAlchemy==1.0.11
14+
Werkzeug==0.11.3
15+
wheel==0.24.0

0 commit comments

Comments
 (0)