summaryrefslogtreecommitdiff
path: root/app-misc/yq/files/yq-3.1.1-tomli.patch
blob: b4ef5ef3dfb9a8944a798e0b7b85834029c218ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 <https://toml.io/>`_ as well. The ``yq`` package installs an executable, ``tomlq``, which uses the
-`toml library <https://github.com/uiri/toml>`_ to transcode TOML to JSON, then pipes it to ``jq``. Roundtrip transcoding
+`tomllib module <https://docs.python.org/3.11/library/tomllib.html>` or `tomli library
+<https://github.com/hukkin/tomli>`_ to transcode TOML to JSON, then pipes it to ``jq``. Transcoding to TOML uses the
+`tomli-w <https://github.com/hukkin/toml-w`_ package. Roundtrip transcoding
 is available with the ``tomlq --toml-output``/``tomlq -t`` option.
 
 .. admonition:: Compatibility note
diff --git a/setup.py b/setup.py
index 9de217e..7d34f8c 100755
--- a/setup.py
+++ b/setup.py
@@ -19,7 +19,8 @@ setup(
     install_requires=[
         "PyYAML >= 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")