Skip to content

Commit 9fe9e7f

Browse files
committed
Ability to dump linkage sets.
1 parent bebc060 commit 9fe9e7f

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.vscode/
55
tsconfig.json
66
jsconfig.json
7+
.DS_Store
78

89
### Python ###
910
# Byte-compiled / optimized / DLL files

alephclient/api.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ def search(self, query, schema=None, schemata=None, filters=None):
108108
filters.append(('schemata', schemata))
109109
if schema is None and schemata is None:
110110
filters.append(('schemata', 'Thing'))
111-
url = self._make_url("entities", query=query, filters=filters)
111+
url = self._make_url('entities', query=query, filters=filters)
112112
return APIResultSet(self, url)
113113

114114
def get_collection(self, collection_id):
115115
"""Get a single collection by ID (not foreign ID!)."""
116-
url = self._make_url("collections/{0}".format(collection_id))
117-
return self._request("GET", url)
116+
url = self._make_url('collections/{0}'.format(collection_id))
117+
return self._request('GET', url)
118118

119119
def get_entity(self, entity_id):
120120
"""Get a single entity by ID."""
121-
url = self._make_url("entities/{0}".format(entity_id))
122-
return self._request("GET", url)
121+
url = self._make_url('entities/{0}'.format(entity_id))
122+
return self._request('GET', url)
123123

124124
def get_collection_by_foreign_id(self, foreign_id):
125125
"""Get a dict representing a collection based on its foreign ID."""
@@ -194,8 +194,7 @@ def map_collection(self, collection_id, mapping):
194194
url = self._make_url("collections/{0}/mapping".format(collection_id))
195195
return self._request("PUT", url, json=mapping)
196196

197-
def stream_entities(self, collection_id=None, include=None,
198-
schema=None, decode_json=True):
197+
def stream_entities(self, collection_id=None, include=None, schema=None):
199198
"""Iterate over all entities in the given collection.
200199
201200
params
@@ -275,6 +274,12 @@ def match(self, entity, collection_ids=None, url=None):
275274
except RequestException as exc:
276275
raise AlephException(exc)
277276

277+
def linkages(self, context_ids=None):
278+
"""Stream all linkages within the given role contexts."""
279+
filters = [('context_id', c) for c in ensure_list(context_ids)]
280+
url = self._make_url('linkages', filters=filters)
281+
return APIResultSet(self, url)
282+
278283
def ingest_upload(self, collection_id, file_path=None, metadata=None):
279284
"""
280285
Create an empty folder in a collection or upload a document to it

alephclient/cli.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,24 @@ def stream_entities(ctx, outfile, schema, foreign_id):
108108
if collection is None:
109109
raise click.BadParameter("Collection %r not found!" % foreign_id)
110110
for entity in api.stream_entities(collection_id=collection.get('id'),
111-
include=include, schema=schema,
112-
decode_json=False):
111+
include=include, schema=schema):
112+
outfile.write(json.dumps(entity))
113+
outfile.write('\n')
114+
except AlephException as exc:
115+
raise click.ClickException(exc.message)
116+
except BrokenPipeError:
117+
raise click.Abort()
118+
119+
120+
@cli.command('linkages')
121+
@click.option('-o', '--outfile', type=click.File('w'), default='-') # noqa
122+
@click.option('-c', '--context', multiple=True, default=[]) # noqa
123+
@click.pass_context
124+
def linkages(ctx, outfile, context):
125+
"""Stream all linkages within the given role contexts."""
126+
api = ctx.obj["api"]
127+
try:
128+
for entity in api.linkages(context_ids=context):
113129
outfile.write(json.dumps(entity))
114130
outfile.write('\n')
115131
except AlephException as exc:

0 commit comments

Comments
 (0)