def load(filename, fpath=(), dpath=None, **kwds): '''Load a file and return its data structure. If dpath given, return substructure at that "path" in the resulting data structure. If filename is not absolute, it will be searched for first in the current working directory, then moo's built-in directory, then in any user-provided paths in "fpath" and finally in directories provided by moo.io.default_load_path. The application is free to provide this fallback. When moo is used from its CLI the MOO_LOAD_PATH environment variable is consulted. ''' fmt = os.path.splitext(filename)[-1] paths = clean_paths(fpath) + list(default_load_path) filename = resolve(filename, paths) if fmt in (".jsonnet", ".schema"): data = moo.jsonnet.load(filename, paths, **kwds) elif fmt in (".csv", ): data = moo.csvio.load(filename, paths, **kwds) elif fmt in (".xls", ".xlsx"): data = moo.xls.load(filename, paths, **kwds) else: data = anyconfig.load(filename) if data is None: raise ValueError(f'no data from {filename}') if dpath: return select_path(data, dpath) return data
def loads(jtext, paths=(), **kwds): ''' Load Jsonnet text ''' paths = clean_paths(paths) ic = ImportCallback(paths) text = evaluate_snippet("<stdin>", jtext, import_callback=ic, **kwds) return json.loads(text)
def imports(fname, paths=(), **kwds): ''' Return the imports needed by the Jsonnet file ''' paths = clean_paths(paths) fname = resolve(fname, paths) ic = ImportCallback(paths) evaluate_file(fname, import_callback=ic, **kwds) ret = list(ic.found) ret.sort() return ret
def load(fname, paths=(), **kwds): ''' Load a Jsonnet file. some useful kwds are: - ext_vars :: dictionary of variables, to get via std.extVar() - ext_codes :: dictionary of code, to get via std.extVar() - native_callbacks :: call python from Jsonnet - import_callbacks :: help find imports Application code should consider using moo.io.load(). ''' paths = clean_paths(paths) fname = resolve(fname, paths) ic = ImportCallback(paths) try: text = evaluate_file(fname, import_callback=ic, **kwds) except RuntimeError as err: raise RuntimeError(f"in file: {fname}") from err return json.loads(text)
def test_clean_paths(): 'Check clean paths' paths = clean_paths("some-file") assert len(paths) == 2