Skip to main content

Creates config file for configparser module to use within the user's home config dir

Project description

Introduction

smashconfig module is a small package intended to cut back rewriting a single function that I use repeatedly in crafting command line interface. That function creates a 'config.ini' within the user's config project's dir ('~/.config//'), which is then read from to reduce hardcoding user specific information, such as IDs, user specific dir paths, etc..

Below is the dir in question:

import os

def assure_config_files(dir_path, pkg_name, config_contents=None):
    """ Builds a user config ini file with the dir_path (if NoneType set to $HOME/.config),
        and the pkg_name for making a dir to put the file inside.

    :dir_path: 
        a string of the absolute path to mkdir for config file 
            if None function uses: `os.environ['HOME'] + '.config/'`
    :pkg_name:
        a string of the name of the dir in your dir_path that will house
        your config file, which is always 'config.ini'
    :config_contents:
        a string that is inserted as the contents of the new config file
    :returns:
        returns absolute path to the config file as a string

    """
    if isinstance(dir_path, type(None)):
        dir_path = os.environ['HOME'] + '/.config/'
    dir_conf = dir_path + pkg_name
    file_conf = dir_conf + '/config.ini'
    if not os.path.exists(dir_conf):
        os.mkdir(dir_conf)
    if not os.path.isfile(file_conf):
        if config_contents:
            with open(file_conf, 'w') as f:
                f.write(config_contents)
        else:
            with open(file_conf, 'w') as f:
                pass
    return file_conf

Useage

You can use the package as follows:

pip install --editable .

In the code:

from core import smashconfig
smashconfig.assure_config_files(None, 'mypackage')

This creates the dir ~/.config/mypackage/ with the file config.ini within it. You can all pass a string to parameter contents to write to the file, however this is optional.

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

smashconfig-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smashconfig-1.0.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: smashconfig-1.0.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for smashconfig-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a94251c7fb00b0a662f57a751480c023061cb0b7477e1ef358ec8b648a19617a
MD5 9ae5b337fb3d858e72b338df50f9eed2
BLAKE2b-256 1f0a94056535f60c23b92e71456c46203b4497113c379e8bcb7ad9adc53012aa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: smashconfig-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for smashconfig-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 012b219a2b8edb0ef3a1ec56828cd93ce41a328734d54e16e7ddaab7a589a6ad
MD5 d7f23b88e86dacaaea35680f5ea46425
BLAKE2b-256 3591be92254e662fb326041f9559e733294d284f078485347c2c6dbd49eb44b9

See more details on using hashes here.

Supported by

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