|
| 1 | +import argparse |
| 2 | +from argparse import _SubParsersAction |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from package_parser.cli._run_all import _run_all_command |
| 6 | +from package_parser.cli._run_annotations import _run_annotations |
| 7 | +from package_parser.cli._run_api import _run_api_command |
| 8 | +from package_parser.cli._run_usages import _run_usages_command |
| 9 | + |
| 10 | +_API_COMMAND = "api" |
| 11 | +_USAGES_COMMAND = "usages" |
| 12 | +_ANNOTATIONS_COMMAND = "annotations" |
| 13 | +_ALL_COMMAND = "all" |
| 14 | + |
| 15 | + |
| 16 | +def cli() -> None: |
| 17 | + args = _get_args() |
| 18 | + if args.command == _API_COMMAND: |
| 19 | + _run_api_command(args.package, args.src, args.out) |
| 20 | + elif args.command == _USAGES_COMMAND: |
| 21 | + _run_usages_command(args.package, args.client, args.tmp, args.out) |
| 22 | + elif args.command == _ANNOTATIONS_COMMAND: |
| 23 | + _run_annotations(args.api, args.usages, args.out) |
| 24 | + elif args.command == _ALL_COMMAND: |
| 25 | + _run_all_command(args) |
| 26 | + |
| 27 | + |
| 28 | +def _get_args() -> argparse.Namespace: |
| 29 | + parser = argparse.ArgumentParser(description="Analyze Python code.") |
| 30 | + |
| 31 | + # Commands |
| 32 | + subparsers = parser.add_subparsers(dest="command") |
| 33 | + _add_api_subparser(subparsers) |
| 34 | + _add_usages_subparser(subparsers) |
| 35 | + _add_annotations_subparser(subparsers) |
| 36 | + _add_all_subparser(subparsers) |
| 37 | + |
| 38 | + return parser.parse_args() |
| 39 | + |
| 40 | + |
| 41 | +def _add_api_subparser(subparsers: _SubParsersAction) -> None: |
| 42 | + api_parser = subparsers.add_parser(_API_COMMAND, help="List the API of a package.") |
| 43 | + api_parser.add_argument( |
| 44 | + "-p", |
| 45 | + "--package", |
| 46 | + help="The name of the package.", |
| 47 | + type=str, |
| 48 | + required=True, |
| 49 | + ) |
| 50 | + api_parser.add_argument( |
| 51 | + "-s", |
| 52 | + "--src", |
| 53 | + help="Directory containing the Python code of the package. If this is omitted, we try to locate the package " |
| 54 | + "with the given name in the current Python interpreter.", |
| 55 | + type=Path, |
| 56 | + required=False, |
| 57 | + default=None, |
| 58 | + ) |
| 59 | + api_parser.add_argument( |
| 60 | + "-o", "--out", help="Output directory.", type=Path, required=True |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def _add_usages_subparser(subparsers: _SubParsersAction) -> None: |
| 65 | + usages_parser = subparsers.add_parser( |
| 66 | + _USAGES_COMMAND, help="Find usages of API elements." |
| 67 | + ) |
| 68 | + usages_parser.add_argument( |
| 69 | + "-p", |
| 70 | + "--package", |
| 71 | + help="The name of the package. It must be installed in the current interpreter.", |
| 72 | + type=str, |
| 73 | + required=True, |
| 74 | + ) |
| 75 | + usages_parser.add_argument( |
| 76 | + "-c", |
| 77 | + "--client", |
| 78 | + help="Directory containing Python code that uses the package.", |
| 79 | + type=Path, |
| 80 | + required=True, |
| 81 | + ) |
| 82 | + usages_parser.add_argument( |
| 83 | + "-t", |
| 84 | + "--tmp", |
| 85 | + help="Directory where temporary files can be stored (to save progress in case the program crashes).", |
| 86 | + type=Path, |
| 87 | + required=True, |
| 88 | + ) |
| 89 | + usages_parser.add_argument( |
| 90 | + "-o", "--out", help="Output directory.", type=Path, required=True |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def _add_annotations_subparser(subparsers): |
| 95 | + generate_parser = subparsers.add_parser( |
| 96 | + _ANNOTATIONS_COMMAND, help="Generate Annotations automatically." |
| 97 | + ) |
| 98 | + generate_parser.add_argument( |
| 99 | + "-a", |
| 100 | + "--api", |
| 101 | + help="File created by the 'api' command.", |
| 102 | + type=Path, |
| 103 | + required=True, |
| 104 | + ) |
| 105 | + generate_parser.add_argument( |
| 106 | + "-u", |
| 107 | + "--usages", |
| 108 | + help="File created by the 'usages' command that contains usage counts.", |
| 109 | + type=Path, |
| 110 | + required=True, |
| 111 | + ) |
| 112 | + generate_parser.add_argument( |
| 113 | + "-o", "--out", help="Output directory.", type=Path, required=True |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def _add_all_subparser(subparsers: _SubParsersAction) -> None: |
| 118 | + all_parser = subparsers.add_parser( |
| 119 | + _ALL_COMMAND, |
| 120 | + help="Run api and usages command in parallel and then run annotations command.", |
| 121 | + ) |
| 122 | + all_parser.add_argument( |
| 123 | + "-p", |
| 124 | + "--package", |
| 125 | + help="The name of the package.", |
| 126 | + type=str, |
| 127 | + required=True, |
| 128 | + ) |
| 129 | + all_parser.add_argument( |
| 130 | + "-s", |
| 131 | + "--src", |
| 132 | + help="Directory containing the Python code of the package. If this is omitted, we try to locate the package " |
| 133 | + "with the given name in the current Python interpreter.", |
| 134 | + type=Path, |
| 135 | + required=False, |
| 136 | + default=None, |
| 137 | + ) |
| 138 | + all_parser.add_argument( |
| 139 | + "-c", |
| 140 | + "--client", |
| 141 | + help="Directory containing Python code that uses the package.", |
| 142 | + type=Path, |
| 143 | + required=True, |
| 144 | + ) |
| 145 | + all_parser.add_argument( |
| 146 | + "-o", "--out", help="Output directory.", type=Path, required=True |
| 147 | + ) |
0 commit comments