Skip to main content

A versatile Python library for loading and managing configuration files (JSON, YAML, XML, TOML, and INI/DEF-style) with attribute-style access, merging capabilities.

Project description

loadstructure

A versatile Python library for loading and managing configuration files (JSON, YAML, XML, TOML, and INI/DEF-style) with attribute-style access, merging capabilities.

✨ Features

  • Multiple Formats: Load configurations from JSON, YAML, XML, TOML, and INI/DEF files.
  • Attribute Access: Access settings using dot notation (e.g., config.server.port).
  • Auto-Creation: Automatically creates nested keys if they do not exist.
  • Schema Validation: Enforce types and structure with optional schema support.
  • Merging: Merge multiple configuration files, with later files overriding earlier settings.
  • Update & Replace: Update partial configs with .update() or replace entire configs with .replace().
  • Saving: Save your configuration back to the original or new file format.

Installation

Using uv:

uv add loadstructure

Using pip

pip install loadstructure

📦 Dependencies

The following dependencies are required and will be installed automatically if you use standard Python packaging practices:

  • YAML: pyyaml >=6.0.3
  • TOML: toml >=0.10.2
  • XML: xmltodict >=1.0.2

📖 Usage

Basic Loading

Instantiate ConfigManager with the path to your config file and call .load().

config.json:

{
  "app": {
    "name": "App",
    "version": "1.0.0",
    "features": {
      "login": true,
      "analytics": false,
      "dark_mode": true,
      "notifications": true
    },
    "ui": {
      "theme": "light",
      "language": "en"
    }
  },
  "modules": {
    "editor": {
      "enabled": true,
      "autosave_interval": 5
    },
    "export": {
      "formats": ["pdf", "html", "md"],
      "compress_output": false
    }
  }
}

Load a configuration file

from loadstructure import ConfigManager

config = ConfigManager("app_config.json")
cfg = config.load()

# Attribute-style access
print(cfg.app.name)                       # App
print(cfg.app.ui.theme)                   # light
print(cfg.modules.editor.autosave_interval)  # 5

# Dictionary-style access
print(cfg["app"]["features"]["dark_mode"])  # True

Nested key access using dotted paths

theme = cfg.get("app.ui.theme")
print(theme)  # light

Schema validation

config = ConfigManager("config.json", schema={
    "app": {"name": str, "version": str},
    "modules": dict
})
cfg = config.load()

cfg.app.name = "MyApp"  # valid
cfg.app.version = 2      # raises SchemaError because type must be str

Updating and replacing configurations

# Update part of the config
cfg.update({
    "app": {
        "version": "1.1.0"
    }
})

# Replace entire config
cfg.replace({
    "app": {
        "name": "NewApp",
        "version": "2.0.0"
    }
})

Modifying and saving values

cfg.app.features.analytics = True
cfg.modules.export.compress_output = True
fg.app.tech = {
    'frontend':'(html,css,js)',
    'backend':'electron'
}

# Save changes back to JSON
config.save()

Merging multiple configuration files

# app_config_override.json can override some settings
merged_cfg = ConfigManager.merge(["app_config.json", "app_config_override.json"])
print(merged_cfg.app.features.analytics)

License

This project is licensed under the MIT License for full details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loadstructure-0.2.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loadstructure-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file loadstructure-0.2.0.tar.gz.

File metadata

  • Download URL: loadstructure-0.2.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for loadstructure-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4c06a0d439e01f90b9b06f2331da4b1dcf75739988b6ce5920487127c6712b07
MD5 f9765ac1571c69e57f8da8d3a4fb8685
BLAKE2b-256 c82bd4e917a7dd3a37082e9088711fd62c0304e3e2606440142238fb5d5bdd85

See more details on using hashes here.

File details

Details for the file loadstructure-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for loadstructure-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8b06ac3f876916f94fc94463050e478224335948a9f755ce75c0a51fac56355
MD5 05a10c6c3f0643875575c9a05ac60291
BLAKE2b-256 5ac90d9f5d87680406872563ea570c5f60af5e21ce90f61b138e035d1116183b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page