Repository navigation
Expand file tree
/
Copy pathcommands.py
More file actions
269 lines (232 loc) · 10 KB
/
Copy pathcommands.py
File metadata and controls
269 lines (232 loc) · 10 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding:utf-8 -*-
from flask_script import Command, Option, prompt_bool
from flask_security.utils import encrypt_password, verify_password, get_hmac
import flask
import interpreter
from base64 import b64encode
import os
import datetime
import rdflib
from nanopub import Nanopublication
from cookiecutter.main import cookiecutter
import tempfile
np = rdflib.Namespace("http://www.nanopub.org/nschema#")
def rando():
return b64encode(os.urandom(24)).decode('utf-8')
class Configure(Command):
'''Create a Whyis configuration and customization directory.'''
def get_options(self):
return [
]
def run(self, extension_directory=None, extension_name=None):
# Create project from the cookiecutter-pypackage/ template
extra_context = { 'SECRET_KEY':rando(), 'SECURITY_PASSWORD_SALT': rando() }
cookiecutter('config-template/', extra_context=extra_context)
class LoadNanopub(Command):
'''Add a nanopublication to the knowledge graph.'''
def get_options(self):
return [
Option('--input', '-i', dest='input_file',
type=str),
Option('--format', '-f', dest='file_format',
type=str),
Option('--revises', '-r', dest='was_revision_of',
type=str),
]
def run(self, input_file, file_format="trig", was_revision_of=None):
if was_revision_of is not None:
wasRevisionOf = set(flask.current_app.db.objects(predicate=np.hasAssertion,
subject=rdflib.URIRef(was_revision_of)))
if len(wasRevisionOf) == 0:
print "Could not find active nanopublication to revise:", was_revision_of
return
was_revision_of = wasRevisionOf
g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize(), store="Sleepycat")
graph_tempdir = tempfile.mkdtemp()
g.store.open(graph_tempdir, True)
#g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize())
g1 = g.parse(location=input_file, format=file_format, publicID=flask.current_app.NS.local)
if len(list(g.subjects(rdflib.RDF.type, np.Nanopublication))) == 0:
print "Could not find existing nanopublications.", len(g1), len(g)
new_np = Nanopublication(store=g1.store)
new_np.add((new_np.identifier, rdflib.RDF.type, np.Nanopublication))
new_np.add((new_np.identifier, np.hasAssertion, g1.identifier))
new_np.add((g1.identifier, rdflib.RDF.type, np.Assertion))
nanopub_prepare_graph = rdflib.ConjunctiveGraph(store="Sleepycat")
nanopub_prepare_graph_tempdir = tempfile.mkdtemp()
nanopub_prepare_graph.store.open(nanopub_prepare_graph_tempdir, True)
nanopubs = []
for npub in flask.current_app.nanopub_manager.prepare(g, store=nanopub_prepare_graph.store):
if was_revision_of is not None:
for r in was_revision_of:
print "Marking as revision of", r
npub.pubinfo.add((npub.assertion.identifier, flask.current_app.NS.prov.wasRevisionOf, r))
print 'Prepared', npub.identifier
nanopubs.append(npub)
flask.current_app.nanopub_manager.publish(*nanopubs)
print "Published", npub.identifier
class RetireNanopub(Command):
'''Retire a nanopublication from the knowledge graph.'''
def get_options(self):
return [
Option('--nanopub_uri', '-n', dest='nanopub_uri',
type=str),
]
def run(self, nanopub_uri):
flask.current_app.nanopub_manager.retire(nanopub_uri)
class TestAgent(Command):
'''Add a nanopublication to the knowledge graph.'''
def get_options(self):
return [
Option('--agent', '-a', dest='agent_path',
type=str),
Option('--dry-run', '-d', action="store_true", dest='dry_run'),
]
def run(self, agent_path, dry_run=False):
app = flask.current_app
from pydoc import locate
agent_class = locate(agent_path)
agent = agent_class()
agent.dry_run = dry_run
if agent.dry_run:
print "Dry run, not storing agent output."
agent.app = app
print agent.get_query()
results = []
if agent.query_predicate == app.NS.whyis.globalChangeQuery:
results.extend(agent.process_graph(app.db))
else:
for resource in agent.getInstances(app.db):
for np_uri, in app.db.query('''select ?np where {
graph ?assertion { ?e ?p ?o.}
?np a np:Nanopublication;
np:hasAssertion ?assertion.
}''', initBindings={'e': resource.identifier}, initNs=app.NS.prefixes):
np = app.nanopub_manager.get(np_uri)
results.extend(agent.process_graph(np))
for np in results:
print np.serialize(format="trig")
class UpdateUser(Command):
"""Update a user in Whyis"""
def get_options(self):
return [
Option('--email', '-e', dest='email',
type=str),
Option('--password', '-p', dest='password',
type=str),
Option('--fn', '-f', dest='fn',
type=str),
Option('--ln', '-l', dest='ln',
type=str),
Option('--username', '-u', dest='identifier', type=str),
Option('--add-roles', dest="add_roles", type=str),
Option('--remove-roles', dest="remove_roles", type=str)
]
def run(self, identifier, email, password, fn, ln, add_roles, remove_roles):
user = flask.current_app.datastore.get_user(identifier)
print "Modifying user", user.resUri
if password is not None:
verified = verify_password(password,encrypt_password(password))
if verified:
user.password = encrypt_password(password)
else:
"User password not verified."
roles = set(user.roles)
if add_roles is not None:
for r in add_roles.split(','):
role = flask.current_app.datastore.find_or_create_role(name=r)
roles.add(role)
if remove_roles is not None:
for r in remove_roles.split(','):
role = flask.current_app.datastore.find_or_create_role(name=r)
roles.remove(role)
user.roles = list(roles)
if email is not None:
user.email = email
if fn is not None:
user.givenName = fn
if ln is not None:
user.familyName = ln
flask.current_app.datastore.commit()
print "Updated user: %s" % (user, )
class CreateUser(Command):
"""Add a user to Whyis"""
def get_options(self):
return [
Option('--email', '-e', dest='email',
type=str),
Option('--password', '-p', dest='password',
type=str),
Option('--fn', '-f', dest='fn',
type=str),
Option('--ln', '-l', dest='ln',
type=str),
Option('--username', '-u', dest='identifier', type=str),
Option('--roles', dest="roles", type=str)
]
def run(self, email, password, fn, ln, identifier, roles=[]):
print 'Password verified:', verify_password(password,encrypt_password(password))
role_objects = []
if roles is not None:
role_objects = [flask.current_app.datastore.find_or_create_role(name=r) for r in roles.split(',')]
user = dict(identifier=identifier, email=email,
password=encrypt_password(password),
givenName=fn, familyName=ln,
confirmed_at = datetime.datetime.utcnow(), roles = role_objects)
user_obj = flask.current_app.datastore.create_user(**user)
print "Created user: %s (%s)" % (user, ', '.join([r.resUri for r in role_objects]))
class Test(Command):
"""
Run tests
"""
verbosity = 2
failfast = False
def get_options(self):
return [
Option('--verbosity', '-v', dest='verbosity',
type=int, default=self.verbosity),
Option('--failfast', dest='failfast',
default=self.failfast, action='store_false')
]
def run(self, verbosity, failfast):
import sys
import glob
import unittest
exists = os.path.exists
isdir = os.path.isdir
join = os.path.join
project_path = os.path.abspath(os.path.dirname('.'))
sys.path.insert(0, project_path)
# our special folder for blueprints
if exists('apps'):
sys.path.insert(0, join('apps'))
loader = unittest.TestLoader()
all_tests = []
if exists('apps'):
for path in glob.glob('apps/*'):
if isdir(path):
tests_dir = join(path, 'tests')
if exists(join(path, 'tests.py')):
all_tests.append(loader.discover(path, 'tests.py'))
elif exists(tests_dir):
all_tests.append(loader.discover(tests_dir, pattern='test*.py'))
if exists('tests') and isdir('tests'):
all_tests.append(loader.discover('tests', pattern='test*.py'))
elif exists('tests.py'):
all_tests.append(loader.discover('.', pattern='tests.py'))
test_suite = unittest.TestSuite(all_tests)
unittest.TextTestRunner(
verbosity=verbosity, failfast=failfast).run(test_suite)
class RunInterpreter(Command):
'''Add a nanopublication to the knowledge graph.'''
def get_options(self):
return [
Option('--input', '-i', dest='config_file',
type=str),
]
def run(self, config_file=None):
app = flask.current_app
if config_file is not None :
agent = interpreter.Interpreter(config_fn=config_file)
agent.app = app
agent.process_graph(app.db)