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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a94251c7fb00b0a662f57a751480c023061cb0b7477e1ef358ec8b648a19617a
|
|
| MD5 |
9ae5b337fb3d858e72b338df50f9eed2
|
|
| BLAKE2b-256 |
1f0a94056535f60c23b92e71456c46203b4497113c379e8bcb7ad9adc53012aa
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
012b219a2b8edb0ef3a1ec56828cd93ce41a328734d54e16e7ddaab7a589a6ad
|
|
| MD5 |
d7f23b88e86dacaaea35680f5ea46425
|
|
| BLAKE2b-256 |
3591be92254e662fb326041f9559e733294d284f078485347c2c6dbd49eb44b9
|