Skip to content

Commit b88cda0

Browse files
committed
ENH: Use a python script to generate the skeleton code for a new filter
1 parent 26f0cb2 commit b88cda0

8 files changed

Lines changed: 536 additions & 94 deletions

scripts/filter.cpp.in

Lines changed: 0 additions & 70 deletions
This file was deleted.

scripts/make_filter.py

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,130 @@
33

44
from pathlib import Path
55

6-
def make_filter(output_dir: Path, name: str, template_dir: Path) -> None:
7-
header_template_file = f'{template_dir}/filter.hpp.in'
6+
def make_filter(plugin_dir: Path, name: str, template_dir: Path) -> None:
7+
8+
plugin_name = plugin_dir.name
9+
filter_name = f'{name}Filter'
10+
header_template_file = f'{template_dir}/simplnx_filter.hpp.in'
811
header_file_contents: str
912
with open(header_template_file, 'r') as header_file:
10-
header_file_contents = header_file.read().replace('@FILTER_NAME@', name).replace('@UUID@', str(uuid.uuid4()))
13+
header_file_contents = header_file.read().replace('@FILTER_NAME@', filter_name).replace('@UUID@', str(uuid.uuid4()))
14+
header_file_contents = header_file_contents.replace('@PLUGIN_NAME_UPPER@', plugin_name.upper())
15+
header_file_contents = header_file_contents.replace('@PLUGIN_NAME@', plugin_name)
16+
header_file_contents = header_file_contents.replace('@PARAMETER_KEYS@', " // THESE NEED TO BE GENERATED\n")
1117

1218
# write contents out to the new target header file
13-
header_target_file = f'{output_dir}/{name}.hpp'
19+
header_target_file = f'{plugin_dir}/src/{plugin_name}/Filters/{name}Filter.hpp'
1420
print(f'Writing Header file: {header_target_file}')
1521
with open(header_target_file, 'w') as header_file:
1622
header_file.write(header_file_contents)
1723

18-
cpp_template_file = f'{template_dir}/filter.cpp.in'
24+
cpp_template_file = f'{template_dir}/simplnx_filter.cpp.in'
1925
cpp_file_contents: str
2026
with open(cpp_template_file, 'r') as cpp_file:
21-
cpp_file_contents = cpp_file.read().replace('@FILTER_NAME@', name)
27+
cpp_file_contents = cpp_file.read().replace('@ALGORITHM_NAME@', name)
28+
cpp_file_contents = cpp_file_contents.replace('@FILTER_NAME@', filter_name)
29+
cpp_file_contents = cpp_file_contents.replace('@PLUGIN_NAME@', plugin_name)
30+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_KEYS@', " //TODO: THESE NEED TO BE GENERATED\n")
31+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_INCLUDES@', "\n //TODO: PARAMETER_INCLUDES")
32+
cpp_file_contents = cpp_file_contents.replace('@DEFAULT_TAGS@', "\"\"")
33+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_DEFS@', "\n //TODO: PARAMETER_DEFS")
34+
cpp_file_contents = cpp_file_contents.replace('@PREFLIGHT_DEFS@', "\n //TODO: PREFLIGHT_DEFS")
35+
cpp_file_contents = cpp_file_contents.replace('@PROPOSED_ACTIONS@', "\n //TODO: PROPOSED_ACTIONS")
36+
cpp_file_contents = cpp_file_contents.replace('@PREFLIGHT_UPDATED_DEFS@', "\n //TODO: PREFLIGHT_UPDATED_DEFS")
37+
cpp_file_contents = cpp_file_contents.replace('@PREFLIGHT_UPDATED_VALUES@', "\n //TODO: PREFLIGHT_UPDATED_VALUES")
38+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_JSON_CONSTANTS@', "\n //TODO: PARAMETER_JSON_CONSTANTS")
39+
cpp_file_contents = cpp_file_contents.replace('@INPUT_VALUES_DEF@', "\n //TODO: INPUT_VALUES_DEF")
40+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_JSON_CONVERSION@', "/* This is a NEW filter and not ported so this section does not matter */")
41+
42+
# write contents out to the new target CPP file
43+
cpp_target_file = f'{plugin_dir}/src/{plugin_name}/Filters/{name}Filter.cpp'
44+
print(f'Writing CPP file: {cpp_target_file}')
45+
with open(cpp_target_file, 'w') as cpp_file:
46+
cpp_file.write(cpp_file_contents)
47+
48+
# ***************************************************************************
49+
# Algorithm File Generation
50+
# ***************************************************************************
51+
header_template_file = f'{template_dir}/simplnx_algorithm.hpp.in'
52+
header_file_contents: str
53+
with open(header_template_file, 'r') as header_file:
54+
header_file_contents = header_file.read().replace('@FILTER_NAME@', name).replace('@UUID@', str(uuid.uuid4()))
55+
header_file_contents = header_file_contents.replace('@PLUGIN_NAME_UPPER@', plugin_name.upper())
56+
header_file_contents = header_file_contents.replace('@PLUGIN_NAME@', plugin_name)
57+
header_file_contents = header_file_contents.replace('@PARAMETER_KEYS@', " // THESE NEED TO BE GENERATED\n")
58+
header_file_contents = header_file_contents.replace('@PARAMETER_INCLUDES@', "\n//TODO: PARAMETER_INCLUDES")
59+
header_file_contents = header_file_contents.replace('@INPUT_VALUE_STRUCT_DEF@', "//TODO: INPUT_VALUE_STRUCT_DEF\n")
2260

2361
# write contents out to the new target header file
24-
cpp_target_file = f'{output_dir}/{name}.cpp'
62+
header_target_file = f'{plugin_dir}/src/{plugin_name}/Filters/Algorithms/{name}.hpp'
63+
print(f'Writing Header file: {header_target_file}')
64+
with open(header_target_file, 'w') as header_file:
65+
header_file.write(header_file_contents)
66+
67+
cpp_template_file = f'{template_dir}/simplnx_algorithm.cpp.in'
68+
cpp_file_contents: str
69+
with open(cpp_template_file, 'r') as cpp_file:
70+
cpp_file_contents = cpp_file.read().replace('@FILTER_NAME@', name)
71+
72+
# write contents out to the new target CPP file
73+
cpp_target_file = f'{plugin_dir}/src/{plugin_name}/Filters/Algorithms/{name}.cpp'
2574
print(f'Writing CPP file: {cpp_target_file}')
2675
with open(cpp_target_file, 'w') as cpp_file:
2776
cpp_file.write(cpp_file_contents)
2877

78+
# ***************************************************************************
79+
# Unit test File Generation
80+
# ***************************************************************************
81+
cpp_template_file = f'{template_dir}/simplnx_unit_test.cpp.in'
82+
cpp_file_contents: str
83+
with open(cpp_template_file, 'r') as cpp_file:
84+
cpp_file_contents = cpp_file.read().replace('@FILTER_NAME@', filter_name)
85+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_INCLUDES@', "\n //TODO: PARAMETER_INCLUDES")
86+
cpp_file_contents = cpp_file_contents.replace('@PLUGIN_NAME@', plugin_name)
87+
cpp_file_contents = cpp_file_contents.replace('@PARAMETER_DEFS@', "\n //TODO: PARAMETER_DEFS")
88+
89+
# write contents out to the new target CPP file
90+
cpp_target_file = f'{plugin_dir}/test/{name}Test.cpp'
91+
print(f'Writing Doc file: {cpp_target_file}')
92+
with open(cpp_target_file, 'w') as cpp_file:
93+
cpp_file.write(cpp_file_contents)
94+
95+
96+
# ***************************************************************************
97+
# Documentation File Generation
98+
# ***************************************************************************
99+
cpp_template_file = f'{template_dir}/simplnx_docs.md.in'
100+
cpp_file_contents: str
101+
with open(cpp_template_file, 'r') as cpp_file:
102+
cpp_file_contents = cpp_file.read().replace('@FILTER_NAME@', name)
103+
104+
# write contents out to the new target CPP file
105+
cpp_target_file = f'{plugin_dir}/docs/{name}Filter.md'
106+
print(f'Writing Doc file: {cpp_target_file}')
107+
with open(cpp_target_file, 'w') as cpp_file:
108+
cpp_file.write(cpp_file_contents)
109+
110+
111+
29112
print(f'===================================================================')
30-
print(f'Do NOT forget to add your filter to the appropriate CMake Files')
113+
print(f'Do NOT forget to add your filter and algorithm to the CMakeLists.txt file at:')
114+
print(f'{plugin_dir}/CMakeLists.txt')
115+
print(f'Update the unit test CMakeLists.txt file at:')
116+
print(f'{plugin_dir}/test/CMakeLists.txt')
117+
print(f'Do NOT forget to update the documentation file when you are done writing the file')
31118
print(f'===================================================================')
32119

33120
def main() -> None:
34121
parser = argparse.ArgumentParser(description='Creates simplnx filter header and implementation skeleton codes')
35-
parser.add_argument('-o', '--output_dir', type=Path, help='Input directory where files will be created')
122+
parser.add_argument('-o', '--plugin_dir', type=Path, help='Plugin Directory to create the filter')
36123
parser.add_argument('-n', '--name', type=str, help='Name of filter')
37124
parser.add_argument('-t', '--template_dir', type=Path, help='Location of template files')
38125

39126
args = parser.parse_args()
40127

41128
print('args:')
42-
print(f' output_dir = \"{args.output_dir}\"')
129+
print(f' plugin_dir = \"{args.plugin_dir}\"')
43130
print(f' name = \"{args.name}\"')
44131
print(f' template_dir = \"{args.template_dir}\"')
45132
print('')
@@ -48,7 +135,12 @@ def main() -> None:
48135
print(f' THIS WILL OVERWRITE ANY EXISTING FILE')
49136
print(f'===================================================================')
50137

51-
make_filter(args.output_dir, args.name, args.template_dir)
138+
make_filter(args.plugin_dir, args.name, args.template_dir)
52139

53140
if __name__ == '__main__':
54141
main()
142+
143+
# -----------------------------------------------------------------------------
144+
# Example invocation
145+
# python make_filter.py --plugin_dir /Users/mjackson/Workspace1/simplnx/src/Plugins/SimplnxCore --name "ReadNotesFile" --template_dir /Users/mjackson/Workspace1/simplnx/scripts
146+
# -----------------------------------------------------------------------------

scripts/simplnx_algorithm.cpp.in

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "@FILTER_NAME@.hpp"
2+
3+
#include "simplnx/DataStructure/DataArray.hpp"
4+
#include "simplnx/DataStructure/DataGroup.hpp"
5+
6+
using namespace nx::core;
7+
8+
// -----------------------------------------------------------------------------
9+
@FILTER_NAME@::@FILTER_NAME@(DataStructure& dataStructure, const IFilter::MessageHandler& mesgHandler, const std::atomic_bool& shouldCancel, @FILTER_NAME@InputValues* inputValues)
10+
: m_DataStructure(dataStructure)
11+
, m_InputValues(inputValues)
12+
, m_ShouldCancel(shouldCancel)
13+
, m_MessageHandler(mesgHandler)
14+
{
15+
}
16+
17+
// -----------------------------------------------------------------------------
18+
@FILTER_NAME@::~@FILTER_NAME@() noexcept = default;
19+
20+
// -----------------------------------------------------------------------------
21+
Result<> @FILTER_NAME@::operator()()
22+
{
23+
/**
24+
* This section of the code should contain the actual algorithmic codes that
25+
* will accomplish the goal of the file.
26+
*
27+
* If you can parallelize the code there are a number of examples on how to do that.
28+
* GenerateIPFColors is one example
29+
*
30+
* If you need to determine what kind of array you have (Int32Array, Float32Array, etc)
31+
* look to the ExecuteDataFunction() in simplnx/Utilities/FilterUtilities.hpp template
32+
* function to help with that code.
33+
* An Example algorithm class is `CombineAttributeArrays` and `RemoveFlaggedVertices`
34+
*
35+
* There are other utility classes that can help alleviate the amount of code that needs
36+
* to be written.
37+
*
38+
* REMOVE THIS COMMENT BLOCK WHEN YOU ARE FINISHED WITH THE FILTER_HUMAN_NAME
39+
*/
40+
41+
return {};
42+
}

scripts/simplnx_algorithm.hpp.in

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include "@PLUGIN_NAME@/@PLUGIN_NAME@_export.hpp"
4+
5+
#include "simplnx/DataStructure/DataPath.hpp"
6+
#include "simplnx/DataStructure/DataStructure.hpp"
7+
#include "simplnx/Filter/IFilter.hpp"
8+
@PARAMETER_INCLUDES@
9+
10+
/**
11+
* This is example code to put in the Execute Method of the filter.
12+
@EXECUTE_EXAMPLE_CODE@
13+
*/
14+
15+
namespace nx::core
16+
{
17+
18+
struct @PLUGIN_NAME_UPPER@_EXPORT @FILTER_NAME@InputValues
19+
{
20+
@INPUT_VALUE_STRUCT_DEF@
21+
};
22+
23+
/**
24+
* @class @FILTER_NAME@
25+
* @brief This algorithm implements support code for the @FILTER_NAME@Filter
26+
*/
27+
28+
class @PLUGIN_NAME_UPPER@_EXPORT @FILTER_NAME@
29+
{
30+
public:
31+
@FILTER_NAME@(DataStructure& dataStructure, const IFilter::MessageHandler& mesgHandler, const std::atomic_bool& shouldCancel, @FILTER_NAME@InputValues* inputValues);
32+
~@FILTER_NAME@() noexcept;
33+
34+
@FILTER_NAME@(const @FILTER_NAME@&) = delete;
35+
@FILTER_NAME@(@FILTER_NAME@&&) noexcept = delete;
36+
@FILTER_NAME@& operator=(const @FILTER_NAME@&) = delete;
37+
@FILTER_NAME@& operator=(@FILTER_NAME@&&) noexcept = delete;
38+
39+
Result<> operator()();
40+
41+
private:
42+
DataStructure& m_DataStructure;
43+
const @FILTER_NAME@InputValues* m_InputValues = nullptr;
44+
const std::atomic_bool& m_ShouldCancel;
45+
const IFilter::MessageHandler& m_MessageHandler;
46+
};
47+
48+
} // namespace complex

scripts/simplnx_docs.md.in

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# INSERT_HUMAN_NAME
2+
3+
## Group (Subgroup)
4+
5+
What group (and possibly subgroup) does the filter belong to
6+
7+
## Description
8+
9+
This **Filter** .....
10+
11+
Images can be used with this:
12+
13+
![](Images/@FILTER_NAME@_1.png)
14+
15+
## Warning
16+
17+
## Notes
18+
19+
## Caveats
20+
21+
% Auto generated parameter table will be inserted here
22+
23+
## Reference
24+
25+
26+
## Example Pipelines
27+
28+
29+
## License & Copyright
30+
31+
Please see the description file distributed with this plugin.
32+
33+
## DREAM3D Mailing Lists
34+
35+
If you need help, need to file a bug report or want to request a new feature, please head over to the [DREAM3DNX-Issues](https://github.com/BlueQuartzSoftware/DREAM3DNX-Issues/discussions) GitHub site where the community of DREAM3D-NX users can help answer your questions.

0 commit comments

Comments
 (0)