Skip to content

Commit f1d56f6

Browse files
committed
c
1 parent 91fa289 commit f1d56f6

File tree

6 files changed

+119
-44
lines changed

6 files changed

+119
-44
lines changed

app.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ handlers:
1818
- url: /js
1919
static_dir: js
2020

21+
- url: /static
22+
static_dir: static
23+
24+
- url: /example
25+
static_dir: examples
26+
2127
- url: /less/css
2228
static_dir: less/css
2329

js/main.js

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $(document).ready(function(){
3535
});
3636

3737
$('#graph-render').on('click', function () {
38-
var graph = JSON.parse($('pre#log').text().trim());
38+
var graph = JSON.parse($('pre#graph-log').text().trim());
3939
$('#graph').html('');
4040
renderGraph(graph, '#graph');
4141
});
@@ -62,7 +62,8 @@ actions.processResponse = function(response) {
6262
$('#entities').html('');
6363
if (responseObj['entities'].length) {
6464
$('#raw-results').show();
65-
$('#log').text(JSON.stringify(responseObj.graph, undefined, 2));
65+
$('#entities-log').text(JSON.stringify(responseObj.entities, undefined, 2));
66+
$('#graph-log').text(JSON.stringify(responseObj.graph, undefined, 2));
6667
actions.displayEntities(responseObj['entities'])
6768
}
6869
};
@@ -104,8 +105,18 @@ actions.renderSnippet = function(el, entity) {
104105
//console.log(operation)
105106
var actionWidgetClass = actions.actionTypeToWidgetMap[operation['@type']];
106107
if (actionWidgetClass) {
107-
var widget = new actionWidgetClass(operation);
108-
widget.render($('.action-widget'), $('.action-log'));
108+
try {
109+
var widget = new actionWidgetClass(operation);
110+
widget.render($('.action-widget'), $('.action-log'));
111+
} catch (e) {
112+
113+
var msg = (e instanceof actions.ActionWidget.Exception) ?
114+
e.message : 'Application error';
115+
if ($('#validation-errors').text()) {
116+
$('#validation-errors').append($('<br>'));
117+
}
118+
$('#validation-errors').append(msg);
119+
}
109120
} else {
110121
if ($('#validation-errors').text()) {
111122
$('#validation-errors').append($('<br>'));
@@ -121,19 +132,24 @@ actions.renderSnippet = function(el, entity) {
121132
// Actions widget base class ***************************************************
122133

123134

124-
actions.ActionWidget = function(operation) {
125-
this.operation_ = operation;
135+
actions.ActionWidget = function(name, handler) {
126136
this.button_ = null;
127137
this.log_ = null;
138+
if (!name) {
139+
throw new actions.ActionWidget.Exception('Missing action name');
140+
}
141+
if (!handler) {
142+
throw new actions.ActionWidget.Exception('Missing action handler');
143+
}
144+
this.name_ = name;
145+
this.handler_ = handler;
146+
};
128147

129-
var handler = operation['http://schema.org/actionHandler'];
130-
this.method_ = handler['http://schema.org/httpMethod'];
131-
this.name_ = handler['http://schema.org/name'];
132-
this.url_ = handler['http://schema.org/url']['@id'];
133148

134-
var handlerClass = actions.actionHandlerTypes[handler['@type']];
135-
this.handler_ = new handlerClass(this.url_);
136-
};
149+
actions.ActionWidget.Exception = function(message) {
150+
this.message = message;
151+
this.name = 'ActionWidgetException';
152+
}
137153

138154

139155
actions.ActionWidget.prototype.render = function(widgetParent, logParent) {
@@ -212,6 +228,51 @@ actions.QuoteActionWidget.prototype.launch = function() {
212228
};
213229

214230

231+
// SearchAction widget **********************************************************
232+
233+
actions.SearchActionWidget = function(operation) {
234+
235+
//TODO(ewag): Factor out.
236+
237+
var handler = operation['http://schema.org/actionHandler'];
238+
if (!handler) {
239+
throw new actions.ActionWidget.Exception('Missing action handler');
240+
}
241+
var name = handler['http://schema.org/name'] || 'Search';
242+
if (!(handler['http://schema.org/url'] &&
243+
handler['http://schema.org/url']['@id'])) {
244+
throw new actions.ActionWidget.Exception('Missing handler url')
245+
}
246+
var url = handler['http://schema.org/url']['@id'];
247+
248+
var handlerClass = actions.actionHandlerTypes[handler['@type']];
249+
if (!handlerClass) {
250+
throw new actions.ActionWidget.Exception(
251+
'Unknown handler type: ' + handler['@type']);
252+
}
253+
var actionHandler = new handlerClass(url);
254+
255+
actions.ActionWidget.call(this, name, actionHandler);
256+
};
257+
actions.SearchActionWidget.prototype = Object.create(
258+
actions.ActionWidget.prototype);
259+
260+
261+
actions.SearchActionWidget.prototype.launch = function() {
262+
var el = $('<div class="result"></div>');
263+
this.log_.append(el);
264+
265+
console.log(this.handler_)
266+
267+
var callback = function(success, content) {
268+
this.updateWidgetState(success);
269+
console.log(JSON.parse(content));
270+
}
271+
272+
this.handler_.trigger({'test': 'someval'}, el, $.proxy(callback, this));
273+
};
274+
275+
215276
// Action Handlers *************************************************************
216277

217278
actions.ActionHandler = function(url) {
@@ -246,8 +307,8 @@ actions.HttpHandler.prototype.trigger = function(params, resultBox, callback) {
246307
}),
247308
contentType: 'application/json; charset=utf-8',
248309
complete: $.proxy(function(e) {
249-
var success = this.callback(resultBox, e);
250-
callback(success);
310+
var successAndResponse = this.callback(resultBox, e);
311+
callback(successAndResponse[0], successAndResponse[1]);
251312
}, this)
252313
}
253314
// What is the name of the param?
@@ -260,7 +321,7 @@ actions.HttpHandler.prototype.callback = function(el, e) {
260321
var success = false;
261322
if (e.status !== 200) {
262323
el.append($('<p><span class="text-danger">Server error</span></p> '));
263-
return success;
324+
return [success, null];
264325
}
265326
var response = JSON.parse(e.responseText)
266327
if (response.errors && response.errors.length) {
@@ -283,7 +344,8 @@ actions.HttpHandler.prototype.callback = function(el, e) {
283344
'</span></p>' +
284345
'<p class="debug">Debug: ' + response.result.debug+ '</p>'));
285346
}
286-
return success;
347+
var content = response.result ? response.result.content : null;
348+
return [success, content];
287349
}
288350

289351

@@ -310,7 +372,8 @@ actions.WebPageHandler.prototype.trigger = function(
310372

311373
actions.actionTypeToWidgetMap = {
312374
'http://schema.org/ReviewAction': actions.ReviewActionWidget,
313-
'http://schema.org/QuoteAction': actions.QuoteActionWidget
375+
'http://schema.org/QuoteAction': actions.QuoteActionWidget,
376+
'http://schema.org/SearchAction': actions.SearchActionWidget
314377
};
315378

316379

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def post(self):
116116

117117

118118
result = None
119+
119120
try:
120121
if method == 'GET':
121122
result = urllib2.urlopen(('%s?%s' % (data['url'], urllib.urlencode(data['params']))))
@@ -139,7 +140,8 @@ def post(self):
139140
'url': url_parts[0],
140141
'params': url_parts[1],
141142
'code': '%s %s' % (str(result.getcode()), result.msg),
142-
'debug': debug
143+
'debug': debug,
144+
'content': result.read(),
143145
}
144146

145147
print response

rdf_parser.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,25 @@
2424
import re
2525

2626

27+
context = {
28+
"@vocab": "http://schema.org/",
29+
"http://schema.org/url": {
30+
"@type": "@id"
31+
},
32+
"url": {
33+
"@type": "@id"
34+
}
35+
}
2736

2837
def parse_document(url):
2938
document = urlopen(url)
3039
logging.info(url)
3140

32-
print dir(document)
33-
34-
print dir(document.headers)
35-
print document.headers.getheader('Content-type')
36-
print document.headers.typeheader
37-
38-
39-
40-
print re.findall(r"(?P<name>.*?): (?P<value>.*?)\r\n", str(document.headers))
41-
42-
4341
with closing(document) as f:
4442
g = rdflib.Graph()
4543
# This is a hack to force rewriting relative url to full urls.
4644
# TODO(ewag): Rethink whole issue of custom context resolution.
47-
context = {
48-
"@vocab": "http://schema.org/",
49-
"http://schema.org/url": {
50-
"@type": "@id"
51-
},
52-
"url": {
53-
"@type": "@id"
54-
}
55-
}
45+
5646
if document.headers.typeheader.split(';')[0] == 'application/json':
5747
g, parse_errors = process_jsonld(g, f.read(), context, url)
5848
else:
@@ -65,11 +55,17 @@ def parse_document(url):
6555
return graph, entities, warnings, list(errors)
6656

6757
def parse_string(docString):
58+
g = rdflib.Graph()
6859
doc = html5lib.parse(docString, treebuilder="dom")
69-
g, parse_errors = process_dom(doc, None)
60+
g, parse_errors = process_dom(g, doc, context, None)
7061
graph, entities, warnings, errors = process_graph(g)
7162
errors.update(parse_errors)
7263

64+
print 'e'
65+
for s, p, o in g:
66+
print 'aaa'
67+
print s, p, o
68+
7369
return graph, entities, warnings, list(errors)
7470

7571

templates/index.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h1 class="map-appname">Test Actions</h1>
3232
<button id="parse" class="btn btn-primary">Parse</button>
3333
</div>
3434
<div class="tab-pane" id="url-input-tab">
35-
<textarea rows="4" id="provider-url" placeholder="Resource page url">http://googleknowledge.github.io/ActionsSamples/restaurant.html</textarea>
35+
<textarea rows="4" id="provider-url" placeholder="Resource page url">http://localhost:8080/example/places.html</textarea>
3636
<button id="fetch" class="btn btn-primary">Crawl</button>
3737
</div>
3838
<div class="tab-pane" id="feed-input-tab">
@@ -56,12 +56,17 @@ <h1 class="map-appname">Test Actions</h1>
5656
</div>
5757
<div id="raw-results">
5858
<a class="collapsed" data-toggle="collapse" data-target="#raw">
59-
Recognized entity graph
59+
Raw entities
6060
</a>
6161
<div id="raw" class="collapse">
62-
<pre id="log" class="prettyprint lang-html linenums"></pre>
62+
<pre id="entities-log" class="prettyprint lang-html linenums"></pre>
63+
</div><br>
64+
<a class="collapsed" data-toggle="collapse" data-target="#graph-raw">
65+
Recognized entity graph
66+
</a>
67+
<div id="graph-raw" class="collapse">
68+
<pre id="graph-log" class="prettyprint lang-html linenums"></pre>
6369
<button id="graph-render" class="btn btn-primary" data-toggle="modal" data-target="#graph-modal">Visualize</button>
64-
6570
</div>
6671
</div>
6772
</div>

validator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def prepare_query(validation_rule):
1515

1616

1717
def validate(g):
18+
19+
20+
1821
errors = set()
1922
for q in VALIDATION_QUERIES:
2023
if len(g.query(q[0])) < 1:

0 commit comments

Comments
 (0)