Thursday, 8 June 2023

update toml file(with varibles) using python

I am using this code to update the existing toml file which contains variables. Python toml module is not able to parse this toml file, how can use this code to update the toml file. I have tried the 'from yaml.loader import SafeLoader' also, but didn't help.

Code:

import toml,requests,uuid,argparse
import collections.abc

class TOMLConfig:
    def __init__(self,config, file):
        self.config = config
        self.file=file

    def update(self):
        with open(self.file, "r") as f:
            existing_config = toml.load(f)

        self._update_config(existing_config, self.config)

        with open(self.file, "w") as f:
            f.write(toml.dumps(existing_config))

    def _update_config(self, existing_config, new_config):
        for key, value in new_config.items():
            if isinstance(value, collections.abc.Mapping):
                self._update_config(existing_config.setdefault(key, {}), value)
            else:
                existing_config[key] = value

TOML FIle

[system]
data_directory = "${DATA_DIRECTORY}"

[server]
telemetry_enabled = ${CONFIGS_TELEMETRY_ENABLED}

Error:

╰─> python3 /tmp/config.py -p  "{'data':{'shared_buffer_capacity_mb':8096,'block_cache_capacity_mb':8096,'AA_cache_capacity_mb':2048,'AA_memory_limit_mb':10240},'streaming':{'minimal_scheduling':True}}" -f /tmp/b
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/toml/decoder.py", line 511, in loads
    ret = decoder.load_line(line, currentlevel, multikey,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/toml/decoder.py", line 778, in load_line
    value, vtype = self.load_value(pair[1], strictly_valid)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/toml/decoder.py", line 910, in load_value
    raise ValueError("This float doesn't have a leading "
ValueError: This float doesn't have a leading digit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/config.py", line 45, in <module>
    f=cobj.update()
      ^^^^^^^^^^^^^
  File "/tmp/config.py", line 20, in update
    existing_config = toml.load(f)
                      ^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/toml/decoder.py", line 156, in load
    return loads(f.read(), _dict, decoder)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/toml/decoder.py", line 514, in loads
    raise TomlDecodeError(str(err), original, pos)
toml.decoder.TomlDecodeError: This float doesn't have a leading digit (line 5 column 1 char 88)



from update toml file(with varibles) using python

No comments:

Post a Comment