Skip to main content

Simplifies and enchances functionalities in Python's ConfigParser

Project description

SmartConfigParser

https://travis-ci.org/ricard33/smartconfigparser.png

Builds on top of the builtin SafeConfigParser but adds various convenience functionalities like default values.

from smartconfigparser import Config

config = Config()
config.read('config.ini')

user = config.get('SECTION', 'user', 'John')
age = config.getint('SECTION', 'age', 28)
weight = config.getfloat('SECTION', 'weight', 80.2)
is_developer = config.getboolean('SECTION', 'is_developer', True)
hobbies = config.getlist('SECTION', 'hobbies', ['diving', 'making software'])

# if SECTION does not exist in config.ini get default values
print(user, age, weight, is_developer, hobbies)
# ('John', 28, 80.2, True, ['diving', 'making software'])

Installation

Install it with pip:

pip install smartconfigparser

Test

run tests

python test_smartconfigparser.py

Example

DJANGO default settings file

import os
from smartconfigparser import Config

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

CONFIG_PATH = os.path.join(BASE_DIR, 'config')
if not os.path.exists(CONFIG_PATH):
    os.makedirs(CONFIG_PATH)

CONFIG_FILE = os.path.join(CONFIG_PATH, 'config.ini')
config = Config()
config.read(CONFIG_FILE)

try:
    SECRET_KEY = config.get('DJANGO', 'SECRET_KEY')
except:
    print('SECRET_KEY not found! Generating a new one...')
    import random

    SECRET_KEY = "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$^&*(-_=+)") for i in range(50)])
    if not config.has_section('DJANGO'):
        config.add_section('DJANGO')
    config.set('DJANGO', 'SECRET_KEY', SECRET_KEY)
    with open(CONFIG_FILE, 'wt') as f:
        config.write(f)

DEBUG = config.getboolean('DJANGO', 'DEBUG', False)

ALLOWED_HOSTS = config.getlist('DJANGO', 'ALLOWED_HOSTS', ['localhost', '127.0.0.1'])

# ...

DATABASES = {
    'default': {
        'ENGINE': config.get('DATABASE', 'engine', 'django.db.backends.sqlite3'),
        'NAME': config.get('DATABASE', 'name', 'db.sqlite3'),
        'USER': config.get('DATABASE', 'user', ''),
        'PASSWORD': config.get('DATABASE', 'password', ''),
        'HOST': config.get('DATABASE', 'host', ''),
        'PORT': config.get('DATABASE', 'port', ''),
    }
}

config.ini file for a developer

[DJANGO]
DEBUG = True

config.ini file for production server

[DATABASE]
engine = django.db.backends.postgresql
name = database
user = postgres_db_user
password = very_strong_password
host = localhost
port = 5432

Usage

smartconfigparser.get

config.get(section, option, default_value)

same as ConfigParser.get() method except that it return default value if section or option does not exists

smartconfigparser.getint

config.getint(section, option, default_value)

same as ConfigParser.getint() method except that it return default value if section or option does not exists

smartconfigparser.getfloat

config.getfloat(section, option, default_value)

same as ConfigParser.getfloat() method except that it return default value if section or option does not exists

smartconfigparser.getboolean

config.getboolean(section, option, default_value)

same as ConfigParser.getboolean() method except that it return default value if section or option does not exists

smartconfigparser.getlist

config.getlist(section, option, default_list)

Return a list of the words in the option value, using comma (‘,’) as the delimiter string

config.ini file

[section]
list = a,b,c

example

print(config.getlist('section', 'list', []))
# ['a', 'b', 'c']
print(config.getlist('section_does_not_exists', 'list', []))
# []

smartconfigparser.set

config.set(section, option, value)

same as ConfigParser.set() method except that it create section if section does not exists

example

from smartconfigparser import Config

config = Config()
config.set('section_does_not_exist', 'user', 'John DOE')
with open('config.ini', 'wt') as configfile:
    config.write(configfile)

config.ini

[section_does_not_exist]
user = John DOE

License

MIT - See LICENSE file

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

SmartConfigParser-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

SmartConfigParser-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file SmartConfigParser-1.0.0.tar.gz.

File metadata

  • Download URL: SmartConfigParser-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for SmartConfigParser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5b45dd2f57fdd9e4441e705c64636c72a5f0e956c2028a77e7c07d79efead887
MD5 1a3089d484542096fcb15ef56e3c4c05
BLAKE2b-256 8b064cda2c2c26e0cdbced075903adf76d0bdce9b5a667aad5e2b6239b7bd890

See more details on using hashes here.

File details

Details for the file SmartConfigParser-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: SmartConfigParser-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for SmartConfigParser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b000a8a48bc99c30980cebf2e207b2fe9613a952d3d8563be90b0b8cd545ab4
MD5 c33e8c38a68fa398dc394d4aa8f560d2
BLAKE2b-256 48b5647dba4170ae2d1af14e3829c4e3aecb4cf5eeba775b3cf5097b6b0244fa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page