Search command internals¶
vcspull.cli.search is the implementation behind
vcspull search — it matches repositories with
field-aware queries.
Search repositories functionality for vcspull.
-
class vcspull.cli.search.SearchToken¶class vcspull.cli.search.SearchToken¶
Bases:
NamedTupleParsed query token with optional field restrictions.
-
class vcspull.cli.search.SearchPattern¶class vcspull.cli.search.SearchPattern¶
Bases:
objectCompiled search pattern tied to repository fields.
-
vcspull.cli.search.normalize_fields(fields)¶vcspull.cli.search.normalize_fields(fields)¶
Normalize and validate search fields.
Examples
>>> normalize_fields(["name", "url"]) ('name', 'url') >>> normalize_fields(["name,url", "workspace"]) ('name', 'url', 'workspace') >>> normalize_fields(None) ('name', 'path', 'url', 'workspace')
-
vcspull.cli.search.parse_query_terms(terms, *, default_fields)¶vcspull.cli.search.parse_query_terms(terms, *, default_fields)¶
Parse raw search terms into field-scoped tokens.
Examples
>>> tokens = parse_query_terms( ... ["name:django", "github"], ... default_fields=("name", "url"), ... ) >>> tokens[0] SearchToken(fields=('name',), pattern='django') >>> tokens[1] SearchToken(fields=('name', 'url'), pattern='github')
-
vcspull.cli.search.compile_search_patterns(tokens, *, ignore_case, smart_case, fixed_strings, word_regexp)¶vcspull.cli.search.compile_search_patterns(tokens, *, ignore_case, smart_case, fixed_strings, word_regexp)¶
Compile search tokens into regex patterns.
- Parameters:
- Returns:
Compiled search patterns
- Return type:
Examples
>>> tokens = [SearchToken(fields=("name",), pattern="django")] >>> patterns = compile_search_patterns( ... tokens, ... ignore_case=True, ... smart_case=False, ... fixed_strings=False, ... word_regexp=False, ... ) >>> bool(patterns[0].regex.search("Django")) True
-
vcspull.cli.search.evaluate_match(fields, patterns, *, match_any)¶vcspull.cli.search.evaluate_match(fields, patterns, *, match_any)¶
Return match status and matched substrings by field.
- Parameters:
- Returns:
Match status and mapping of matched fields to match strings
- Return type:
Examples
>>> fields = { ... "name": "django", ... "path": "~/code/django", ... "url": "git+https://github.com/django/django.git", ... "workspace": "~/code/", ... } >>> tokens = parse_query_terms(["name:django"], default_fields=("name", "url")) >>> patterns = compile_search_patterns( ... tokens, ... ignore_case=False, ... smart_case=False, ... fixed_strings=False, ... word_regexp=False, ... ) >>> matched, matches = evaluate_match(fields, patterns, match_any=False) >>> matched True >>> matches["name"] ['django']
-
vcspull.cli.search.highlight_text(text, patterns, *, colors, base_color=None)¶vcspull.cli.search.highlight_text(text, patterns, *, colors, base_color=None)¶
Return text with regex matches highlighted.
Examples
>>> from vcspull.cli._colors import ColorMode >>> colors = Colors(ColorMode.NEVER) >>> highlight_text("django", [re.compile("jan")], colors=colors) 'django'
-
vcspull.cli.search.find_search_matches(repos, patterns, *, match_any, invert_match)¶vcspull.cli.search.find_search_matches(repos, patterns, *, match_any, invert_match)¶
Return search matches for repositories.
- Parameters:
repos (
list[ConfigDict]) – Repository configurations to searchpatterns (
list[SearchPattern]) – Compiled search patternsmatch_any (
bool) – Whether any token match is sufficientinvert_match (
bool) – Whether to return non-matching repositories
- Returns:
Search results containing matched fields
- Return type:
Examples
>>> repos = [ ... { ... "name": "django", ... "path": "/tmp/django", ... "url": "git+https://github.com/django/django.git", ... "workspace_root": "~/code/", ... }, ... ] >>> tokens = parse_query_terms(["django"], default_fields=DEFAULT_FIELDS) >>> patterns = compile_search_patterns( ... tokens, ... ignore_case=False, ... smart_case=False, ... fixed_strings=False, ... word_regexp=False, ... ) >>> results = find_search_matches( ... repos, ... patterns, ... match_any=False, ... invert_match=False, ... ) >>> [item["name"] for item in results] ['django']
-
vcspull.cli.search.create_search_subparser(parser)¶vcspull.cli.search.create_search_subparser(parser)¶
Create
vcspull searchargument subparser.- Parameters:
parser (
argparse.ArgumentParser) – The parser to configure- Return type:
Examples
>>> import argparse >>> parser = argparse.ArgumentParser() >>> create_search_subparser(parser) >>> parsed = parser.parse_args(["django"]) >>> parsed.query_terms ['django']
-
vcspull.cli.search.search_repos(query_terms, config_path, workspace_root, output_json, output_ndjson, color, *, fields, ignore_case, smart_case, fixed_strings, word_regexp, invert_match, match_any, emit_output=True)¶vcspull.cli.search.search_repos(query_terms, config_path, workspace_root, output_json, output_ndjson, color, *, fields, ignore_case, smart_case, fixed_strings, word_regexp, invert_match, match_any, emit_output=True)¶
Search configured repositories.
- Parameters:
config_path (
pathlib.Path|None) – Path to config file, or None to auto-discoveroutput_json (
bool) – Output as JSONoutput_ndjson (
bool) – Output as NDJSONcolor (
str) – Color mode (auto, always, never)ignore_case (
bool) – Force case-insensitive matchingsmart_case (
bool) – Enable smart-case matchingfixed_strings (
bool) – Treat terms as literal stringsword_regexp (
bool) – Match whole words onlyinvert_match (
bool) – Return non-matching repositoriesmatch_any (
bool) – Match if any term matchesemit_output (
bool) – Whether to emit human/JSON output
- Returns:
Search results
- Return type:
Examples
>>> from vcspull.config import save_config_yaml >>> config_file = tmp_path / ".vcspull.yaml" >>> save_config_yaml( ... config_file, ... {"~/code/": {"django": {"repo": "git+https://github.com/django/django.git"}}}, ... ) >>> results = search_repos( ... ["django"], ... config_path=config_file, ... workspace_root=None, ... output_json=False, ... output_ndjson=False, ... color="never", ... fields=None, ... ignore_case=False, ... smart_case=False, ... fixed_strings=False, ... word_regexp=False, ... invert_match=False, ... match_any=False, ... emit_output=False, ... ) >>> [item["name"] for item in results] ['django']