An analyzer of code cyclomatic complexity, which only works with Python 3.
A list of supported languages:
- Python
- Golang
pip3 install git+https://github.com/usharerose/janus.git
Usage: janus [OPTIONS] [PATHS]...
Args: paths (tuple): the paths of files or directories
Options:
--help Show this message and exit.
You can input a file as target
janus foo.py
You can input directory, and analyze the files under it recursively
janus directory/
You can also input multiple paths
janus directory/ foo.py
>>> from janus.app import Janus
>>> app = Janus()
>>> result = app.process(('/foo.py',))
>>> print(result)
{"data": [{"file": "/foo.py", "cyclomatic_complexity": 27}]}
According to the definition of cyclomatic complexity
in Wikipedia
Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J. McCabe, Sr. in 1976.
The library is based on the following formula
V(G) = P + 1
* P is the number of decision points in the program
- Add the language-specific
tree-sitter
as submodule
[submodule "vendor/tree-sitter-go"]
path = vendor/tree-sitter-go
url = https://github.com/tree-sitter/tree-sitter-go
- Execute the script to store the multiple
tree-sitter
into the library for local dev
python build_tree_sitter_lang_lib.py
- Register the mapping of language's file extension to name in the file
janus/settings.py
EXTENSION_LANG_NAME_MAPPING = {
'.py': 'python',
'.go': 'go',
# please add it here
}
- Add the language-specific analyzer into the file
janus/analyzer.py
inherited fromBaseComplexityAnalyzer
. Commonly you only need to overwrite the following configurations
class FooAnalyzer(BaseComplexityAnalyzer):
EXTENSION = '.foo'
# refer to the value in https://github.com/tree-sitter/tree-sitter-foo/blob/master/src/grammar.json
DECISION_TYPES = [
'if', ...
]
- Add test cases under the directory
tests/