Skip to content

Support running user rule files located outside of the NsiqCppStyle package #40

@mosherubin

Description

@mosherubin

Problem Description

Currently any rule file used in an NsiqCppStyle session (e.g., RULE_3_1_A_do_not_start_filename_with_underbar) must reside in the nsiqcppstyle\rules package folder. You can see this in nsiqcppstyle_runmanager.py:

def LoadRules(self, checkingRuleNames):
    ...
    for ruleName in checkingRuleNames:
        ruleModule = __import__("rules." + ruleName)
        self.loadedRule.append(ruleModule)
    ...

I have written many proprietary rules that I do not want to return to the NsiqCppStyle repository. To include them in my NsiqCppStyle checking runs, I must copy my rules to the rules folder. Should I upgrade NsiqCppStyle to a newer version I may need to copy all of my proprietary rules again. Should I push changes through a GitHub pull request, I have to be careful not to add my proprietary rules to the commit. In general, I want to keep my proprietary rules totally separate from the NsiqCppStyle package.

Proposed Solution

The proposal is to support a new command-line option for specifying one or more additional paths that should be searched for rules. These paths can be anywhere in the file system. For all rule names in the filefilter.txt file, NsiqCppStyle should search first the rules folder, followed by any additional folders. If found, the engine should import the rule file. The following Stack Overflow post provides Python code that can be used to import the rule file from anywhere. Here is a test script I wrote:

d:\junk\import-from-anywhere\main.py

import importlib.util
import sys
spec = importlib.util.spec_from_file_location("test.py", "d:\\junk\\test.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["test.py"] = foo
spec.loader.exec_module(foo)
print("foobar=%d" % (foo.foobar))

d:\junk\test.py

foobar = 5

Running main.py shows the import in action:

D:\Junk\NsiqCppStyle\Import-From-Anywhere>python main.py
foobar=5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions