Skip to content

Commit

Permalink
[WIP] Add config file and scripts to set configs from CLI
Browse files Browse the repository at this point in the history
WIP: while the config file can be generated and updated, the scripts do
not access values from it, yet.

Credits to @jung-benjamin for coming up with the idea of a config file,
thanks!
  • Loading branch information
maxschalz committed Aug 3, 2021
1 parent 0e3e461 commit d61afea
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ eggs/
.eggs/
*.egg-info/
*.egg

config.ini
1 change: 1 addition & 0 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .init_python import init_python
from .init_latex import init_latex
from .config import cmdline_config

__author__ = "Max Schalz"
__copyright__ = "Copyright 2021, Max Schalz"
Expand Down
71 changes: 71 additions & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import configparser
import os

CONFIG_FNAME = "config.ini"
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"..")

def argparser():
"""Argument parser to obtain config updates from the commandline."""
default_config = generate_default_config()

description = "Update the default values in the configuration file."
description += " Allowed sections and options are:\n"
for section in default_config.sections():
description += f" {section}\n"
for option in default_config.options(section):
description += f" {option}\n"
parser = argparse.ArgumentParser(
description=description,
formatter_class=argparse.RawTextHelpFormatter)

section = "Config section, e.g., 'global'"
parser.add_argument("section", metavar="section", type=str,
help=section)
option = "Config option, e.g., 'author'"
parser.add_argument("option", metavar="option", type=str,
help=option)
value = "Config value, e.g., 'Jane Doe'"
parser.add_argument("value", metavar="value", type=str, help=value)

return parser.parse_args()

def cmdline_config():
args = argparser()
update_config(args.section, args.option, args.value)

def generate_default_config():
"""Generate a default config parser."""
config = configparser.ConfigParser()

config["global"] = {"author": "Jane Doe"}
config["python"] = {"license": "BSD-3-Clause"}
config["latex"] = {"title": "Document"}

return config

def update_config(section, option, value):
"""Update an already existing config file."""
if not os.path.isfile(os.path.join(CONFIG_PATH, CONFIG_FNAME)):
generate_default_config()

config = configparser.ConfigParser()
if os.path.isfile(os.path.join(CONFIG_PATH, CONFIG_FNAME)):
with open(os.path.join(CONFIG_PATH, CONFIG_FNAME), "r") as f:
config.read_file(f)
else:
config = generate_default_config()

if not config.has_section(section):
raise TypeError(f"Invalid section: {section}")
if not config.has_option(section, option):
raise TypeError(f"Invalid option: {option}")
config.set(section, option, value)

with open(os.path.join(CONFIG_PATH, CONFIG_FNAME), "w") as f:
config.write(f)

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def main():
"Programming Language :: Python :: 3"],
install_requires=None,
entry_points={"console_scripts": ["initpython = scripts:init_python",
"initlatex = scripts:init_latex"]},
"initlatex = scripts:init_latex",
"initconfig = scripts:cmdline_config"]},
)

if __name__=="__main__":
Expand Down

0 comments on commit d61afea

Please sign in to comment.