ogc-na 0.2.25__py3-none-any.whl → 0.2.27__py3-none-any.whl

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.

Potentially problematic release.


This version of ogc-na might be problematic. Click here for more details.

ogc/na/ingest_json.py CHANGED
@@ -42,6 +42,7 @@ from pathlib import Path
42
42
  from typing import Union, Optional, Sequence, cast, Iterable, Any
43
43
 
44
44
  import jq
45
+ import pyld.jsonld
45
46
  from jsonpath_ng.ext import parse as json_path_parse
46
47
  from jsonschema import validate as json_validate
47
48
  from pyld import jsonld
@@ -159,7 +160,9 @@ def _document_loader(secure: bool = False,
159
160
  return loader
160
161
 
161
162
 
162
- def validate_context(context: Union[dict, str] = None, filename: Union[str, Path] = None) -> dict:
163
+ def validate_context(context: Union[dict, str] = None,
164
+ filename: Union[str, Path] = None,
165
+ transform_args: dict | None = None) -> dict:
163
166
  if not context and not filename:
164
167
  return {}
165
168
  if bool(context) == bool(filename):
@@ -178,7 +181,7 @@ def validate_context(context: Union[dict, str] = None, filename: Union[str, Path
178
181
  transform = [transform]
179
182
  for i, t in enumerate(transform):
180
183
  try:
181
- jq.compile(t)
184
+ jq.compile(t, args=transform_args)
182
185
  except Exception as e:
183
186
  raise ValidationError(cause=e,
184
187
  msg=f"Error compiling jq expression for transform at index {i}",
@@ -213,7 +216,8 @@ def add_jsonld_provenance(json_doc: Union[dict, list], metadata: ProvenanceMetad
213
216
 
214
217
  def uplift_json(data: dict | list, context: dict,
215
218
  fetch_timeout: int = 5,
216
- fetch_url_whitelist: Optional[Union[Sequence, bool]] = None) -> dict:
219
+ fetch_url_whitelist: Optional[Union[Sequence, bool]] = None,
220
+ transform_args: dict | None = None) -> dict:
217
221
  """
218
222
  Transform a JSON document loaded in a dict, and embed JSON-LD context into it.
219
223
 
@@ -226,6 +230,7 @@ def uplift_json(data: dict | list, context: dict,
226
230
  :param fetch_url_whitelist: list of regular expressions to filter referenced JSON-LD context URLs before
227
231
  retrieving them. If None, it will not be used; if empty sequence or False, remote fetching operations will
228
232
  throw an exception.
233
+ :param transform_args: Additional arguments to pass as variables to the jq transform
229
234
  :return: the transformed and JSON-LD-enriched data
230
235
  """
231
236
 
@@ -233,7 +238,7 @@ def uplift_json(data: dict | list, context: dict,
233
238
 
234
239
  jsonld.set_document_loader(_document_loader(timeout=fetch_timeout, url_whitelist=fetch_url_whitelist))
235
240
 
236
- validate_context(context)
241
+ validate_context(context, transform_args=transform_args)
237
242
 
238
243
  # Check whether @graph scoping is necessary for transformations and paths
239
244
  scoped_graph = context.get('scope', 'graph') == 'graph' and '@graph' in data
@@ -246,7 +251,7 @@ def uplift_json(data: dict | list, context: dict,
246
251
  if isinstance(transform, str):
247
252
  transform = (transform,)
248
253
  for i, t in enumerate(transform):
249
- tranformed_txt = jq.compile(t).input(data_graph).text()
254
+ tranformed_txt = jq.compile(t, args=transform_args).input(data_graph).text()
250
255
  if logger.isEnabledFor(logging.DEBUG):
251
256
  logger.debug('After transform %d:\n%s', i + 1, tranformed_txt)
252
257
  data_graph = json.loads(tranformed_txt)
@@ -329,7 +334,8 @@ def generate_graph(input_data: dict | list,
329
334
  context: dict[str, Any] | Sequence[dict] = None,
330
335
  base: str | None = None,
331
336
  fetch_timeout: int = 5,
332
- fetch_url_whitelist: Sequence[str] | bool | None = None) -> UpliftResult:
337
+ fetch_url_whitelist: Sequence[str] | bool | None = None,
338
+ transform_args: dict | None = None) -> UpliftResult:
333
339
  """
334
340
  Create a graph from an input JSON document and a YAML context definition file.
335
341
 
@@ -340,6 +346,7 @@ def generate_graph(input_data: dict | list,
340
346
  :param fetch_url_whitelist: list of regular expressions to filter referenced JSON-LD context URLs before
341
347
  retrieving them. If None, it will not be used; if empty sequence or False, remote fetching operations will
342
348
  throw an exception.
349
+ :param transform_args: Additional arguments to pass as variables to the jq transform
343
350
  :return: a tuple with the resulting RDFLib Graph and the JSON-LD enriched file name
344
351
  """
345
352
 
@@ -358,7 +365,8 @@ def generate_graph(input_data: dict | list,
358
365
  base_uri = context_entry.get('base-uri', base_uri)
359
366
  jdoc_ld = uplift_json(input_data, context_entry,
360
367
  fetch_timeout=fetch_timeout,
361
- fetch_url_whitelist=fetch_url_whitelist)
368
+ fetch_url_whitelist=fetch_url_whitelist,
369
+ transform_args=transform_args)
362
370
 
363
371
  options = {}
364
372
  if not base:
@@ -383,7 +391,7 @@ def generate_graph(input_data: dict | list,
383
391
  if logger.isEnabledFor(logging.DEBUG):
384
392
  logger.debug('Uplifted JSON:\n%s', json.dumps(jdoc_ld, indent=2))
385
393
 
386
- g.parse(data=json.dumps(jdoc_ld), format='json-ld')
394
+ g.parse(data=json.dumps(pyld.jsonld.expand(jdoc_ld)), format='json-ld')
387
395
 
388
396
  return UpliftResult(graph=g, uplifted_json=jdoc_ld)
389
397
 
@@ -397,7 +405,8 @@ def process_file(input_fn: str | Path,
397
405
  provenance_base_uri: str | bool | None = None,
398
406
  provenance_process_id: str | None = None,
399
407
  fetch_timeout: int = 5,
400
- fetch_url_whitelist: bool | Sequence[str] | None = None) -> UpliftResult | None:
408
+ fetch_url_whitelist: bool | Sequence[str] | None = None,
409
+ transform_args: dict | None = None) -> UpliftResult | None:
401
410
  """
402
411
  Process input file and generate output RDF files.
403
412
 
@@ -418,6 +427,7 @@ def process_file(input_fn: str | Path,
418
427
  :param fetch_url_whitelist: list of regular expressions to filter referenced JSON-LD context URLs before
419
428
  retrieving them. If None, it will not be used; if empty sequence or False, remote fetching operations will
420
429
  throw an exception
430
+ :param transform_args: Additional arguments to pass as variables to the jq transform
421
431
  :return: List of output files created
422
432
  """
423
433
 
@@ -448,6 +458,7 @@ def process_file(input_fn: str | Path,
448
458
  contexts = (util.load_yaml(context_fn),)
449
459
  else:
450
460
  provenance_contexts = context_fn
461
+ contexts = [util.load_yaml(fn) for fn in context_fn]
451
462
 
452
463
  if not contexts:
453
464
  raise MissingContextException('No context file provided and one could not be discovered automatically')
@@ -478,11 +489,18 @@ def process_file(input_fn: str | Path,
478
489
  end_auto=True,
479
490
  )
480
491
 
492
+ if transform_args is None:
493
+ transform_args = {}
494
+ transform_args['filename'] = str(input_fn.resolve())
495
+ transform_args['basename'] = str(input_fn.name)
496
+ transform_args['dirname'] = str(input_fn.resolve().parent)
497
+
481
498
  uplift_result = generate_graph(input_data,
482
499
  context=contexts,
483
500
  base=base,
484
501
  fetch_timeout=fetch_timeout,
485
- fetch_url_whitelist=fetch_url_whitelist)
502
+ fetch_url_whitelist=fetch_url_whitelist,
503
+ transform_args=transform_args)
486
504
 
487
505
  uplift_result.input_file = input_fn
488
506
 
@@ -619,7 +637,8 @@ def process(input_files: str | Path | Sequence[str | Path],
619
637
  skip_on_missing_context: bool = False,
620
638
  provenance_base_uri: Optional[Union[str, bool]] = None,
621
639
  fetch_timeout: int = 5,
622
- fetch_url_whitelist: Optional[Union[Sequence, bool]] = None) -> list[UpliftResult]:
640
+ fetch_url_whitelist: Optional[Union[Sequence, bool]] = None,
641
+ transform_args: dict | None = None) -> list[UpliftResult]:
623
642
  """
624
643
  Performs the JSON-LD uplift process.
625
644
 
@@ -640,6 +659,7 @@ def process(input_files: str | Path | Sequence[str | Path],
640
659
  :param fetch_url_whitelist: list of regular expressions to filter referenced JSON-LD context URLs before
641
660
  retrieving them. If None, it will not be used; if empty sequence or False, remote fetching operations will
642
661
  throw an exception
662
+ :param transform_args: Additional arguments to pass as variables to the jq transform
643
663
  :return: a list of JSON-LD and/or Turtle output files
644
664
  """
645
665
  result: list[UpliftResult] = []
@@ -679,6 +699,7 @@ def process(input_files: str | Path | Sequence[str | Path],
679
699
  fetch_timeout=fetch_timeout,
680
700
  fetch_url_whitelist=fetch_url_whitelist,
681
701
  domain_cfg=domain_cfg,
702
+ transform_args=transform_args,
682
703
  ))
683
704
  except Exception as e:
684
705
  if skip_on_missing_context:
@@ -699,6 +720,7 @@ def process(input_files: str | Path | Sequence[str | Path],
699
720
  fetch_timeout=fetch_timeout,
700
721
  fetch_url_whitelist=fetch_url_whitelist,
701
722
  domain_cfg=domain_cfg,
723
+ transform_args=transform_args,
702
724
  ))
703
725
  except Exception as e:
704
726
  if skip_on_missing_context:
@@ -808,6 +830,12 @@ def _process_cmdln():
808
830
  help='Set root directory for globs in domain configurations'
809
831
  )
810
832
 
833
+ parser.add_argument(
834
+ '--transform-arg',
835
+ nargs='*',
836
+ help='Additional argument to pass to the jq transforms in the form variable=value'
837
+ )
838
+
811
839
  args = parser.parse_args()
812
840
 
813
841
  if args.domain_config:
@@ -826,6 +854,10 @@ def _process_cmdln():
826
854
  if args.debug:
827
855
  logger.setLevel(logging.DEBUG)
828
856
 
857
+ transform_args = None
858
+ if args.transform_arg:
859
+ transform_args = dict((e.split('=', 1) for e in args.transform_arg))
860
+
829
861
  result = process(input_files,
830
862
  context_fn=args.context,
831
863
  domain_cfg=domain_cfg,
@@ -835,7 +867,8 @@ def _process_cmdln():
835
867
  skip_on_missing_context=args.skip_on_missing_context,
836
868
  provenance_base_uri=False if args.no_provenance else args.provenance_base_uri,
837
869
  fetch_url_whitelist=args.url_whitelist,
838
- )
870
+ transform_args=transform_args,
871
+ )
839
872
 
840
873
  if args.fs:
841
874
  print(args.fs.join(str(output_file) for r in result for output_file in r.output_files))
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ogc-na
3
- Version: 0.2.25
3
+ Version: 0.2.27
4
4
  Summary: OGC Naming Authority tools
5
5
  Author-email: Rob Atkinson <ratkinson@ogc.org>, Piotr Zaborowski <pzaborowski@ogc.org>, Alejandro Villar <avillar@ogc.org>
6
6
  Project-URL: Homepage, https://github.com/opengeospatial/ogc-na-tools/
7
- Project-URL: Documentation, https://opengeospatial.github.com/ogc-na-tools/
7
+ Project-URL: Documentation, https://opengeospatial.github.io/ogc-na-tools/
8
8
  Project-URL: Repository, https://github.com/opengeospatial/ogc-na-tools.git
9
9
  Keywords: ogc,ogc-na,naming authority,ogc rainbow,definitions server
10
10
  Classifier: Development Status :: 4 - Beta
@@ -3,7 +3,7 @@ ogc/na/annotate_schema.py,sha256=RP4LTP0Xm3VTfd_4YrCAcdV2SMHzcyvX-q7Kou19vXM,244
3
3
  ogc/na/domain_config.py,sha256=57BR-vue3OAAlj58lHqocco6MAZPBo7VaQt0BFQehcc,13409
4
4
  ogc/na/download.py,sha256=2afrLyl4WsAlxkCgXsl47fs9mNKfDmhVpeT2iwNSoq0,3354
5
5
  ogc/na/gsp.py,sha256=o_mwUG-FyDqLmA7Mx1eKkXqM7yX8BqxIwHuCS2eE1fo,5626
6
- ogc/na/ingest_json.py,sha256=qAWvRHZtVM2vC_QXkqkgzMaDibR-bL_vU0KIGmohPzE,32394
6
+ ogc/na/ingest_json.py,sha256=M_f2uiQgjzKAvKdLCp0HdxYQksFjG6Di4GIhr7YyLk8,34018
7
7
  ogc/na/profile.py,sha256=K8ylakfXdmp8mwB_jF06FGuANBvg8XtPQIm05LZ8wXM,16477
8
8
  ogc/na/provenance.py,sha256=zm3RwfnjISQfTekYXu1-GlgezGcUJSbmptTkLcitcfs,5446
9
9
  ogc/na/update_vocabs.py,sha256=zF1J7hel3iQ6n3NC3g1B_hZvVnpnzQ4diLb5oh0yA1w,18229
@@ -11,7 +11,7 @@ ogc/na/util.py,sha256=gQj-dQAfVd53Xup56iDna4NvYI3SmR2S-6cc84eBWMM,7995
11
11
  ogc/na/validation.py,sha256=4cbjNiVWZmHxDuS4fU5YPTHdgJyJkLKI8BNSj3d5ULI,4255
12
12
  ogc/na/input_filters/__init__.py,sha256=PvWEI-rj5NNR_Cl903wHtS08oTywjDYEfappY5JtVkI,1169
13
13
  ogc/na/input_filters/csv.py,sha256=O2E3ivjP9i5tGtyvb6UjmR4eM7toVkzP58EUm6bxvfw,2530
14
- ogc_na-0.2.25.dist-info/METADATA,sha256=B8TjuBjyl2qdmE2OuMMJoRCHeQzzYh5PgQ0Hry0uQ5w,3558
15
- ogc_na-0.2.25.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
16
- ogc_na-0.2.25.dist-info/top_level.txt,sha256=Kvy3KhzcIhNPT4_nZuJCmS946ptRr_MDyU4IIhZJhCY,4
17
- ogc_na-0.2.25.dist-info/RECORD,,
14
+ ogc_na-0.2.27.dist-info/METADATA,sha256=CRShuVbACaJjMFF7cZOpaNoNdv6-BBqn-UeGR3R0Toc,3557
15
+ ogc_na-0.2.27.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
16
+ ogc_na-0.2.27.dist-info/top_level.txt,sha256=Kvy3KhzcIhNPT4_nZuJCmS946ptRr_MDyU4IIhZJhCY,4
17
+ ogc_na-0.2.27.dist-info/RECORD,,