|
1 | | -""" main module of testsdiffer for console (cli) usage""" |
2 | | -import os |
| 1 | +""" main module of testsdiffer for console (cli) usage""" |
| 2 | + |
3 | 3 | import pprint |
| 4 | +import sys |
4 | 5 |
|
5 | | -import clifier |
| 6 | +import click |
6 | 7 |
|
7 | 8 | from codegraph import __version__, core |
8 | 9 |
|
9 | | -CLI_CFG_NAME = "conf/cli.yml" |
| 10 | +CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) |
| 11 | + |
| 12 | + |
| 13 | +@click.command(context_settings=CONTEXT_SETTINGS) |
| 14 | +@click.version_option(version=__version__, message="CodeGraph version %(version)s") |
| 15 | +@click.argument("paths", nargs=-1, type=click.Path(exists=True)) |
| 16 | +@click.option( |
| 17 | + "-o", |
| 18 | + "--object-only", |
| 19 | + is_flag=True, |
| 20 | + help="Don't visualize code dependencies as graph", |
| 21 | +) |
| 22 | +@click.option("--file-path", help="File path to start dependency search from") |
| 23 | +@click.option("--distance", type=int, help="Distance to search for dependencies") |
| 24 | +def cli(paths, object_only, file_path, distance): |
| 25 | + """ |
| 26 | + Tool that creates a graph of code to show dependencies between code entities (methods, classes, etc.). |
| 27 | + CodeGraph does not execute code, it is based only on lex and syntax parsing. |
10 | 28 |
|
| 29 | + PATHS: Provide path(s) to code base |
| 30 | + """ |
| 31 | + if not paths: |
| 32 | + click.echo( |
| 33 | + "Error: No paths provided. Please specify at least one path to the code base.", |
| 34 | + err=True, |
| 35 | + ) |
| 36 | + sys.exit(1) |
11 | 37 |
|
12 | | -def cli(): |
13 | | - config_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), CLI_CFG_NAME) |
14 | | - _cli = clifier.Clifier(config_path, prog_version=__version__) |
15 | | - parser = _cli.create_parser() |
16 | | - args = parser.parse_args() |
| 38 | + args = { |
| 39 | + "paths": paths, |
| 40 | + "object_only": object_only, |
| 41 | + "file_path": file_path, |
| 42 | + "distance": distance, |
| 43 | + } |
17 | 44 | main(args) |
18 | 45 |
|
19 | 46 |
|
20 | 47 | def main(args): |
21 | | - usage_graph = core.CodeGraph(args).usage_graph() |
22 | | - pprint.pprint(usage_graph) |
23 | | - if not args.object_only: |
24 | | - # to make more quick work if not needed to visualize |
25 | | - import codegraph.vizualyzer as vz |
| 48 | + code_graph = core.CodeGraph(args) |
| 49 | + usage_graph = code_graph.usage_graph() |
| 50 | + |
| 51 | + if args.get("file_path") and args.get("distance"): |
| 52 | + dependencies = code_graph.get_dependencies(args["file_path"], args["distance"]) |
| 53 | + print(f"Dependencies for {args['file_path']}:") |
| 54 | + for distance, files in dependencies.items(): |
| 55 | + print(f" Distance {distance}: {', '.join(files)}") |
| 56 | + else: |
| 57 | + pprint.pprint(usage_graph) |
| 58 | + if not args["object_only"]: |
| 59 | + import codegraph.vizualyzer as vz |
| 60 | + |
| 61 | + vz.draw_graph(usage_graph) |
| 62 | + |
26 | 63 |
|
27 | | - vz.draw_graph(usage_graph) |
| 64 | +if __name__ == "__main__": |
| 65 | + cli() |
0 commit comments