Skip to main content

Read and write configuration values in a WordPress "wp-config.php" file.

Project description

wp-configr

CircleCI

A Python package for reading and writing configuration values in a WordPress wp-config.php file.

Overview

A default wp-config.php might look like this:

<?php
define( 'DB_NAME', 'database_name_here' );

wpconfigr will help you update the file to:

<?php
define( 'SOMETHING_ENTIRELY_NEW', 'garnet' );
define( 'DB_NAME', 'my_blog' );

wpconfigr exposes two functions:

  • set(key, value) will update a property of name key if it exists, otherwise will create it.
  • get(key) will return the value of the property of name key, or None if it doesn't exist.

Installation

pip install wpconfigr

Command-line usage

Writing values to wp-config.php

Named parameters:

  • --filename: Full path and filename of the wp-config.php file.
  • --key: Name of the property to create/update.
  • --value: String value to set.
  • --set-true: Set the value to boolean true.
  • --set-false: Set the value to boolean false.
  • --log-level: (Optional) Log level.
python -m wpconfigr --filename  /www/wp-config.php \
                    --key       DB_NAME \
                    --value     my_blog \
                    --log-level info

Reading values from wp-config.php

As above, but do not specify a value (via the --value, --set-true or --set-false arguments).

python -m wpconfigr --filename /www/wp-config.php \
                    --key      DB_NAME

Code usage

To update a string holding wp-config.php content:

from wpconfigr import WpConfigString

config = WpConfigString(config_string)
config.set('DB_NAME', 'my_blog')
updated_config_string = config.content

To directly update a wp-config.php file:

from wpconfigr import WpConfigFile

config = WpConfigFile(filename)
config.set('DB_NAME', 'my_blog')
# File is updated immediately after each property update.

To read a property value:

db_name = config.get('DB_NAME')

Development

Prerequisites

wp-configr requires Python 3.x.

Installing dependencies

pip install -e .[dev]

Running tests

python test.py

Changelog

v1.4 - 2018-12-06

  • Fixed bug where commented properties were read and updated.

v1.3 - 2018-12-02

  • Added --set-true and --set-false command-line flags.

v1.2 - 2018-12-02

  • No longer re-writes the configuration file if nothing has changed.

v1.1 - 2018-12-02

  • Added logging.
    • Test runs log everything.
    • Running from the command-line logs only CRITICAL by default, but can be overridden with the new optional --log-level argument.
  • Made the documentation a little clearer, and fixed a typo in a code sample.
  • Replaced internal naming references to FRACTIONAL with FLOAT. The word utterly eluded me for v1.0.
  • Fixed some code formatting and Pylint warnings.
  • Ignore coverage HTML reports in source control.

v1.0 - 2018-12-01

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

wpconfigger-1.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wpconfigger-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for wpconfigger-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ae2b23e22a1b26f6201ffb5edb2bca3b8dd24055286b002454577d0be994d03
MD5 6135df0712a74433605cd9832f08a84c
BLAKE2b-256 c5ba33b71859e0911f91273c5e032a3928cfe3d4eb03a84cf7f2302e49e14765

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