stubber.codemod.enrich¶
Enrich firmware stubs by copying docstrings and parameter information from doc-stubs or python source code. Both (.py or .pyi) files are supported.
Attributes¶
Classes¶
A match between a target and source file to merge docstrings and typehints |
Functions¶
|
Replace per-board volatile lines with stable placeholders. |
|
Restore the original volatile lines that _mask_volatile replaced. |
|
Build a content-based cache key for a merge transform (one or more sources). |
|
Run the (expensive) libcst merge transform and return the new code, or None. |
|
Run _run_merge_transform, transparently caching the result on disk. |
|
Clear the enrich cache. Returns the number of removed entries. |
|
Return simple statistics about the enrich cache. |
|
Given a target and source path, return the package name based on the path. |
|
Compare package names, return True if they are equal, ignoring an _ or u-prefix and case |
Given a target and source path, return a list of tuples of (target, source, package name) that are candidates for merging. |
|
|
Enrich firmware stubs using the doc-stubs in another folder. |
|
Generate a list of merge candidates for the source and target folders. |
|
Enrich a folder containing firmware stubs using the doc-stubs in another folder. |
|
Guess the port name from the folder contents. |
Module Contents¶
- stubber.codemod.enrich._ENRICH_CACHE = 'enrich'¶
- stubber.codemod.enrich.ENRICH_CACHE_VERSION = '9'¶
- stubber.codemod.enrich._NO_CHANGE = '\x00__enrich_no_change__\x00'¶
- stubber.codemod.enrich._VOLATILE_TOKEN = '__ENRICH_VOLATILE_{}__'¶
- stubber.codemod.enrich._VOLATILE_PATTERNS¶
- stubber.codemod.enrich._mask_volatile(text: str) Tuple[str, Dict[str, str]]¶
Replace per-board volatile lines with stable placeholders.
Returns the masked text and a mapping of placeholder -> original line so the original lines can be restored in the transform output.
- stubber.codemod.enrich._restore_volatile(text: str, restore: Dict[str, str]) str¶
Restore the original volatile lines that _mask_volatile replaced.
- stubber.codemod.enrich._enrich_cache_key(source_texts: List[str], target_text: str, module_name: str, copy_params: bool, copy_docstr: bool, copy_returns: bool) str¶
Build a content-based cache key for a merge transform (one or more sources).
- stubber.codemod.enrich._run_merge_transform(source_paths: List[pathlib.Path], target_text: str, module_name: str, filename: str, copy_params: bool, copy_docstr: bool, copy_returns: bool) str | None¶
Run the (expensive) libcst merge transform and return the new code, or None.
All source_paths are merged into the target in a single transform pass, which avoids re-parsing the (potentially large) target once per source doc-stub.
- stubber.codemod.enrich._cached_merge_transform(source_paths: List[pathlib.Path], target_text: str, module_name: str, filename: str, copy_params: bool, copy_docstr: bool, copy_returns: bool) str | None¶
Run _run_merge_transform, transparently caching the result on disk.
- stubber.codemod.enrich.clear_enrich_cache() int¶
Clear the enrich cache. Returns the number of removed entries.
- Return type:
- stubber.codemod.enrich.enrich_cache_stats() Dict[str, Any]¶
Return simple statistics about the enrich cache.
- Return type:
Dict[str, Any]
- class stubber.codemod.enrich.MergeMatch¶
A match between a target and source file to merge docstrings and typehints
- target: pathlib.Path¶
- source: pathlib.Path¶
- stubber.codemod.enrich.package_from_path(target: pathlib.Path, source: pathlib.Path | None = None) str¶
Given a target and source path, return the package name based on the path.
- Parameters:
target (pathlib.Path)
source (Optional[pathlib.Path])
- Return type:
- stubber.codemod.enrich.upackage_equal(src: str, target: str) Tuple[bool, int]¶
Compare package names, return True if they are equal, ignoring an _ or u-prefix and case
- stubber.codemod.enrich.source_target_candidates(source: pathlib.Path, target: pathlib.Path, ext: str | None = None) collections.abc.Generator[MergeMatch, None, None]¶
Given a target and source path, return a list of tuples of (target, source, package name) that are candidates for merging. Goal is to match the target and source files based on the package name, to avoid mismatched merges of docstrings and typehints
Returns a generator of tuples of (target, source, target_package, source_package, is_partial_match)
- Parameters:
source (pathlib.Path)
target (pathlib.Path)
ext (Optional[str])
- Return type:
collections.abc.Generator[MergeMatch, None, None]
- stubber.codemod.enrich.enrich_file(source_path: pathlib.Path | List[pathlib.Path], target_path: pathlib.Path, diff: bool = False, write_back: bool = False, copy_params: bool = False, copy_docstr: bool = False, copy_returns: bool = False) collections.abc.Generator[str, None, None]¶
Enrich firmware stubs using the doc-stubs in another folder. Both (.py or .pyi) files are supported. Both source an target files must exist, and are assumed to match. Any matching of source and target files should be done before calling this function.
- Parameters:
source_path (Union[pathlib.Path, List[pathlib.Path]]) – the path (or list of paths) to the doc-stub file(s) to enrich from. When several paths are given they are merged into the target in a single pass.
target_path (pathlib.Path) – the path to the firmware stub-file to enrich
diff (bool) – if True, return the diff between the original and the enriched source file
write_back (bool) – if True, write the enriched source file back to the source_path
copy_params (bool)
copy_docstr (bool)
copy_returns (bool)
- Return type:
collections.abc.Generator[str, None, None]
Returns: - None or a string containing the diff between the original and the enriched source file
- stubber.codemod.enrich.merge_candidates(source_folder: pathlib.Path, target_folder: pathlib.Path) List[MergeMatch]¶
Generate a list of merge candidates for the source and target folders. Each target is matched with exactly one source file.
- Parameters:
source_folder (pathlib.Path)
target_folder (pathlib.Path)
- Return type:
List[MergeMatch]
- stubber.codemod.enrich.enrich_folder(source_folder: pathlib.Path, target_folder: pathlib.Path, show_diff: bool = False, write_back: bool = False, require_docstub: bool = False, copy_params: bool = False, ext: str | None = None, copy_docstr: bool = False, copy_returns: bool = False) int¶
Enrich a folder containing firmware stubs using the doc-stubs in another folder.
Returns the number of files enriched.
- Parameters:
source_folder (pathlib.Path)
target_folder (pathlib.Path)
show_diff (bool)
write_back (bool)
require_docstub (bool)
copy_params (bool)
ext (Optional[str])
copy_docstr (bool)
copy_returns (bool)
- Return type:
- stubber.codemod.enrich.guess_port_from_path(folder: pathlib.Path) str¶
Guess the port name from the folder contents. ( could also be done based on the path name)
- Parameters:
folder (pathlib.Path)
- Return type: