1
0
mirror of https://github.com/pirate/ArchiveBox.git synced 2025-08-12 01:24:53 +02:00

add header to generated TOML file warning its been converted from INI

This commit is contained in:
Nick Sweeting
2024-09-22 19:27:33 -07:00
parent 7f05026022
commit c8ff8f2b86

View File

@@ -6,6 +6,8 @@ import ast
JSONValue = str | bool | int | None | List['JSONValue'] JSONValue = str | bool | int | None | List['JSONValue']
TOML_HEADER = "# Converted from INI to TOML format: https://toml.io/en/\n\n"
def load_ini_value(val: str) -> JSONValue: def load_ini_value(val: str) -> JSONValue:
"""Convert lax INI values into strict TOML-compliant (JSON) values""" """Convert lax INI values into strict TOML-compliant (JSON) values"""
if val.lower() in ('true', 'yes', '1'): if val.lower() in ('true', 'yes', '1'):
@@ -22,7 +24,7 @@ def load_ini_value(val: str) -> JSONValue:
try: try:
return json.loads(val) return json.loads(val)
except Exception as err: except Exception:
pass pass
return val return val
@@ -50,7 +52,7 @@ def convert(ini_str: str) -> str:
toml_dict[section.upper()][key.upper()] = json.dumps(parsed_value) toml_dict[section.upper()][key.upper()] = json.dumps(parsed_value)
# Build the TOML string # Build the TOML string
toml_str = "" toml_str = TOML_HEADER
for section, items in toml_dict.items(): for section, items in toml_dict.items():
toml_str += f"[{section}]\n" toml_str += f"[{section}]\n"
for key, value in items.items(): for key, value in items.items():
@@ -63,7 +65,7 @@ def convert(ini_str: str) -> str:
### Basic Assertions ### Basic Assertions
test_input = r""" test_input = """
[SERVER_CONFIG] [SERVER_CONFIG]
IS_TTY=False IS_TTY=False
USE_COLOR=False USE_COLOR=False
@@ -225,7 +227,7 @@ NODE_VERSION=v21.7.3
""" """
expected_output = r'''[SERVER_CONFIG] expected_output = TOML_HEADER + '''[SERVER_CONFIG]
IS_TTY = false IS_TTY = false
USE_COLOR = false USE_COLOR = false
SHOW_PROGRESS = false SHOW_PROGRESS = false