GithubHelp home page GithubHelp logo

confucius's Introduction

Confucius

A simpler, clearer approach to configuration.

Quick Start

from confucius import BaseConfig

class Config(BaseConfig)
    HOST = '127.0.0.1'
    PORT : int = 8000

    DEBUG : bool = False
from myapp import Server
from config import Config


server = Server(Config.HOST, Config.PORT)
$ python app.py
- Starting server: 127.0.0.1:8000

$ PORT=80 python app.py
- Starting server: 127.0.0.1:80

$ DEBUG=y python app.py
- Starting debug server: 127.0.0.1:80

Types

Any ANGRY_SNAKE_CASE attributes of a BaseConfig sub-class will be intercepted by the meta-class, and checked for in the environment using os.getenv.

Their type will be determined by their annotation in the class, or fall back to str.

Methods will automatically behave like a property, with access to self.

Handling of type casting can be overridden [as it is for bool] by adding it to the __types__ dict:

class Config(BaseConfig):
    __types__ = {
        json: lambda v: json.loads(v) if isinstance(v, str) else v,
    }

    LOGGING : json = {'version': 1 ...}

All types defined on parent Config classes will be merged with this dict.

Inheritance

Classes, as usual, inherit from their parents. If the type of an attribute is changed, it will raise an AssertionError.

Methods

Method in all-caps will be invoked, and can access self as usual:

class Config(BaseConfig):
   DB_ENGINE = 'postgresql'
   DB_HOST = 'localhost'
   DB_PORT : int = 5432
   DB_USER = 'test_user'
   DB_PASS = 'secret'
   DB_NAME = 'test-db'

   def CONNECTION_STRING(self):
       return f'{self.DB_ENGINE}://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}/{self.DB_NAME}'

Using in Django

In your settings.py, put your settings class (or classes), then use the following code to select one to use:

import os
MODE = os.getenv('DJANGO_MODE', 'Local')
globals().update(globals()[f'{ MODE.title() }Settings'].as_dict())

With Python 3.7

In Python 3.7, a new feature was added which allowed you to define __getattr__ for a module (See PEP 562).

The BaseConfig metaclass provides a module_getattr_factory factory method to provide a __getattr__ that will look up the Config object.

from confucius import BaseConfig

class Config(BaseConfig):
    DB_HOST = 'localhost'
    DB_PORT : int = 5432

__getattr__ = Config.module_getattr_factory()

After importing this module, attempts to access attributes will resolve normally and, if they're not found, call __getattr__, just like on an object.

confucius's People

Contributors

funkybob avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.