|
| 1 | +import argparse |
1 | 2 | import collections
|
2 | 3 | import json
|
3 | 4 | import logging
|
4 | 5 | import os
|
5 | 6 | import subprocess
|
| 7 | +import sys |
6 | 8 | import time
|
7 | 9 |
|
8 | 10 | from .python import Python
|
|
12 | 14 | from .model import (TRUNK_COLOR, LEAF_COLOR, EDGE_COLOR, NODE_COLOR, GROUP_TYPE,
|
13 | 15 | Edge, Group, Node, Variable, is_installed, flatten)
|
14 | 16 |
|
15 |
| -VERSION = '2.1.0' |
| 17 | +VERSION = '2.1.1' |
16 | 18 |
|
17 | 19 | VALID_EXTENSIONS = {'png', 'svg', 'dot', 'gv', 'json'}
|
18 | 20 |
|
@@ -496,7 +498,6 @@ def code2flow(raw_source_paths, output_file, language=None, hide_legend=True,
|
496 | 498 | :param int level: logging level
|
497 | 499 | :rtype: None
|
498 | 500 | """
|
499 |
| - |
500 | 501 | start_time = time.time()
|
501 | 502 |
|
502 | 503 | if not isinstance(raw_source_paths, list):
|
@@ -555,3 +556,84 @@ def code2flow(raw_source_paths, output_file, language=None, hide_legend=True,
|
555 | 556 | # translate to an image if that was requested
|
556 | 557 | if final_img_filename:
|
557 | 558 | _generate_final_img(output_file, extension, final_img_filename, len(edges))
|
| 559 | + |
| 560 | + |
| 561 | +def main(sys_argv=None): |
| 562 | + """ |
| 563 | + CLI interface. Sys_argv is a parameter for the sake of unittest coverage. |
| 564 | + :param sys_argv list: |
| 565 | + :rtype: None |
| 566 | + """ |
| 567 | + parser = argparse.ArgumentParser( |
| 568 | + description=DESCRIPTION, |
| 569 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 570 | + parser.add_argument( |
| 571 | + 'sources', metavar='sources', nargs='+', |
| 572 | + help='source code file/directory paths.') |
| 573 | + parser.add_argument( |
| 574 | + '--output', '-o', default='out.png', |
| 575 | + help=f'output file path. Supported types are {VALID_EXTENSIONS}.') |
| 576 | + parser.add_argument( |
| 577 | + '--language', choices=['py', 'js'], |
| 578 | + help='process this language and ignore all other files.' |
| 579 | + 'If omitted, use the suffix of the first source file.') |
| 580 | + parser.add_argument( |
| 581 | + '--exclude-functions', |
| 582 | + help='exclude functions from the output. Comma delimited.') |
| 583 | + parser.add_argument( |
| 584 | + '--exclude-namespaces', |
| 585 | + help='exclude namespaces (Classes, modules, etc) from the output. Comma delimited.') |
| 586 | + parser.add_argument( |
| 587 | + '--no-grouping', action='store_true', |
| 588 | + help='instead of grouping functions into namespaces, let functions float.') |
| 589 | + parser.add_argument( |
| 590 | + '--no-trimming', action='store_true', |
| 591 | + help='show all functions/namespaces whether or not they connect to anything.') |
| 592 | + parser.add_argument( |
| 593 | + '--hide-legend', action='store_true', |
| 594 | + help='by default, Code2flow generates a small legend. This flag hides it.') |
| 595 | + parser.add_argument( |
| 596 | + '--skip-parse-errors', action='store_true', |
| 597 | + help='skip files that the language parser fails on.') |
| 598 | + parser.add_argument( |
| 599 | + '--source-type', choices=['script', 'module'], default='script', |
| 600 | + help='js only. Parse the source as scripts (commonJS) or modules (es6)') |
| 601 | + parser.add_argument( |
| 602 | + '--ruby-version', default='27', |
| 603 | + help='ruby only. Which ruby version to parse? This is passed directly into ruby-parse. Use numbers like 25, 27, or 31.') |
| 604 | + parser.add_argument( |
| 605 | + '--quiet', '-q', action='store_true', |
| 606 | + help='suppress most logging') |
| 607 | + parser.add_argument( |
| 608 | + '--verbose', '-v', action='store_true', |
| 609 | + help='add more logging') |
| 610 | + parser.add_argument( |
| 611 | + '--version', action='version', version='%(prog)s ' + VERSION) |
| 612 | + |
| 613 | + sys_argv = sys_argv or sys.argv[1:] |
| 614 | + args = parser.parse_args(sys_argv) |
| 615 | + level = logging.INFO |
| 616 | + if args.verbose and args.quiet: |
| 617 | + raise AssertionError("Passed both --verbose and --quiet flags") |
| 618 | + if args.verbose: |
| 619 | + level = logging.DEBUG |
| 620 | + if args.quiet: |
| 621 | + level = logging.WARNING |
| 622 | + |
| 623 | + exclude_namespaces = list(filter(None, (args.exclude_namespaces or "").split(','))) |
| 624 | + exclude_functions = list(filter(None, (args.exclude_functions or "").split(','))) |
| 625 | + lang_params = LanguageParams(args.source_type, args.ruby_version) |
| 626 | + |
| 627 | + code2flow( |
| 628 | + raw_source_paths=args.sources, |
| 629 | + output_file=args.output, |
| 630 | + language=args.language, |
| 631 | + hide_legend=args.hide_legend, |
| 632 | + exclude_namespaces=exclude_namespaces, |
| 633 | + exclude_functions=exclude_functions, |
| 634 | + no_grouping=args.no_grouping, |
| 635 | + no_trimming=args.no_trimming, |
| 636 | + skip_parse_errors=args.skip_parse_errors, |
| 637 | + lang_params=lang_params, |
| 638 | + level=level, |
| 639 | + ) |
0 commit comments