dump-things-pyclient 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/PKG-INFO +3 -1
  2. dump_things_pyclient-0.1.1/dump_things_pyclient/commands/json2ttl.py +64 -0
  3. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/communicate.py +37 -7
  4. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient.egg-info/PKG-INFO +3 -1
  5. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient.egg-info/SOURCES.txt +1 -0
  6. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient.egg-info/entry_points.txt +1 -0
  7. dump_things_pyclient-0.1.1/dump_things_pyclient.egg-info/requires.txt +2 -0
  8. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/pyproject.toml +6 -1
  9. dump_things_pyclient-0.1.0/dump_things_pyclient.egg-info/requires.txt +0 -1
  10. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/README.md +0 -0
  11. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/__init__.py +0 -0
  12. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/commands/__init__.py +0 -0
  13. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/commands/auto_curate.py +0 -0
  14. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/commands/get_records.py +0 -0
  15. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient/commands/read_pages.py +0 -0
  16. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient.egg-info/dependency_links.txt +0 -0
  17. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/dump_things_pyclient.egg-info/top_level.txt +0 -0
  18. {dump_things_pyclient-0.1.0 → dump_things_pyclient-0.1.1}/setup.cfg +0 -0
@@ -1,9 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dump-things-pyclient
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A client library and some CLI command for dump-things-services
5
+ Author-email: Christian Mönch <christian.moench@web.de>
5
6
  Requires-Python: >=3.11
6
7
  Description-Content-Type: text/markdown
8
+ Requires-Dist: dump-things-service>=5.3.0
7
9
  Requires-Dist: requests>=2.32.5
8
10
 
9
11
  # Dump Things Python Client
@@ -0,0 +1,64 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import re
6
+ import sys
7
+
8
+ from dump_things_service.converter import (
9
+ Format,
10
+ FormatConverter,
11
+ )
12
+
13
+
14
+ description = f"""Read JSON records from stdin and convert them to TTL
15
+
16
+ This command reads one record per line, either JSON format or a JSON-string
17
+ with a TTL-document from stdin, converts them to TTL or JSON and prints them
18
+ to stdout.
19
+
20
+ """
21
+
22
+
23
+ def main():
24
+ argument_parser = argparse.ArgumentParser(
25
+ description=description,
26
+ formatter_class=argparse.RawDescriptionHelpFormatter,
27
+ )
28
+ argument_parser.add_argument('schema', help='URL of the schema that should be used')
29
+
30
+ arguments = argument_parser.parse_args()
31
+
32
+ print(f'Creating converter for schema {arguments.schema} ...', file=sys.stderr, end='', flush=True)
33
+ converter = FormatConverter(
34
+ arguments.schema,
35
+ input_format=Format.json,
36
+ output_format=Format.ttl,
37
+ )
38
+ print(' done', file=sys.stderr, flush=True)
39
+
40
+ error = False
41
+ for line in sys.stdin:
42
+ json_object = json.loads(line)
43
+
44
+ object_class = json_object.get('schema_type')
45
+ if object_class is None:
46
+ error = True
47
+ print(f'ERROR: No schema_type in {json_object}', file=sys.stderr, flush=True)
48
+ continue
49
+
50
+ class_name = re.search('([_A-Za-z0-9]*$)', object_class).group(0)
51
+
52
+ try:
53
+ ttl = converter.convert(json_object, class_name)
54
+ except ValueError as ve:
55
+ print(f'ERROR: conversion failed for {json_object}: {ve}', file=sys.stderr, flush=True)
56
+ continue
57
+
58
+ print(json.dumps(ttl))
59
+
60
+ return 1 if error else 0
61
+
62
+
63
+ if __name__ == '__main__':
64
+ sys.exit(main())
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import logging
4
+ import re
4
5
  from itertools import count
5
6
  from typing import (
6
7
  Callable,
@@ -99,6 +100,29 @@ def get(url: str,
99
100
  return _get_from_url(url, token, parameters)
100
101
 
101
102
 
103
+ def collection_get_classes(service_url: str,
104
+ collection: str,
105
+ ) -> Generator[str, None, None]:
106
+ """Read classes that are supported by the collection
107
+
108
+ Get the name of the classes that are known in the collection. If the
109
+ collection does not exist on the server, no class names are returned.
110
+
111
+ :param service_url: the base URL of the service, i.e., the URL up to
112
+ `/<collection>/...` or `/server`
113
+ :param collection: the name of the collection
114
+
115
+ :return: a generator yielding names of the supported classes
116
+ """
117
+ service_url = f'{service_url[:-1]}' if service_url.endswith('/') else service_url
118
+ matcher = re.compile(f'/{collection}/record/([A-Z][_a-zA-Z0-9]*)$')
119
+ open_api_spec = _get_from_url(service_url + '/openapi.json', None)
120
+ for path in open_api_spec['paths']:
121
+ match = matcher.match(path)
122
+ if match:
123
+ yield match.group(1)
124
+
125
+
102
126
  def collection_read_record_with_pid(service_url: str,
103
127
  collection: str,
104
128
  pid: str,
@@ -236,11 +260,12 @@ def collection_write_record(
236
260
  than one record due to inlined-relations extraction. The individual
237
261
  records might have annotations added
238
262
  """
263
+ _check_format_value(format)
239
264
  return _post_to_url(
240
265
  url=_build_url(service_url, collection, f'record/{class_name}'),
241
266
  token=token,
242
- json=record,
243
- params={'format': format})
267
+ params={'format': format},
268
+ **(dict(json=record) if format == 'json' else dict(data=record)))
244
269
 
245
270
 
246
271
  def collection_validate_record(
@@ -269,12 +294,12 @@ def collection_validate_record(
269
294
 
270
295
  :return: True
271
296
  """
272
- service_url = f'{service_url[:-1]}' if service_url.endswith('/') else service_url
297
+ _check_format_value(format)
273
298
  return _post_to_url(
274
299
  url=_build_url(service_url, collection, f'validate/{class_name}'),
275
300
  token=token,
276
- json=record,
277
- params={'format': format})
301
+ params={'format': format},
302
+ **(dict(json=record) if format == 'json' else dict(data=record)))
278
303
 
279
304
 
280
305
  def collection_delete_record(
@@ -660,10 +685,10 @@ def _get_from_url(url: str,
660
685
 
661
686
  def _post_to_url(url: str,
662
687
  token: str | None,
663
- json: JSON,
664
688
  params: dict[str, str] | None = None,
689
+ **kwargs,
665
690
  ) -> JSON:
666
- return _do_request(requests.post, url, token, params, json=json)
691
+ return _do_request(requests.post, url, token, params, **kwargs)
667
692
 
668
693
 
669
694
  def _delete_url(url: str,
@@ -717,3 +742,8 @@ def _get_page(url_base: str,
717
742
  parameters['page'] = first_page
718
743
  parameters['size'] = page_size
719
744
  return _get_from_url(url_base, token, parameters)
745
+
746
+
747
+ def _check_format_value(format: str) -> None:
748
+ if format not in ('json', 'ttl'):
749
+ raise ValueError('Format must be either "json" or "ttl"')
@@ -1,9 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dump-things-pyclient
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A client library and some CLI command for dump-things-services
5
+ Author-email: Christian Mönch <christian.moench@web.de>
5
6
  Requires-Python: >=3.11
6
7
  Description-Content-Type: text/markdown
8
+ Requires-Dist: dump-things-service>=5.3.0
7
9
  Requires-Dist: requests>=2.32.5
8
10
 
9
11
  # Dump Things Python Client
@@ -11,4 +11,5 @@ dump_things_pyclient.egg-info/top_level.txt
11
11
  dump_things_pyclient/commands/__init__.py
12
12
  dump_things_pyclient/commands/auto_curate.py
13
13
  dump_things_pyclient/commands/get_records.py
14
+ dump_things_pyclient/commands/json2ttl.py
14
15
  dump_things_pyclient/commands/read_pages.py
@@ -1,4 +1,5 @@
1
1
  [console_scripts]
2
2
  auto-curate = dump_things_pyclient.commands.auto_curate:main
3
3
  get-records = dump_things_pyclient.commands.get_records:main
4
+ json2ttl = dump_things_pyclient.commands.json2ttl:main
4
5
  read-pages = dump_things_pyclient.commands.read_pages:main
@@ -0,0 +1,2 @@
1
+ dump-things-service>=5.3.0
2
+ requests>=2.32.5
@@ -1,10 +1,14 @@
1
1
  [project]
2
2
  name = "dump-things-pyclient"
3
- version = "0.1.0"
3
+ version = "0.1.1"
4
4
  description = "A client library and some CLI command for dump-things-services"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
7
+ authors = [
8
+ {name="Christian Mönch", email="christian.moench@web.de"},
9
+ ]
7
10
  dependencies = [
11
+ "dump-things-service>=5.3.0",
8
12
  "requests>=2.32.5",
9
13
  ]
10
14
 
@@ -17,3 +21,4 @@ tests = [
17
21
  auto-curate = "dump_things_pyclient.commands.auto_curate:main"
18
22
  read-pages = "dump_things_pyclient.commands.read_pages:main"
19
23
  get-records = "dump_things_pyclient.commands.get_records:main"
24
+ json2ttl = "dump_things_pyclient.commands.json2ttl:main"
@@ -1 +0,0 @@
1
- requests>=2.32.5