ZIO
Python and C++ interface to ZeroMQ and Zyre
jsonnet.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import os
4 import json
5 import _jsonnet
6 
7 def try_path(path, rel):
8  if not rel:
9  raise RuntimeError('Got invalid filename (empty string).')
10  if rel[0] == '/':
11  full_path = rel
12  else:
13  full_path = os.path.join(path, rel)
14  if full_path[-1] == '/':
15  raise RuntimeError('Attempted to import a directory')
16 
17  if not os.path.isfile(full_path):
18  return full_path, None
19  with open(full_path) as f:
20  return full_path, f.read()
21 
22 def import_callback(path, rel):
23  paths = [path] + os.environ.get("WIRECELL_PATH","").split(":")
24  paths += os.environ.get("JSONNET_PATH","").split(":")
25  for maybe in paths:
26  if not maybe:
27  continue
28  try:
29  full_path, content = try_path(maybe, rel)
30  except RuntimeError:
31  continue
32  if content:
33  return full_path, content
34  raise RuntimeError('File not found')
35 
36 
37 def load(fname):
38  '''
39  Load and evaluate a jsonnet file returning data structure.
40  '''
41  text = _jsonnet.evaluate_file(fname, import_callback=import_callback)
42  return json.loads(text)
43 
44 
def import_callback(path, rel)
Definition: jsonnet.py:22
def try_path(path, rel)
Definition: jsonnet.py:7
def load(fname)
Definition: jsonnet.py:37