diff --git a/README.rst b/README.rst index 69d77e1..237b89e 100644 --- a/README.rst +++ b/README.rst @@ -109,7 +109,9 @@ the ``xq --xml-output``/``xq -x`` option. Multiple XML documents can be passed i TOML support ------------ ``yq`` supports `TOML `_ as well. The ``yq`` package installs an executable, ``tomlq``, which uses the -`toml library `_ to transcode TOML to JSON, then pipes it to ``jq``. Roundtrip transcoding +`tomllib module ` or `tomli library +`_ to transcode TOML to JSON, then pipes it to ``jq``. Transcoding to TOML uses the +`tomli-w = 5.3.1", "xmltodict >= 0.11.0", - "toml >= 0.10.0", + "tomli >= 1.2.3; python_version < '3.11'", + "tomli-w", "argcomplete >= 1.8.1", ], extras_require={ diff --git a/yq/__init__.py b/yq/__init__.py index 0ccb8e8..88da1d7 100644 --- a/yq/__init__.py +++ b/yq/__init__.py @@ -246,9 +246,12 @@ def yq( json.dump(doc, json_buffer, cls=JSONDateTimeEncoder) json_buffer.write("\n") elif input_format == "toml": - import toml + if sys.version_info >= (3, 11): + import tomllib + else: + import tomli as tomllib - doc = toml.load(input_stream) # type: ignore + doc = tomllib.loads(input_stream.read()) # type: ignore json.dump(doc, json_buffer, cls=JSONDateTimeEncoder) json_buffer.write("\n") else: @@ -295,13 +298,13 @@ def yq( raise output_stream.write(b"\n" if sys.version_info < (3, 0) else "\n") elif output_format == "toml": - import toml + import tomli_w for doc in decode_docs(jq_out, json_decoder): if not isinstance(doc, dict): msg = "{}: Error converting JSON to TOML: cannot represent non-object types at top level." exit_func(msg.format(program_name)) - toml.dump(doc, output_stream) + output_stream.write(tomli_w.dumps(doc)) else: if input_format == "yaml": loader_class = get_loader( @@ -327,10 +330,13 @@ def yq( ) jq.stdin.write("\n") # type: ignore elif input_format == "toml": - import toml + if sys.version_info >= (3, 11): + import tomllib + else: + import tomli as tomllib for input_stream in input_streams: - json.dump(toml.load(input_stream), jq.stdin, cls=JSONDateTimeEncoder) # type: ignore + json.dump(tomllib.loads(input_stream.read()), jq.stdin, cls=JSONDateTimeEncoder) # type: ignore jq.stdin.write("\n") # type: ignore else: raise Exception("Unknown input format")