Below is a description for merging duplicate checks that are in different directories.
Note: This currently only works with the Python API, so the checks must be merged into a .upy file and not a .upl. You’ll want to place the .upy file into a unique directory.
The following are changes to the .upy script we need to make before deleting the duplicate scripts:
1. Instead of defining a single id, you can define multiple with ‘ids’.
# The ID for the check
def ids():
return ('ID_01', 'ID_02', 'ID_03')
2. When defining the name of each check, you must pass the id as a parameter, and define a name based on that id. The example below uses a Python dictionary to imitate a switch-case scenario with ‘id’.
Note: The name of the check (the value of the dictionary) must include the path from the main Codecheck directory as shown below:
# The short name of the check
def name(id):
return {
'ID_00': 'PATH/TO/DIRECTORY/NAME OF CHECK',
'ID_01': 'SciTools\' Recommended Checks/Variables should be commented',
'ID_02': 'All Checks/Language Specific/C and C++/Variables/Variables should be commented',
'ID_03': 'All Checks/Language Specific/Ada/Variables/Variables should be commented',
}[id]
3. To create a description for each check, the id must also be passed as a parameter into the description() function:
# The short description of the check
def description(id):
return {
'ID_01': "Description of check 01",
'ID_02': "Description of check 02",
'ID_03': "Description of check 03",
}[id]
4. As well as the detailed_description and define_options functions:
# The long description of the check
def detailed_description(id):
return {
'ID_01': """\
detailed description of check 01""",
'ID_02': """\
detailed description of check 02""",
'ID_03': """\
detailed description of check 03""",
}[id]
5. Lastly, if the checks have different core implementations (in the check() function itself), the ID does not need to be passed as a separate parameter, instead it’s accessible as a method of the check object:
def check(check, file):
if check.id() == 'ID_01':
[...]
elif check.id() == 'ID_02':
[...]
else:
[...]