tskel 0.90__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tskel might be problematic. Click here for more details.
- tskel/__init__.py +143 -0
- tskel/arguments.py +247 -0
- tskel/cli.py +53 -0
- tskel/configuration.py +383 -0
- tskel/constant.py +3 -0
- tskel/context_generator.py +194 -0
- tskel/extraction/__init__.py +1 -0
- tskel/extraction/ast_walker.py +309 -0
- tskel/extraction/comment_extractor.py +102 -0
- tskel/extraction/find_rules.py +147 -0
- tskel/extraction/result.py +25 -0
- tskel/extraction/source_analyzer.py +129 -0
- tskel/extraction/structure_extractor.py +254 -0
- tskel/file_processor.py +304 -0
- tskel/integration/__init__.py +1 -0
- tskel/integration/api.py +137 -0
- tskel/integration/extensions.py +26 -0
- tskel/language_handlers/__init__.py +105 -0
- tskel/language_handlers/c_handler.py +126 -0
- tskel/language_handlers/c_sharp_handler.py +112 -0
- tskel/language_handlers/cpp_handler.py +178 -0
- tskel/language_handlers/css_handler.py +22 -0
- tskel/language_handlers/ecmascript_handler.py +111 -0
- tskel/language_handlers/elixir_handler.py +149 -0
- tskel/language_handlers/go_handler.py +111 -0
- tskel/language_handlers/html_handler.py +27 -0
- tskel/language_handlers/java_handler.py +121 -0
- tskel/language_handlers/javascript_handler.py +93 -0
- tskel/language_handlers/kotlin_handler.py +152 -0
- tskel/language_handlers/language_handler.py +457 -0
- tskel/language_handlers/lua_handler.py +37 -0
- tskel/language_handlers/php_handler.py +111 -0
- tskel/language_handlers/python_handler.py +219 -0
- tskel/language_handlers/ruby_handler.py +116 -0
- tskel/language_handlers/rust_handler.py +129 -0
- tskel/language_handlers/scala_handler.py +122 -0
- tskel/language_handlers/swift_handler.py +181 -0
- tskel/language_handlers/tsx_handler.py +18 -0
- tskel/language_handlers/typescript_handler.py +101 -0
- tskel/language_parser/__init__.py +1 -0
- tskel/language_parser/language_styles.py +104 -0
- tskel/language_parser/language_syntax.py +263 -0
- tskel/language_parser/parsed_source.py +21 -0
- tskel/language_parser/source_nodes.py +146 -0
- tskel/models.py +76 -0
- tskel/output/__init__.py +4 -0
- tskel/output/cli_output.py +78 -0
- tskel/output/diagram_graph.py +81 -0
- tskel/output/diagram_models.py +28 -0
- tskel/output/diagram_notation.py +206 -0
- tskel/output/generation_result.py +21 -0
- tskel/output/outline_formatter.py +55 -0
- tskel/output/relation_formatter.py +163 -0
- tskel/output/report.py +162 -0
- tskel/relation/__init__.py +1 -0
- tskel/relation/relation_extractor.py +246 -0
- tskel/relation/relation_models.py +50 -0
- tskel/relation/relation_resolver.py +269 -0
- tskel/relation/relation_syntax.py +43 -0
- tskel/renderers/__init__.py +1 -0
- tskel/renderers/diagram_output_renderer.py +47 -0
- tskel/renderers/json_output_renderer.py +270 -0
- tskel/renderers/output_renderer.py +60 -0
- tskel/renderers/output_renderer_factory.py +51 -0
- tskel/renderers/text_output_renderer.py +173 -0
- tskel-0.90.dist-info/METADATA +349 -0
- tskel-0.90.dist-info/RECORD +78 -0
- tskel-0.90.dist-info/WHEEL +5 -0
- tskel-0.90.dist-info/entry_points.txt +3 -0
- tskel-0.90.dist-info/licenses/LICENSE +21 -0
- tskel-0.90.dist-info/top_level.txt +2 -0
- tskel_report/__init__.py +1 -0
- tskel_report/__main__.py +4 -0
- tskel_report/cli.py +92 -0
- tskel_report/loader.py +77 -0
- tskel_report/model.py +478 -0
- tskel_report/renderer.py +61 -0
- tskel_report/templates/report.html +542 -0
tskel/__init__.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Public API for the tree-sitter context generator src package."""
|
|
2
|
+
|
|
3
|
+
import sys as _sys
|
|
4
|
+
from importlib.machinery import ModuleSpec as _ModuleSpec
|
|
5
|
+
from importlib.util import module_from_spec as _module_from_spec
|
|
6
|
+
|
|
7
|
+
from .extraction.find_rules import CommentTagExtractor
|
|
8
|
+
from .extraction.comment_extractor import CommentType
|
|
9
|
+
from .output.outline_formatter import OutlineFormatter
|
|
10
|
+
from .renderers.output_renderer import OutputRenderer
|
|
11
|
+
from .language_handlers import (
|
|
12
|
+
create_handler,
|
|
13
|
+
CHandler,
|
|
14
|
+
CppHandler,
|
|
15
|
+
CSharpHandler,
|
|
16
|
+
CssHandler,
|
|
17
|
+
ElixirHandler,
|
|
18
|
+
GoHandler,
|
|
19
|
+
HtmlHandler,
|
|
20
|
+
JavaHandler,
|
|
21
|
+
JavaScriptHandler,
|
|
22
|
+
KotlinHandler,
|
|
23
|
+
LanguageHandler,
|
|
24
|
+
LuaHandler,
|
|
25
|
+
PhpHandler,
|
|
26
|
+
PythonHandler,
|
|
27
|
+
RubyHandler,
|
|
28
|
+
RustHandler,
|
|
29
|
+
ScalaHandler,
|
|
30
|
+
SwiftHandler,
|
|
31
|
+
TsxHandler,
|
|
32
|
+
TypeScriptHandler,
|
|
33
|
+
)
|
|
34
|
+
from .extraction.source_analyzer import SourceAnalyzer
|
|
35
|
+
from .language_parser.parsed_source import ParsedSource, parse_source
|
|
36
|
+
from .extraction.result import ExtractionResult
|
|
37
|
+
from .extraction.ast_walker import AstWalker
|
|
38
|
+
from .integration.api import create_generator, generate_from_args
|
|
39
|
+
from .arguments import CliHelpRequested, parse_configuration
|
|
40
|
+
from .integration.extensions import OutputTransform, ExtractionTransform
|
|
41
|
+
from .configuration import GeneratorConfig
|
|
42
|
+
from .file_processor import FileProcessor
|
|
43
|
+
from .extraction.structure_extractor import StructureExtractor
|
|
44
|
+
from .models import FileStructure
|
|
45
|
+
from .context_generator import ContextGenerator
|
|
46
|
+
from .output.generation_result import GenerationResult, PartitionDocument
|
|
47
|
+
|
|
48
|
+
__all__ = [
|
|
49
|
+
'parse_configuration',
|
|
50
|
+
'generate_from_args',
|
|
51
|
+
'CliHelpRequested',
|
|
52
|
+
'ExtractionTransform',
|
|
53
|
+
'OutputTransform',
|
|
54
|
+
'ParsedSource',
|
|
55
|
+
'SourceAnalyzer',
|
|
56
|
+
'parse_source',
|
|
57
|
+
'create_handler',
|
|
58
|
+
'create_generator',
|
|
59
|
+
'GeneratorConfig',
|
|
60
|
+
'PartitionDocument',
|
|
61
|
+
'StructureExtractor',
|
|
62
|
+
'FileStructure',
|
|
63
|
+
'LanguageHandler',
|
|
64
|
+
'PythonHandler',
|
|
65
|
+
'JavaScriptHandler',
|
|
66
|
+
'PhpHandler',
|
|
67
|
+
'JavaHandler',
|
|
68
|
+
'TypeScriptHandler',
|
|
69
|
+
'TsxHandler',
|
|
70
|
+
'HtmlHandler',
|
|
71
|
+
'CssHandler',
|
|
72
|
+
'CHandler',
|
|
73
|
+
'CppHandler',
|
|
74
|
+
'CSharpHandler',
|
|
75
|
+
'ElixirHandler',
|
|
76
|
+
'GoHandler',
|
|
77
|
+
'KotlinHandler',
|
|
78
|
+
'LuaHandler',
|
|
79
|
+
'RubyHandler',
|
|
80
|
+
'RustHandler',
|
|
81
|
+
'ScalaHandler',
|
|
82
|
+
'SwiftHandler',
|
|
83
|
+
'AstWalker',
|
|
84
|
+
'CommentTagExtractor',
|
|
85
|
+
'CommentType',
|
|
86
|
+
'OutlineFormatter',
|
|
87
|
+
'OutputRenderer',
|
|
88
|
+
'FileProcessor',
|
|
89
|
+
'ContextGenerator',
|
|
90
|
+
'ExtractionResult',
|
|
91
|
+
'GenerationResult',
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Supported submodule imports must share globals with their implementations.
|
|
96
|
+
_SUPPORTED_MODULES = {
|
|
97
|
+
'api': 'integration.api',
|
|
98
|
+
'analysis': 'extraction.source_analyzer',
|
|
99
|
+
'extensions': 'integration.extensions',
|
|
100
|
+
'ast_walker': 'extraction.ast_walker',
|
|
101
|
+
'structure_extractor': 'extraction.structure_extractor',
|
|
102
|
+
'comment_extractor': 'extraction.comment_extractor',
|
|
103
|
+
'comment_types': 'extraction.comment_extractor',
|
|
104
|
+
'find_rules': 'extraction.find_rules',
|
|
105
|
+
'find_extractor': 'extraction.find_rules',
|
|
106
|
+
'source_nodes': 'language_parser.source_nodes',
|
|
107
|
+
'language_syntax': 'language_parser.language_syntax',
|
|
108
|
+
'language_styles': 'language_parser.language_styles',
|
|
109
|
+
'processor': 'context_generator',
|
|
110
|
+
'languages': 'language_handlers',
|
|
111
|
+
}
|
|
112
|
+
for _name, _path in _SUPPORTED_MODULES.items():
|
|
113
|
+
_module = _sys.modules[f'{__name__}.{_path}']
|
|
114
|
+
_sys.modules[f'{__name__}.{_name}'] = _module
|
|
115
|
+
globals()[_name] = _module
|
|
116
|
+
|
|
117
|
+
# Handler submodules must not load a second set of classes under the supported name.
|
|
118
|
+
_handler_prefix = f'{__name__}.language_handlers.'
|
|
119
|
+
for _name, _module in tuple(_sys.modules.items()):
|
|
120
|
+
if _name.startswith(_handler_prefix):
|
|
121
|
+
_suffix = _name[len(_handler_prefix):]
|
|
122
|
+
if _suffix == 'language_handler':
|
|
123
|
+
_suffix = 'languages'
|
|
124
|
+
_sys.modules[f'{__name__}.languages.{_suffix}'] = _module
|
|
125
|
+
languages.languages = _sys.modules[f'{__name__}.language_handlers.language_handler']
|
|
126
|
+
|
|
127
|
+
# Supported generation imports need a namespace, not a source directory.
|
|
128
|
+
generation = _module_from_spec(_ModuleSpec(f'{__name__}.generation', None, is_package=True))
|
|
129
|
+
_sys.modules[generation.__name__] = generation
|
|
130
|
+
_sys.modules[f'{__name__}.generation.processor'] = processor
|
|
131
|
+
generation.processor = processor
|
|
132
|
+
_sys.modules[f'{__name__}.generation.context_generator'] = processor
|
|
133
|
+
generation.context_generator = processor
|
|
134
|
+
_sys.modules[f'{__name__}.generation.result'] = _sys.modules[f'{__name__}.output.generation_result']
|
|
135
|
+
generation.result = _sys.modules[f'{__name__}.output.generation_result']
|
|
136
|
+
_sys.modules[f'{__name__}.generation.file_processor'] = _sys.modules[f'{__name__}.file_processor']
|
|
137
|
+
generation.file_processor = _sys.modules[f'{__name__}.file_processor']
|
|
138
|
+
|
|
139
|
+
_extraction = _sys.modules[f'{__name__}.extraction']
|
|
140
|
+
_sys.modules[f'{__name__}.extraction.find_extractor'] = find_rules
|
|
141
|
+
_extraction.find_extractor = find_rules
|
|
142
|
+
_sys.modules[f'{__name__}.extraction.source_nodes'] = source_nodes
|
|
143
|
+
_extraction.source_nodes = source_nodes
|
tskel/arguments.py
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Tree-sitter context file generator CLI.
|
|
2
|
+
|
|
3
|
+
Extracts class/function structure with type annotations from source files
|
|
4
|
+
and outputs a document suitable for use as LLM context.
|
|
5
|
+
|
|
6
|
+
Usage examples:
|
|
7
|
+
# Display generated context on stdout, with operation status on stderr
|
|
8
|
+
tskel /path/to/file.py
|
|
9
|
+
tskel /path/to/src/*.js --lang js
|
|
10
|
+
tskel /path/to/src --lang py
|
|
11
|
+
tskel dir1 dir2 dir3
|
|
12
|
+
|
|
13
|
+
tskel examples --json
|
|
14
|
+
tskel examples --json=compact
|
|
15
|
+
|
|
16
|
+
# Write to the format default or a chosen filename
|
|
17
|
+
tskel /path/to/project --file
|
|
18
|
+
tskel /path/to/project --file context.txt
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import argparse
|
|
23
|
+
from typing import NoReturn, TextIO
|
|
24
|
+
|
|
25
|
+
from .configuration import (
|
|
26
|
+
DEFAULT_JSON_LEVEL, DEFAULT_OUTPUT_FILE, JSON_LEVELS, OUTPUT_EXTENSIONS,
|
|
27
|
+
CliOverrides, ConfigurationLoader, GeneratorConfig,
|
|
28
|
+
)
|
|
29
|
+
from .language_handlers import STYLE_NAMES
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
_JSON_COMPACT_FORMAT = "json=compact"
|
|
33
|
+
_TRUE_VALUES = frozenset({"1", "true", "t", "yes", "y", "on"})
|
|
34
|
+
_FALSE_VALUES = frozenset({"0", "false", "f", "no", "n", "off"})
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _parse_bool(value: str) -> bool:
|
|
38
|
+
"""Parse case-insensitive CLI boolean spellings."""
|
|
39
|
+
normalized = value.lower()
|
|
40
|
+
if normalized in _TRUE_VALUES:
|
|
41
|
+
return True
|
|
42
|
+
if normalized in _FALSE_VALUES:
|
|
43
|
+
return False
|
|
44
|
+
raise argparse.ArgumentTypeError(f"invalid truth value {value!r}")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class CliHelpRequested(Exception):
|
|
48
|
+
"""Carry normal CLI help to library callers without printing or exiting."""
|
|
49
|
+
|
|
50
|
+
def __init__(self, help_text: str) -> None:
|
|
51
|
+
self.help_text = help_text
|
|
52
|
+
super().__init__(help_text)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class _LibraryArgumentParser(argparse.ArgumentParser):
|
|
56
|
+
"""Convert parser errors and help into exceptions before output is written."""
|
|
57
|
+
|
|
58
|
+
def error(self, message: str) -> NoReturn:
|
|
59
|
+
"""Raise a syntax error without argparse's printing or process exit."""
|
|
60
|
+
raise argparse.ArgumentError(None, message)
|
|
61
|
+
|
|
62
|
+
def print_help(self, file: TextIO | None = None) -> NoReturn:
|
|
63
|
+
"""Intercept the help action before it prints to any output stream."""
|
|
64
|
+
raise CliHelpRequested(self.format_help())
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class ArgumentParserBuilder:
|
|
68
|
+
"""Own the shared CLI option schema independently of configuration loading."""
|
|
69
|
+
|
|
70
|
+
def build(
|
|
71
|
+
self,
|
|
72
|
+
parser_type: type[argparse.ArgumentParser] = argparse.ArgumentParser,
|
|
73
|
+
) -> argparse.ArgumentParser:
|
|
74
|
+
"""Build the one shared option schema with the requested error/help boundary."""
|
|
75
|
+
parser = parser_type(
|
|
76
|
+
prog="tskel",
|
|
77
|
+
description="Generate a structure context from source files.",
|
|
78
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
79
|
+
epilog=__doc__,
|
|
80
|
+
)
|
|
81
|
+
parser.add_argument(
|
|
82
|
+
"paths",
|
|
83
|
+
nargs="*",
|
|
84
|
+
help="Source files or directories. Overrides YAML paths; defaults to ./ when neither is set.",
|
|
85
|
+
)
|
|
86
|
+
self._add_output_arguments(parser)
|
|
87
|
+
self._add_language_arguments(parser)
|
|
88
|
+
self._add_comment_arguments(parser)
|
|
89
|
+
self._add_filter_arguments(parser)
|
|
90
|
+
return parser
|
|
91
|
+
|
|
92
|
+
def _add_output_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
93
|
+
"""Register output arguments on the parser."""
|
|
94
|
+
parser.add_argument("--quiet", action="store_true", default=None,
|
|
95
|
+
help="Suppress command messages and write summaries; keep generated output, errors, and help.")
|
|
96
|
+
parser.add_argument("--color", action="store_true", default=None,
|
|
97
|
+
help="Color text outlines on stdout, including redirected output; files stay plain.")
|
|
98
|
+
parser.add_argument("--format", choices=tuple(OUTPUT_EXTENSIONS), default=None,
|
|
99
|
+
help="Document syntax (default: text): " + ", ".join(OUTPUT_EXTENSIONS) + ".")
|
|
100
|
+
parser.add_argument(
|
|
101
|
+
"--json", "--json=compact", dest="format", action="store_const", const=_JSON_COMPACT_FORMAT,
|
|
102
|
+
help="Select compact JSON. Use --json=pretty for two-space indentation.",
|
|
103
|
+
)
|
|
104
|
+
parser.add_argument(
|
|
105
|
+
"--json=pretty", dest="format", action="store_const", const="json",
|
|
106
|
+
help=argparse.SUPPRESS,
|
|
107
|
+
)
|
|
108
|
+
parser.add_argument(
|
|
109
|
+
"--json_level", "--json-level", type=int, choices=JSON_LEVELS, metavar="LEVEL",
|
|
110
|
+
help=f"JSON detail: 1-2 summary, 3-4 declaration details, 5 complete (default: {DEFAULT_JSON_LEVEL}).",
|
|
111
|
+
)
|
|
112
|
+
parser.add_argument(
|
|
113
|
+
"--file",
|
|
114
|
+
metavar="FILE",
|
|
115
|
+
nargs="?",
|
|
116
|
+
const="",
|
|
117
|
+
help=(f"Write output to FILE. Bare --file or --file 1 uses the format default "
|
|
118
|
+
f"({DEFAULT_OUTPUT_FILE} for text); the value true also selects it. "
|
|
119
|
+
"Other values are filenames."),
|
|
120
|
+
)
|
|
121
|
+
parser.add_argument(
|
|
122
|
+
"--segment",
|
|
123
|
+
nargs="?",
|
|
124
|
+
const=1,
|
|
125
|
+
type=int,
|
|
126
|
+
choices=range(6),
|
|
127
|
+
metavar="N",
|
|
128
|
+
help="Segment output by directory depth (0-5; bare selects 1). Enables default file output if needed.",
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
def _add_language_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
132
|
+
"""Register language arguments on the parser."""
|
|
133
|
+
parser.add_argument("--style", choices=STYLE_NAMES, default=None,
|
|
134
|
+
help="Signature language style; auto follows the selected parser for each file.")
|
|
135
|
+
parser.add_argument(
|
|
136
|
+
"--lang", metavar="LANG", help="Activate only the handler for the given language extension (e.g. py, js, php)."
|
|
137
|
+
)
|
|
138
|
+
parser.add_argument(
|
|
139
|
+
"--public_only",
|
|
140
|
+
"--public-only",
|
|
141
|
+
dest="public_only",
|
|
142
|
+
action="store_true",
|
|
143
|
+
default=None,
|
|
144
|
+
help="Emit only public class methods and constructors (default: false).",
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def _add_comment_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
148
|
+
"""Register comment arguments on the parser."""
|
|
149
|
+
parser.add_argument(
|
|
150
|
+
"--comment",
|
|
151
|
+
dest="enable_comment",
|
|
152
|
+
nargs="?",
|
|
153
|
+
const=True,
|
|
154
|
+
type=_parse_bool,
|
|
155
|
+
metavar="BOOL",
|
|
156
|
+
default=None,
|
|
157
|
+
help="Include documentation comments (default: true).",
|
|
158
|
+
)
|
|
159
|
+
parser.add_argument(
|
|
160
|
+
"--comment_scope",
|
|
161
|
+
action="append",
|
|
162
|
+
metavar="SCOPE",
|
|
163
|
+
default=None,
|
|
164
|
+
help="Documentation-comment scope: global, class, method, or all. Can be repeated.",
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
def _add_filter_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
168
|
+
"""Register filter arguments on the parser."""
|
|
169
|
+
parser.add_argument(
|
|
170
|
+
"--gitignore",
|
|
171
|
+
nargs="?",
|
|
172
|
+
const=True,
|
|
173
|
+
type=_parse_bool,
|
|
174
|
+
metavar="BOOL",
|
|
175
|
+
default=None,
|
|
176
|
+
help="Apply root and nested .gitignore filtering during directory traversal (default: true).",
|
|
177
|
+
)
|
|
178
|
+
parser.add_argument("--config", metavar="FILE", help="Load settings from a YAML configuration file.")
|
|
179
|
+
parser.add_argument(
|
|
180
|
+
"--exclude",
|
|
181
|
+
metavar="PATTERN",
|
|
182
|
+
action="append",
|
|
183
|
+
dest="excludes",
|
|
184
|
+
help='Regex pattern to exclude files/dirs by path. Can be specified multiple times. e.g. "abc", "(abc1|abc2)"',
|
|
185
|
+
)
|
|
186
|
+
parser.add_argument(
|
|
187
|
+
"--glob",
|
|
188
|
+
metavar="PATTERN",
|
|
189
|
+
action="append",
|
|
190
|
+
dest="globs",
|
|
191
|
+
help="Include relative paths matching a quoted wildcard before regex filters. Repeat for OR; * matches /.",
|
|
192
|
+
)
|
|
193
|
+
parser.add_argument(
|
|
194
|
+
"--filter",
|
|
195
|
+
metavar="PATTERN",
|
|
196
|
+
action="append",
|
|
197
|
+
dest="filters",
|
|
198
|
+
help="Regex pattern to include files by path after --glob. Repeat for OR; both groups must match.",
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def build_arg_parser(
|
|
203
|
+
parser_type: type[argparse.ArgumentParser] = argparse.ArgumentParser,
|
|
204
|
+
) -> argparse.ArgumentParser:
|
|
205
|
+
"""Build the shared CLI schema with the requested error/help boundary."""
|
|
206
|
+
return ArgumentParserBuilder().build(parser_type)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def parse_configuration(args: list[str]) -> GeneratorConfig:
|
|
210
|
+
"""Load CLI/YAML settings without source reads, output, or process exit.
|
|
211
|
+
|
|
212
|
+
Invalid option syntax raises argparse.ArgumentError. Help raises
|
|
213
|
+
CliHelpRequested before configuration discovery or source processing.
|
|
214
|
+
"""
|
|
215
|
+
if not isinstance(args, list) or any(not isinstance(value, str) for value in args):
|
|
216
|
+
raise TypeError("args must be a list of strings without the executable name.")
|
|
217
|
+
namespace = build_arg_parser(_LibraryArgumentParser).parse_args(args)
|
|
218
|
+
return _load_configuration(namespace)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _load_configuration(args: argparse.Namespace) -> GeneratorConfig:
|
|
222
|
+
"""Load YAML settings with explicit parsed CLI overrides."""
|
|
223
|
+
json_compact = args.format == _JSON_COMPACT_FORMAT
|
|
224
|
+
return ConfigurationLoader().load(
|
|
225
|
+
CliOverrides(
|
|
226
|
+
format="json" if json_compact else args.format,
|
|
227
|
+
json_compact=json_compact,
|
|
228
|
+
json_level=args.json_level,
|
|
229
|
+
style=args.style,
|
|
230
|
+
config=args.config,
|
|
231
|
+
paths=args.paths or None,
|
|
232
|
+
file=args.file,
|
|
233
|
+
segment=args.segment,
|
|
234
|
+
enable_comment=args.enable_comment,
|
|
235
|
+
comment_scope=args.comment_scope,
|
|
236
|
+
excludes=args.excludes,
|
|
237
|
+
filters=args.filters,
|
|
238
|
+
globs=args.globs,
|
|
239
|
+
disable_gitignore=None if args.gitignore is None else not args.gitignore,
|
|
240
|
+
lang=args.lang,
|
|
241
|
+
public_only=args.public_only,
|
|
242
|
+
quiet=args.quiet,
|
|
243
|
+
color=args.color,
|
|
244
|
+
)
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
tskel/cli.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Display or save generated source context using shared CLI options.
|
|
2
|
+
|
|
3
|
+
Examples:
|
|
4
|
+
tskel src --lang py
|
|
5
|
+
tskel src --file context.txt
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
from io import TextIOWrapper
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from .integration.api import _generate_configured
|
|
13
|
+
from .arguments import CliHelpRequested, build_arg_parser as _build_arg_parser, parse_configuration
|
|
14
|
+
from .configuration import ConfigurationError
|
|
15
|
+
from .integration.extensions import OutputTransform, ExtractionTransform
|
|
16
|
+
from .output.report import CliReportFormatter, FileWriter
|
|
17
|
+
from .output.cli_output import CliOutput
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def main(
|
|
21
|
+
argv: list[str] | None = None,
|
|
22
|
+
*,
|
|
23
|
+
extraction_transform: ExtractionTransform | None = None,
|
|
24
|
+
output_transform: OutputTransform | None = None,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Forward CLI options, generate once, and perform CLI output/error handling."""
|
|
27
|
+
_configure_redirected_streams()
|
|
28
|
+
try:
|
|
29
|
+
config = parse_configuration(sys.argv[1:] if argv is None else argv)
|
|
30
|
+
generation = _generate_configured(
|
|
31
|
+
config,
|
|
32
|
+
extraction_transform=extraction_transform,
|
|
33
|
+
output_transform=output_transform,
|
|
34
|
+
)
|
|
35
|
+
CliOutput(CliReportFormatter(), FileWriter()).emit(config, generation)
|
|
36
|
+
except CliHelpRequested as exc:
|
|
37
|
+
print(exc.help_text, end="")
|
|
38
|
+
except argparse.ArgumentError as exc:
|
|
39
|
+
_build_arg_parser().error(str(exc))
|
|
40
|
+
except (ConfigurationError, OSError, ValueError) as exc:
|
|
41
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
42
|
+
sys.exit(1)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _configure_redirected_streams() -> None:
|
|
46
|
+
"""Use UTF-8/LF for redirected CLI text without replacing caller-owned streams."""
|
|
47
|
+
for stream in (sys.stdout, sys.stderr):
|
|
48
|
+
if isinstance(stream, TextIOWrapper) and not stream.isatty():
|
|
49
|
+
stream.reconfigure(encoding="utf-8", errors=stream.errors, newline="\n")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
main()
|