{"id":28958742,"url":"https://github.com/aqzi/settingsloader","last_synced_at":"2026-03-18T02:03:15.099Z","repository":{"id":300409983,"uuid":"1006112099","full_name":"aqzi/SettingsLoader","owner":"aqzi","description":"Type safe settings loader for python - support for env, args, secrets, app setttings and more","archived":false,"fork":false,"pushed_at":"2025-09-17T13:32:34.000Z","size":65,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-29T14:17:01.196Z","etag":null,"topics":["config","parser","python","settings","typesafe"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aqzi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-06-21T14:18:48.000Z","updated_at":"2025-09-17T13:32:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"d7ebb270-d577-4da1-b82c-afbaaa6a2a40","html_url":"https://github.com/aqzi/SettingsLoader","commit_stats":null,"previous_names":["aqzi/settingsloader"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aqzi/SettingsLoader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aqzi%2FSettingsLoader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aqzi%2FSettingsLoader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aqzi%2FSettingsLoader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aqzi%2FSettingsLoader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aqzi","download_url":"https://codeload.github.com/aqzi/SettingsLoader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aqzi%2FSettingsLoader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30641722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-18T01:41:58.583Z","status":"online","status_checked_at":"2026-03-18T02:00:07.824Z","response_time":104,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","parser","python","settings","typesafe"],"created_at":"2025-06-23T23:06:45.155Z","updated_at":"2026-03-18T02:03:15.093Z","avatar_url":"https://github.com/aqzi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SettingsLoader\n![Version 0.2.2](https://img.shields.io/badge/version-0.2.2-brightgreen) ![example workflow](https://github.com/aqzi/SettingsLoader/actions/workflows/python-tests.yml/badge.svg) ![License MIT](https://img.shields.io/badge/license-MIT-green])\n\nSettingsLoader is a component to load env, args, secrets and app settings into one type safe object. It's especially valuable if you need to pull settings from multiple sources. By default, it supports YAML, JSON, .env files, and command-line arguments. Additionally, you can extend it with custom source loaders to handle other file formats or configuration mechanisms as needed.\n\n![code2flow logo](https://raw.githubusercontent.com/aqzi/SettingsLoader/master/assets/flow.png)\n\n## Install\nInstall the package with pip:\n```sh\npip install SettingsLoaderTypeSafe\n```\n\nOnce installed, import it in your project and start using it right away. For example:\n```python\nfrom settings_loader.core import SettingsLoader\n\n#Settings: your main settings class\n#settings.yml: path to main config file\nsettings = SettingsLoader(Settings, 'settings.yml').load()\n```\n\n\n## Usage guide\nThis step-by-step guide demonstrates how to use the library with a playful example featuring a lion to showcase the different features.\n\n1. \u003cb\u003eDefine your main settings file\u003c/b\u003e\u003cbr/\u003e\nThe main settings file must be in either YAML or JSON format. This file should include a `settings_loader` object that specifies the configuration for your sources. The `settings_loader` maps source keys to their corresponding source files. The supported file types for the sources are: json, yaml and env. Don't use 'env', 'args' and 'this' as custom source keys, as these are reserved for loading environment variables, command-line arguments and for making references within the current context, respectively.\n\n    ```yml\n    settings_loader:\n        animal_info: [animal_settings/animal_specs.json, animal_settings/animal_specs_part2.yml]\n        animal_caretaker_info: [animal_settings/caretaker_info.txt] #this requires custom loader as .txt is not supported by default\n        secrets: [animal_settings/secret_info.env]\n    ```\n\n    The main settings file should contain all the configuration settings for your application. Within this file, you can reference values from other parts of the main settings file itself, from external source files, or from command-line arguments.\n    References are defined using the syntax: `{{\u003csource_key\u003e.\u003cfield_path\u003e}}`. The reference placeholder is dot seperated!\n    - source_key refers to a key defined in your settings_loader.\n    - field_path specifies the path to the desired value within the corresponding source file. Fields within field_path are also dot seperated.\n    \n    Within a reference, the `\u003e` symbol defines a fallback sequence. The settings loader first tries to resolve the value from the leftmost source. If that source is None, it proceeds to the next one, and so on, until a value is found. The sequence may end with a default value if desired.\n\n    ```yml\n    animal_name: Leo #static value\n\n    #Will be replaced by 'animal_type' value within the animal_info source. The 'animal_info' key is binded to both settings/animal_specs.json and settings/animal_specs_part2.yml\n    #If 'animal_info' would be specified in both files, it will be taken from the last source in the list. In this case it would come from animal_specs_part2.yml\n    species: \"{{animal_info.animal_type}}\"\n\n    specs:\n        endangered: \"{{animal_info.extra_spec.endangered}}\" #extra_spec.endangered is a path to a nested field within animal_info source\n        weight_kg: \"{{animal_info.weight_kg}}\"\n        characteristics:\n            habitat: \"{{animal_info.characteristics.habitat}}\"\n            lifespan_years: \"{{animal_info.characteristics.lifespan_years}}\"\n\n    feeding_schedule:\n        # food_type is taken from args, then env, otherwise defaults to \"fish\". The sequence can be extended.\n        # if the default value contains dots, make sure to wrap it in single quoates\n        food_type: \"{{args.food_type \u003e env.food_type \u003e fish}}\"\n        feeding_times_per_day: 3\n\n    medical_history:\n        primary_vet: John\n        last_checkup_date: \"{{secrets.last_vet_visit_date}}\"\n        vaccinations_up_to_date: \"{{secrets.vaccinations_status}}\"\n\n    caretaker_info:\n        name: Emma\n        contact_number: \"{{animal_caretaker_info.contact_number}}\"\n        preferred_language: \"{{animal_caretaker_info.primary_language}}\"\n        #You can add additional text along with 1 or more refs. However this only works for strings!\n        area_of_expertise: \"All animals living in {{this.specs.characteristics.habitat}}\"\n\n    preferences:\n        favorite_toys: [ball, \"{{animal_info.toy}}\"]\n    ```\n\n    We also support object references with optional field overwrites. To apply overwrites, use the reserved keyword `__base__` to specify the object to copy from, then define any fields you want to override.\n\n    ```yml\n    specs_copy: \"{{this.specs}}\" #Creates a direct copy of the existing 'specs' object (from main settings) without any modifications.\n    specs_copy_with_overwrite:\n    __base__: \"{{this.specs}}\" #Direct copy of specs\n    weight_kg: \"{{env.weight_kg}}\" #After copy, overwrite weight_kg with value from the environment\n    characteristics:\n        lifespan_years: \"15-20\" #another overwrite\n    ```\n\n    To finalize this first step, consider the following example illustrating settings from one of the source files: animal_settings/secrets_info.env, which is mapped to the source key 'secrets'. In the main settings file, medical_history.last_checkup_date references last_vet_visit_date from the secrets source, resulting in the value '2025-06-01'. References within the same source file are also supported. Typically, source keys used in references correspond to those defined in the settings_loader section of the main settings file. However, there is an exception: the reserved keyword 'this', which allows referencing other fields within the same source file. In the example below, vaccinations_status references status in the same .env file using {{this.status}}. This resolves vaccinations_status to True. In the main settings, medical_history.vaccinations_up_to_date can then refer to vaccinations_status in secrets, and will also resolve to True.\n    ```env\n    last_vet_visit_date=2025-06-01\n    vaccinations_status={{this.status}}\n\n    status=True\n    ```\n\n2. \u003cb\u003eDefine your data classes\u003c/b\u003e\u003cbr/\u003e\nCreate data classes to represent the structure of your settings. Each class must inherit from pydantic.BaseModel to ensure type safety and validation. Be sure to assign default values to all optional fields. In this example, the AnimalConfig class models the complete set of information from the main settings file. Note that the `settings_loader` section is excluded from these data classes.\n\n    ```python\n    class FeedingSchedule(BaseModel):\n        food_type: str = \"meat\"\n        feeding_times_per_day: int = 2\n\n    class MedicalHistory(BaseModel):\n        primary_vet: str\n        last_checkup_date: Optional[datetime.date] = None #Optional field must have a default value!\n        vaccinations_up_to_date: bool = False\n        notes: Optional[str] = None\n\n    class CaretakerInfo(BaseModel):\n        name: str\n        contact_number: Optional[str] = None\n        preferred_language: str = \"English\"\n        area_of_expertise: str\n\n    class Characteristics(BaseModel):\n        habitat: str\n        lifespan_years: str\n\n    class Specs(BaseModel):\n        weight_kg: float\n        endangered: bool = False\n        characteristics: Characteristics\n\n    class Preferences(BaseModel):\n        favorite_toys: list[str]\n\n    class AnimalConfig(BaseModel):\n        animal_name: str\n        species: str\n        specs: Specs\n        feeding_schedule: FeedingSchedule\n        medical_history: MedicalHistory\n        caretaker_info: CaretakerInfo\n        preferences: Preferences\n        specs_copy: Specs\n        specs_copy_with_overwrite: Specs\n    ```\n\n3. \u003cb\u003eCreate the script\u003c/b\u003e\u003cbr/\u003e\nYou’ll always need to load the main settings file, but how you extend it depends on your configuration. Below are three common patterns:\n    1. Load your main settings file and specify the corresponding data class for type safety:\n        ```python\n        settings = SettingsLoader(AnimalConfig, 'settings.yml').load()\n        ```\n    2. In addition to the main settings, you can parse command-line arguments. This requires defining an argument class:\n        ```python\n        class ArgsSettings(BaseModel):\n            food_type: Optional[str]\n\n        settings = SettingsLoader(AnimalConfig, 'settings.yml').with_args(ArgsSettings).load()\n        ```\n    3. If you have a custom file format (ex: .txt), define a loader function and register it with the loader:\n        ```python\n        def load_txt_key_value(file_path: str) -\u003e dict[str, str]:\n            result: dict[str, str] = {}\n            with open(file_path, 'r', encoding='utf-8') as f:\n                for line in f:\n                    line = line.strip()\n                    if not line or \":\" not in line:\n                        continue\n                    key, value = line.split(\":\", 1)\n                    result[key.strip()] = value.strip()\n            return result\n\n        settings = (\n            SettingsLoader(AnimalConfig, 'settings.yaml')\n                .with_custom_source_loaders({\n                    'txt': load_txt_key_value #txt corresponds to file extension name\n                })\n                .load()\n        )\n        ```\n    More examples can be found inside the unit tests.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faqzi%2Fsettingsloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faqzi%2Fsettingsloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faqzi%2Fsettingsloader/lists"}