Skip to content

Commit

Permalink
command line tool to extract values from natlink.ini (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
dougransom authored Nov 30, 2024
1 parent 1504756 commit 6708e9e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"
[project]
name = "natlinkcore"
authors = [{name = "Quintijn Hoogenboom (maintainer)", email = "[email protected]"}]
version="5.4.2.dev5"
version="5.4.2.dev6"
dynamic = [ "description"]
requires-python = ">=3.10"
readme = "readme.md"
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions src/natlinkcore/configure/natlink_extract_ini_value.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6708e9e

Please sign in to comment.