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.1.tar.gz (10.4 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.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for loadstructure-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fdecb0715b5e9600c61e6eb75cc09cecaba7ee6e353a8145f9b45eef0c608eee
MD5 c05a27781a61a15c8d24fd4dc2d8003c
BLAKE2b-256 e3649063fded343bb53079dc94610423b7e3852eb8268cb27aa0f6d9ca2722c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loadstructure-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4c103a5a9e105d9c2219c487455b6700877b3117ede11d8efb7f655574a4519
MD5 9cf691c652a08fb65b13e4f60216cbe8
BLAKE2b-256 907a6a915521bf308d717e2ddc67fc70bb5ca610b8986db3228121028bcd5ce5

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