This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagora-cli
More file actions
executable file
·146 lines (109 loc) · 5.17 KB
/
agora-cli
File metadata and controls
executable file
·146 lines (109 loc) · 5.17 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
"""
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
This file is part of the Smart Developer Hub Project:
http://www.smartdeveloperhub.org
Center for Open Middleware
http://www.centeropenmiddleware.com/
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
Copyright (C) 2015 Center for Open Middleware.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
"""
import click
from time import time
from agora.client.wrapper import Agora
from blessings import Terminal
from rdflib import Graph
from requests import ConnectionError
__author__ = 'Fernando Serena'
total_derefs = 0
total_triples = 0
def stringify_uri_list(objs):
if not len(objs):
return '[]'
return ', '.join(map(lambda x: x.n3(graph.namespace_manager), objs))
def out_load(uri, triples):
global total_derefs
global total_triples
total_derefs += 1
total_triples += len(triples)
mto = ''
if len(triples) > 1:
mto = 's'
print term.cyan + 'Deref' + '(<{}>) => {} triple{}'.format(uri, len(triples), mto) + term.normal
def out_seeds(seeds):
pass
# print seeds
def out_plink(uri, objs, sp):
print term.green + '({}) Looking for triples with property {} at {}'. \
format(sp, uri.n3(graph.namespace_manager), stringify_uri_list(objs)) + term.normal
def out_link(uri, objs, sp):
print term.green + '({}) Following {} from {}'. \
format(sp, uri.n3(graph.namespace_manager), stringify_uri_list(objs)) + term.normal
def out_type(uri, objs, sp):
print term.green + "({}) Searching {}'s at {}". \
format(sp, uri.n3(graph.namespace_manager), stringify_uri_list(objs)) + term.normal
def out_type_validation((_, s, p, o)):
print term.green + 'Extracted {} as subject of type {}'.format(s.n3(graph.namespace_manager),
o.n3(graph.namespace_manager)) + term.white
def out_tree(node):
print term.bold_blue("Starting tree navigation {}".format(node.n3(graph.namespace_manager)))
term = Terminal()
graph = Graph()
print term.bold + term.underline + 'Agora Console Client v0.3' + term.normal
@click.command()
@click.option('--host', default='localhost', help='Hostname of the Agora service.')
@click.option('--port', default=9001, help='Port that the Agora is listening.')
@click.option('--gp', default='{}', help='The graph pattern of your required fragment.')
@click.option('--u', is_flag=True, help='Unnatended plan execution.')
def get_fragment(host, port, gp, u):
agora = Agora(host, port)
print 'Trying to retrieve a fragment for:',
print term.bold + gp + term.normal
raw_input(term.bold_blue('(send)'))
print 'Requesting a plan...',
start_time = time()
try:
fragment, namespaces, plan = agora.get_fragment_generator(gp, on_load=out_load, on_seeds=out_seeds,
on_plink=out_plink, on_link=out_link,
on_type=out_type,
on_type_validation=out_type_validation,
on_tree=out_tree)
except ConnectionError, e:
print term.red + "\nCouldn't connect to {} due to:\n{}".format(host, e.message) + term.normal
return
print 'Done! (in ~{}ms)'.format(int(1000 * (time() - start_time)))
raw_input(term.bold_blue('(take a look)'))
print term.bold_yellow('Search plan:')
print term.yellow + plan.serialize(format='turtle') + term.normal
for (prefix, uri) in namespaces:
graph.bind(prefix, uri)
raw_input(term.blink(term.bold_blue('(run)')))
final_triples = 0
for _, s, p, o in fragment:
graph.add((s, p, o))
final_triples += 1
print u'{} {} {} .'.format(s.n3(graph.namespace_manager),
p.n3(graph.namespace_manager),
o.n3(graph.namespace_manager))
if not u:
raw_input(term.bold_blue('(more)'))
print term.bold_yellow('\nFragment:')
fragment = unicode(graph.serialize(format='turtle'), errors='ignore')
print term.yellow(fragment)
print term.bold_white('Summary:')
print 'Resources dereferenced = {}'.format(term.bold(str(total_derefs)))
print 'Triples collected = {}'.format(term.bold(str(total_triples)))
print 'Fragment triples = {}'.format(term.bold(str(final_triples)))
if __name__ == '__main__':
get_fragment()