StackGuru
StackGuru

Reputation: 503

How to Build Config Files in python?

As I'm new and learning python, exploring different ways to build a config file for python based framework.

I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.

# config.py

class Config:
    APP_NAME = 'myapp'
    SECRET_KEY = 'secret-key-of-myapp'
    ADMIN_NAME = 'administrator'

    AWS_DEFAULT_REGION = 'ap-northeast-2'

    STATIC_PREFIX_PATH = 'static'
    ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
    MAX_IMAGE_SIZE = 5242880 # 5MB


class DevelopmentConfig(Config):
    DEBUG = True

    AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
    AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
    AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'

    DATABASE_URI = 'database-uri-for-dev'

class TestConfig(Config):
    DEBUG = True
    TESTING = True

    AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
    AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
    AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'

    DATABASE_URI = 'database-uri-for-dev'


class ProductionConfig(Config):
    DEBUG = False

    AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
    AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
    AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'

    DATABASE_URI = 'database-uri-for-dev'


class CIConfig:
    SERVICE = 'travis-ci'
    HOOK_URL = 'web-hooking-url-from-ci-service'

# main.py

import sys
import config

...

if __name__ == '__main__':
    env = sys.argv[1] if len(sys.argv) > 2 else 'dev'

    if env == 'dev':
        app.config = config.DevelopmentConfig
    elif env == 'test':
        app.config = config.TestConfig
    elif env == 'prod':
        app.config = config.ProductionConfig
    else:
        raise ValueError('Invalid environment name')

    app.ci = config.CIConfig

What is app.config and app.ci ? How is it being used ?

  1. And also, what all other best possible pythonic way to manage config files ?
  2. If I have multiple set of profiles/credentials (username-password), how do i manage them ?
  3. Any possible encryption to files containing credentials ?

Will be of great learning to me.

Upvotes: 0

Views: 1567

Answers (1)

Bishal
Bishal

Reputation: 837

Here is a small example of how you could use config files

class Config:
  APP_NAME='myapp'
  ADMIN='admin'

class DevelopmentConfig(Config):
  DEBUG = True
  ADMIN = 'dev_admin'

class ProductionConfig(Config):
  DEBUG = False

def main():
  config = ProductionConfig # Change to DevelopmentConfig to experiment

  # You may now use your config where you want
  print(config.DEBUG)
  print(config.ADMIN)

if __name__ == "__main__":
  main()

This example does not use command line arguments like your example but should give you a good idea of building config files and using them.

In your example app.ci refers to configuration for continuous integration(CI) environment.

Upvotes: 1

Related Questions