kwconfig

Richard Wen

A Python module for managing config files in keyword style json format.

from kwconfig import kwconfig
config = kwconfig('config.json')

Install

  1. Install Python
  2. Install kwconfig via pip
pip install kwconfig

Usage

import kwconfig

# Specify a file path for creating kwconfig object
config = kwconfig.manage('config.json')

For more details on module usage, see the example in kwconfig.manage.

Modules

class kwconfig.manage(file_path, defaults={})[source]

Manage configuration files in keyword style JSON format.

Notes:
Creates a configuration file with the defaults if the file_path does not exist.
Args:
file_path (str):
Path to the configuration file in JSON format.
defaults (dict):
Dictionary of a default keyword dict.
Attributes:
file_path (str):
Path to the configuration file in JSON format.

Examples:

# Import the kwconfig module
import kwconfig

# Specify a file path for creating config object
config = kwconfig.manage('config.json', defaults={'key0': 'value0'})

# Update the config file with a key and value dict
config.update({'key1': 'value1', 'key2': 'value2'})

# Add a keyword dict to existing config file
# If a key exists, it will be updated
# If a key does not exist, it will be added
other_config = {'key3': 'value3'}
other_config = config.add(other_config)

# Write new values using keyword dict
config.overwrite({
  'new_key1': 'new_value1',
  'new_key2': 'new_value2'
})

# Obtain a dict of the config file contents
kw = config.read()

# Remove the key named "key1"
config.remove('key1')

# Reset to defaults
config.reset()

# Send commands to run

# Set default key and value
config.command(['-s', 'new_key1=newvalue1'], i=0)

# Remove default key
config.command(['-r', 'new_key1'], i=0)

# Print defaults
config.command(['-v'], i=0)

# Reset defaults
config.command(['-d'], i=0)
add(other_config)[source]

Add default arguments to another configuration dictionary.

Adds the default arguments from kwconfig.manage.file_path to another configuration dictionary if it is not already set.

Args:
other_config (dict):
The other configuration dictionary of keyword arguments.
Return:
A dict of the combined and updated config file.
command(argv, i=0, doc='', quit=True, silent=False)[source]

Consume a list of arguments and execute commands.

Runs a kwconfig method based on item i in argv:

  • ['-h'] or []: show doc text
  • ['-s', '<arg>=<value>']: set default optional arguments
  • ['-r', '<arg>']: remove default arguments
  • ['-v']: view default arguments
  • ['-d']: reset default arguments
Args:
argv (listof str):
A list of str arguments to execute commands (e.g. from the command line).
doc (str):
String containing the help documentation to print out on the command line interface when using -h or not entering any commands or arguments.
quit (bool):
Whether to quit if a command is executed (if a command is not found, it will not exit).
silent (bool):
Whether to suppress messages (True) or allow them (False).
overwrite(new_config)[source]

Overwrite default arguments for configuration file.

Overwrites the contents of kwconfig.manage.file_path with new_config.

Args:
new_config (dict):
The new default arguments as a keyword dictionary.
read()[source]

Read default arguments from configuration file.

Reads the configuration file content into a keyword dictionary.

Returns:
A dict of the default arguments from kwconfig.manage.file_path.
remove(k)[source]

Remove keys from configuration file.

Removes a key from kwconfig.manage.file_path.

Args:
k (str):
The key to remove from kwconfig.manage.file_path.
reset()[source]

Reset keyword values for configuration file.

Resets the contents of kwconfig.manage.file_path with kwconfig.manage.defaults.

update(arguments)[source]

Update keyword values for configuration file.

Updates the contents of kwconfig.manage.file_path with arguments.

Args:
arguments (dict):
Keyword dict to use as an update.
kwconfig.parse(argv)[source]

Parse optional list of keyword arguments into a dict.

Parses a list of keyword arguments defined by a leading -- and separated by = (for example, –key=value).

Args:
argv (listof str):
Keyword dict to use as an update.

Examples:

# Import the kwconfig module
import kwconfig

# Create a sample list of keyword arguments
argv = ['--key1=value1', '--key2=value2']

# Parse into a keyword dict
kwdict = kwconfig.parse(argv)

# View the values of key1 and key2
print('key1: ' + kwdict['key1'])
print('key2: ' + kwdict['key2'])