Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit cdef325

Browse files
author
William McLendon
committed
Cleaning up examples
1 parent 12f9753 commit cdef325

File tree

9 files changed

+133
-94
lines changed

9 files changed

+133
-94
lines changed

.github/workflows/test-driver-core.yml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ jobs:
4646
source ${{ env.venv-name }}/bin/activate
4747
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
4848
cd source
49-
# python3 -m pip install wheel configparserenhanced @ git+https://github.com/sandialabs/ConfigParserEnhanced.git@master#egg=configparserenhanced
5049
python3 -m pip install wheel -r requirements.txt -r requirements-test.txt -r doc/requirements.txt
5150
#ls -ltrhF
5251
@@ -68,18 +67,39 @@ jobs:
6867
with:
6968
path: source
7069

71-
- name: Execute
70+
- name: Execute Unit Tests
7271
run: |
7372
source ${{ env.venv-name }}/bin/activate
73+
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
7474
cd source
7575
python3 -m pytest --version
76-
#ls -ltrhF
77-
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
7876
python3 -m pytest --cov=setprogramoptions --cov-report=term --cov-report=html:${{ github.workspace }}/htmlcov --cov-config=.coveragerc
79-
echo "htmlcov"
8077
ls -ltrhF ${{ github.workspace }}/htmlcov
8178
82-
- name: Save Artifacts
79+
- name: Test Installation
80+
run: |
81+
source ${{ env.venv-name }}/bin/activate
82+
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
83+
cd source
84+
python3 -m pip install .
85+
86+
- name: Test Examples
87+
run: |
88+
source ${{ env.venv-name }}/bin/activate
89+
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
90+
cd source/examples
91+
python3 ./example-01.py
92+
python3 ./example-02.py
93+
python3 ./example-03.py
94+
95+
- name: Test Uninstaller
96+
run: |
97+
source ${{ env.venv-name }}/bin/activate
98+
echo "VIRTUAL_ENV = ${VIRTUAL_ENV}"
99+
cd source
100+
python3 -m pip uninstall -y setprogramoptions
101+
102+
- name: Save Coverage Artifacts
83103
uses: actions/upload-artifact@v2
84104
with:
85105
name: coverage

examples/example-01.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--------------------------------------------------------------------------------
2+
- example-01.py
3+
--------------------------------------------------------------------------------
4+
ls -l -t -r --time-style="+%Y-%m-%d %H:%M:%S"
5+
Done

examples/example-01.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env python3
22
# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
3-
3+
from pathlib import Path
44
import setprogramoptions
55

6+
print(80*"-")
7+
print(f"- {Path(__file__).name}")
8+
print(80*"-")
69

710
filename = "example-01.ini"
811
popts = setprogramoptions.SetProgramOptions(filename)

examples/example-02.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ opt-set -G : Ninja
1111
opt-set -G : "Unix Makefiles"
1212

1313
[MYPROJ_OPTIONS]
14-
opt-set-cmake-var MYPROJ_CXX_FLAGS : "-O0 -fopenmp"
15-
opt-set-cmake-var MYPROJ_ENABLE_OPTION_A BOOL FORCE : ON
16-
opt-set-cmake-var MYPROJ_ENABLE_OPTION_B BOOL PARENT_SCOPE : ON
14+
opt-set-cmake-var MYPROJ_CXX_FLAGS STRING : "-O0 -fopenmp"
15+
opt-set-cmake-var MYPROJ_ENABLE_OPTION_A BOOL FORCE : ON
16+
opt-set-cmake-var MYPROJ_ENABLE_OPTION_B BOOL : ON
1717

1818
[MYPROJ_SOURCE_DIR]
1919
opt-set /path/to/source/dir

examples/example-02.log

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--------------------------------------------------------------------------------
2+
- example-02.py
3+
--------------------------------------------------------------------------------
4+
5+
Bash output
6+
-----------
7+
cmake \
8+
-G=Ninja \
9+
-DMYPROJ_CXX_FLAGS:STRING="-O0 -fopenmp" \
10+
-DMYPROJ_ENABLE_OPTION_A:BOOL=ON \
11+
-DMYPROJ_ENABLE_OPTION_B:BOOL=ON \
12+
/path/to/source/dir
13+
14+
CMake fragment output
15+
---------------------
16+
set(MYPROJ_CXX_FLAGS "-O0 -fopenmp" CACHE STRING "from .ini configuration")
17+
set(MYPROJ_ENABLE_OPTION_A ON CACHE BOOL "from .ini configuration" FORCE)
18+
set(MYPROJ_ENABLE_OPTION_B ON CACHE BOOL "from .ini configuration")
19+
20+
Done

examples/example-02.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env python3
22
# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
3-
3+
from pathlib import Path
44
import setprogramoptions
55

6+
print(80*"-")
7+
print(f"- {Path(__file__).name}")
8+
print(80*"-")
9+
610

711
filename = "example-02.ini"
812
popts = setprogramoptions.SetProgramOptionsCMake(filename)

examples/example-03.ini

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,5 @@ opt-set-cmake-var CMAKE_CXX_FLAGS STRING : "${LDFLAGS|ENV} -foo"
1111
[TEST_VAR_EXPANSION_UPDATE_01]
1212
opt-set cmake
1313
use TEST_VAR_EXPANSION_COMMON
14+
# This will be skipped by the BASH generator without a FORCE option added
1415
opt-set-cmake-var CMAKE_CXX_FLAGS STRING: "${CMAKE_CXX_FLAGS|CMAKE} -bar"
15-
16-
17-
[TEST_VAR_EXPANSION_UPDATE_02]
18-
use TEST_VAR_EXPANSION_UPDATE_01
19-
opt-set-cmake-var CMAKE_F90_FLAGS STRING: "${CMAKE_F90_FLAGS|CMAKE} -baz"
20-
21-
22-
[TEST_VAR_EXPANSION_UPDATE_03]
23-
use TEST_VAR_EXPANSION_UPDATE_01
24-
opt-set-cmake-var CMAKE_CXX_FLAGS STRING: "${CMAKE_CXX_FLAGS|CMAKE} -bif"

examples/example-03.log

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--------------------------------------------------------------------------------
2+
- example-03.py
3+
--------------------------------------------------------------------------------
4+
filename: example-03.ini
5+
section_name = TEST_VAR_EXPANSION_UPDATE_01
6+
7+
parser.configparserenhanceddata[TEST_VAR_EXPANSION_UPDATE_01]
8+
-------------------------------------------------------------
9+
{}
10+
11+
Show parser.options
12+
-------------------
13+
{'TEST_VAR_EXPANSION_UPDATE_01': [{'type': ['opt_set'], 'value': None, 'params': ['cmake']},
14+
{'type': ['opt_set_cmake_var'], 'value': '${LDFLAGS|ENV} -foo', 'params': ['CMAKE_CXX_FLAGS', 'STRING']},
15+
{'type': ['opt_set_cmake_var'], 'value': '${CMAKE_CXX_FLAGS|CMAKE} -bar', 'params': ['CMAKE_CXX_FLAGS', 'STRING']}]}
16+
17+
Bash Output
18+
-----------
19+
Note: The _second_ assignment to `CMAKE_CXX_FLAGS` is skipped by a BASH generator
20+
without a `FORCE` option since by definition all CMake `-D` options on a
21+
BASH command line are both CACHE and FORCE. Within a CMake source fragment
22+
changing an existing CACHE var requires a FORCE option to be set so we should
23+
skip the second assignment to maintain consistency between the bash and cmake
24+
fragment generators with respect to the CMakeCache.txt file that would be
25+
generated.
26+
The `WARNING` message below is terse since it's in compact form -- disable
27+
the `exception_control_compact_warnings` flag to get the full warning message.
28+
29+
!! EXCEPTION SKIPPED (WARNING : ValueError) @ File "/Users/wcmclen/Library/Python/3.9/lib/python/site-packages/setprogramoptions/SetProgramOptionsCMake.py", line 294, in _program_option_handler_opt_set_cmake_var_bash
30+
31+
cmake \
32+
-DCMAKE_CXX_FLAGS:STRING="${LDFLAGS} -foo"
33+
34+
CMake Fragment
35+
--------------
36+
set(CMAKE_CXX_FLAGS "$ENV{LDFLAGS} -foo" CACHE STRING "from .ini configuration")
37+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -bar" CACHE STRING "from .ini configuration")
38+
39+
Done.

examples/example-03.py

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,49 @@
11
#!/usr/bin/env python3
22
# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
3-
4-
import os
3+
from pathlib import Path
54
from pprint import pprint
6-
75
import setprogramoptions
86

9-
10-
11-
def find_config_ini(filename="config.ini", rootpath="."):
12-
"""
13-
Recursively searches for a particular file among the subdirectory structure.
14-
If we find it, then we return the full relative path to `pwd` to that file.
15-
16-
The _first_ match will be returned.
17-
18-
Args:
19-
filename (str): The _filename_ of the file we're searching for. Default: 'config.ini'
20-
rootpath (str): The _root_ path where we will begin our search from. Default: '.'
21-
22-
Returns:
23-
String containing the path to the file if it was found. If a matching filename is not
24-
found then `None` is returned.
25-
26-
"""
27-
output = None
28-
for dirpath, dirnames, filename_list in os.walk(rootpath):
29-
if filename in filename_list:
30-
output = os.path.join(dirpath, filename)
31-
break
32-
if output is None:
33-
raise FileNotFoundError("Unable to find {} in {}".format(filename, os.getcwd())) # pragma: no cover
34-
return output
35-
36-
7+
def print_separator(label):
8+
print("")
9+
print(f"{label}")
10+
print("-"*len(label))
11+
return
3712

3813
def test_setprogramoptions(filename="config.ini"):
39-
print("filename : {}".format(filename))
40-
print("")
14+
print(f"filename: {filename}")
15+
16+
section_name = "TEST_VAR_EXPANSION_UPDATE_01"
17+
print(f"section_name = {section_name}")
4118

4219
parser = setprogramoptions.SetProgramOptionsCMake(filename=filename)
4320
parser.debug_level = 0
4421
parser.exception_control_level = 4
4522
parser.exception_control_compact_warnings = True
4623

47-
section_name = "TEST_VAR_EXPANSION_UPDATE_01"
48-
49-
parse_section(parser, section_name)
24+
data = parser.configparserenhanceddata[section_name]
25+
print_separator(f"parser.configparserenhanceddata[{section_name}]")
26+
pprint(data, width=120)
5027

51-
print("")
52-
print("parser.options")
53-
print("--------------")
28+
print_separator("Show parser.options")
5429
pprint(parser.options, width=200, sort_dicts=False)
5530

31+
print_separator("Bash Output")
32+
print("Note: The _second_ assignment to `CMAKE_CXX_FLAGS` is skipped by a BASH generator")
33+
print(" without a `FORCE` option since by definition all CMake `-D` options on a ")
34+
print(" BASH command line are both CACHE and FORCE. Within a CMake source fragment")
35+
print(" changing an existing CACHE var requires a FORCE option to be set so we should")
36+
print(" skip the second assignment to maintain consistency between the bash and cmake")
37+
print(" fragment generators with respect to the CMakeCache.txt file that would be")
38+
print(" generated.")
39+
print(" The `WARNING` message below is terse since it's in compact form -- disable")
40+
print(" the `exception_control_compact_warnings` flag to get the full warning message.")
5641
print("")
57-
print("Bash Output")
58-
print("-----------")
5942
option_list = parser.gen_option_list(section_name, generator="bash")
43+
print("")
6044
print(" \\\n ".join(option_list))
6145

62-
print("")
63-
print("CMake Fragment")
64-
print("--------------")
46+
print_separator("CMake Fragment")
6547
option_list = parser.gen_option_list(section_name, generator="cmake_fragment")
6648
if len(option_list) > 0:
6749
print("\n".join(option_list))
@@ -72,42 +54,17 @@ def test_setprogramoptions(filename="config.ini"):
7254
return 0
7355

7456

75-
76-
def parse_section(parser, section):
77-
78-
data = parser.configparserenhanceddata[section]
79-
80-
print("\nData")
81-
print("----")
82-
pprint(data, width=120)
83-
84-
# Print the loginfo from the last search (change if to True to enable)
85-
if (False):
86-
print("\nLogInfo")
87-
print("-------")
88-
#parser._loginfo_print(pretty=True)
89-
90-
# Filter out just the entry and exits for handlers
91-
handler_list = [
92-
(d['type'], d['name']) for d in parser._loginfo if d['type'] in ['handler-entry', 'handler-exit']
93-
]
94-
pprint(handler_list, width=120)
95-
96-
return data
97-
98-
99-
10057
def main():
10158
"""
10259
main app
10360
"""
104-
fname_ini = "example-03.ini"
105-
fpath_ini = find_config_ini(filename=fname_ini)
106-
test_setprogramoptions(filename=fpath_ini)
61+
print(80*"-")
62+
print(f"- {Path(__file__).name}")
63+
print(80*"-")
64+
test_setprogramoptions(filename="example-03.ini")
10765
return 0
10866

10967

110-
11168
if __name__ == "__main__":
11269
main()
11370
print("Done.")

0 commit comments

Comments
 (0)