/
__init__.py
126 lines (101 loc) · 3.99 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import json
import time
from flask import Flask, render_template, request
from groundstation import logger
log = logger.getLogger(__name__)
# XXX We won't always be using the github adaptor!!
from groundstation.protocols import github as github_protocol
from groundstation.gref import Gref
import pygit2
from groundstation.utils import oid2hex
from groundstation.objects.root_object import RootObject
from groundstation.objects.update_object import UpdateObject
def jsonate(obj, escaped):
jsonbody = json.dumps(obj)
if escaped:
jsonbody = jsonbody.replace("</", "<\\/")
return jsonbody
def channels_json(station, escaped=False):
channels = [{"name": channel} for channel in station.channels()]
return jsonate(channels, escaped)
def grefs_json(station, channel, escaped=False):
grefs = [gref.as_dict() for gref in station.grefs(channel)]
return jsonate(grefs, escaped)
def make_airship(station):
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html",
channels_json=channels_json(station, True),
current_time=time.time())
@app.route("/channels")
def list_channels():
return channels_json(station)
@app.route("/grefs/<channel>")
def list_grefs(channel):
return grefs_json(station, channel)
@app.route("/gref/<channel>/<path:identifier>")
def fetch_gref(channel, identifier):
adaptor = github_protocol.GithubReadAdaptor(station, channel)
gref = Gref(station.store, channel, identifier)
log.info("Trying to fetch channel: %s identifier: %s" %
(channel, identifier))
marshalled_thread = adaptor.get_issue(gref)
root_obj = marshalled_thread["roots"].pop()
root = root_obj.as_json()
root["hash"] = oid2hex(pygit2.hash(root_obj.as_object()))
response = []
while marshalled_thread["thread"]:
node = marshalled_thread["thread"].pop()
data = json.loads(node.data)
data["parents"] = list(node.parents)
data["hash"] = oid2hex(pygit2.hash(node.as_object()))
response.append(data)
return jsonate({"content": response, "root": root, "tips": marshalled_thread["tips"]}, False)
@app.route("/gref/<channel>/<path:identifier>", methods=['POST'])
def update_gref(channel, identifier):
# adaptor = github_protocol.GithubWriteAdaptor(station, channel)
gref = Gref(station.store, channel, identifier)
# Ugly type coercion
user = request.form["user"]
body = request.form["body"]
parents = map(str, json.loads(request.form["parents"]))
payload = {
"type": "comment",
"id": None,
"body": body,
"user": user
}
update_object = UpdateObject(parents, json.dumps(payload))
oid = station.write(update_object.as_object())
station.update_gref(gref, [oid], parents)
return jsonate({"response": "ok"}, False)
@app.route("/grefs/<channel>", methods=['PUT'])
def create_gref(channel):
def _write_object(obj):
return station.write(obj.as_object())
name = request.form["name"]
protocol = request.form["protocol"]
user = request.form["user"]
body = request.form["body"]
title = request.form["title"]
gref = Gref(station.store, channel, name)
root = RootObject(name, channel, protocol)
root_oid = _write_object(root)
_title = UpdateObject([root_oid], json.dumps({
"type": "title",
"id": None,
"body": title,
"user": user
}))
title_oid = _write_object(_title)
_body = UpdateObject([title_oid], json.dumps({
"type": "body",
"id": None,
"body": body
}))
body_oid = _write_object(_body)
station.update_gref(gref, [body_oid], [])
return ""
return app