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.2.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.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loadstructure-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 c032c94c4379de2776862d45747ecd65e581480fa0b8405a9ebcbc4494a3cfa2
MD5 da96827dde686a2a1722028586c24639
BLAKE2b-256 33cf38d2736a478cdd16cad95859e1da8961963fa3a49661a64b0a9b085fba61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loadstructure-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c6e381ab9aa07737a2a3f34ab14697fa76e9c042e4a71642c66308689c3269a1
MD5 1e2e09f8f2a3ed50f2d7b883950cebd5
BLAKE2b-256 3234213ae5efdce3be184515c3ba3bfdacc488a3b91b0ba03aff77306b921c18

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