如何从 Python 导入正确调用 jsonnet

cer*_*wen 5 python python-import jsonnet

我正在使用 jsonnet 构建将由 Python 代码使用的 json 对象,使用bindings从 Python 调用 jsonnet 。我想设置我的目录结构,以便 jsonnet 文件位于与运行 Python 代码的位置相关的一个或多个子目录中,例如:

foo.py jsonnet/ jsonnet/bar.jsonnet jsonnet/baz.libsonnet

运行foo.py然后应该能够使用_jsonnet.evaluate_snippet()从文件中读取的字符串jsonnet/,从jsonnet/. 做到这一点的最佳方法是什么?

sba*_*ski 5

默认导入器使用相对于导入它们的文件的路径。如果evaluate_snippet您需要手动传递路径。这样 jsonnet 就知道在哪里查找导入的文件。

如果您打算处理这些文件,您可以使用自定义导入程序。(题外话:jsonnet 试图避免对源文件进行预处理,因此 jsonnet 中可能有更好的方法或缺少的功能。)

下面是关于如何在 Python 中使用自定义导入器的完整工作示例(根据提供的目录结构进行了调整):

import os
import unittest

import _jsonnet


#  Returns content if worked, None if file not found, or throws an exception
def try_path(dir, rel):
    if not rel:
        raise RuntimeError('Got invalid filename (empty string).')
    if rel[0] == '/':
        full_path = rel
    else:
        full_path = dir + rel
    if full_path[-1] == '/':
        raise RuntimeError('Attempted to import a directory')

    if not os.path.isfile(full_path):
        return full_path, None
    with open(full_path) as f:
        return full_path, f.read()


def import_callback(dir, rel):
    full_path, content = try_path(dir, rel)
    if content:
        return full_path, content
    raise RuntimeError('File not found')


class JsonnetTests(unittest.TestCase):
    def setUp(self):
        self.input_filename = os.path.join(
            "jsonnet",
            "bar.jsonnet",
        )
        self.expected_str = '{\n   "num": 42,\n   "str": "The answer to life ..."\n}\n'
        with open(self.input_filename, "r") as infile:
            self.input_snippet = infile.read()

    def test_evaluate_file(self):
        json_str = _jsonnet.evaluate_file(
            self.input_filename,
            import_callback=import_callback,
        )
        self.assertEqual(json_str, self.expected_str)

    def test_evaluate_snippet(self):
        json_str = _jsonnet.evaluate_snippet(
            "jsonnet/bar.jsonnet",
            self.input_snippet,
            import_callback=import_callback,
        )
        self.assertEqual(json_str, self.expected_str)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

注意:它是jsonnet repo 示例的修改版本。