diff --git a/pyproject.toml b/pyproject.toml index 3b8178e..9fda2a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi" [project] name = "natlinkcore" authors = [{name = "Quintijn Hoogenboom (maintainer)", email = "q.hoogenboom@antenna.nl"}] -version="5.4.2.dev5" +version="5.4.2.dev6" dynamic = [ "description"] requires-python = ">=3.10" readme = "readme.md" @@ -54,6 +54,7 @@ testpaths= [ [project.scripts] natlinkconfig_cli = "natlinkcore.configure.natlinkconfig_cli:main_cli" natlink_extensions = "natlinkcore.configure.natlink_extensions:main" +natlink_extract_ini_value = "natlinkcore.configure.natlink_extract_ini_value:main" [project.gui-scripts] natlinkconfig_gui = "natlinkcore.configure.natlinkconfig_gui:main_gui" diff --git a/readme.md b/readme.md index a1d3b6c..1b61747 100644 --- a/readme.md +++ b/readme.md @@ -11,6 +11,14 @@ You can install from [The Python Package Index (PyPI)](https://pypi.org/) with Note that natlinkcore will not install if you have not installed Natlink first. Natlink is installed through running an installer. +This will install utilties you can run from a shell prompt: natlinkconfig_cli, natlinkconfig_gui, and natlink_extract_ini_value. + +You can use natlink_extract_ini_value to grab a directory or setting out of natlink.ini for copying and pasting or a shell script. +``` +PS C:\Users\doug\code\dt\natlinkcore> natlink_extract_ini_value -s directories -k dragonflyuserdirectory +C:\Users\doug\Documents +``` + ## Test Framework Tests use the [pytest](https://docs.pytest.org/) framework. diff --git a/src/natlinkcore/configure/natlink_extract_ini_value.py b/src/natlinkcore/configure/natlink_extract_ini_value.py new file mode 100644 index 0000000..da3a333 --- /dev/null +++ b/src/natlinkcore/configure/natlink_extract_ini_value.py @@ -0,0 +1,50 @@ +import argparse +import sys +from natlinkcore.configure.natlinkconfigfunctions import NatlinkConfig +import configparser +from pathlib import Path +import configparser + +def get_config_value(config, section, key): + if not config.has_section(section): + raise configparser.NoSectionError(section) + if not config.has_option(section, key): + raise configparser.NoOptionError(key, section) + return config.get(section, key) + +def main(): + parser = argparse.ArgumentParser(description= + f"""prints the value for the key in the section of natlink.ini to standard out. You can also ask for the full path to the ini file. + for example, to get the dragonflyuserdirectory key in the directories section: + {sys.argv[0]} --section 'directories' --key dragonflyuserdirectory + """) + parser.add_argument('-i', "--ini_file",action='store_true', help='print the full path to the natlink.ini file') + + + + parser.add_argument('-s', '--section', type=str, help='Section name') + parser.add_argument('-k', '--key', type=str, help='Key name') + + args = parser.parse_args() + + + try: + config=NatlinkConfig() + config_path=config.natlinkconfig_path + config_file=Path(config_path)/"natlink.ini" + if args.ini_file: + print(config_file) + exit(0) + if not (args.key and args.section): + raise Exception("Must supply key and section or -i/--ini_file") + config=configparser.ConfigParser() + config.read(config_file) + v=get_config_value(config,args.section,args.key) + print(f"{v}") + except Exception as e: + print(e) + exit(-1) + + +if __name__ == "__main__": + main()