-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
165 lines (137 loc) · 4.07 KB
/
cli.py
File metadata and controls
165 lines (137 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import typing as t
import rich_click as click
from rich import print as rprint, box
from rich.pretty import pprint
from rich.syntax import Syntax
from rich.table import Table
from whats_that_code.election import guess_language_all_methods
from .api import Searchcode
sc = Searchcode(user_agent="searchCode-sdk/cli")
__all__ = ["cli"]
@click.group()
def cli():
"""
Searchcode
Simple, comprehensive code search.
"""
...
@cli.command()
@click.argument("query", type=str)
@click.option("--pretty", help="Return results in raw JSON format.", is_flag=True)
@click.option(
"--page",
type=int,
default=0,
help="Start page number (defaults to 0).",
)
@click.option(
"--per-page",
type=int,
default=100,
help="Results per page (defaults to 100).",
)
@click.option(
"--lines-of-code-lt",
type=int,
help="Filter to sources with less lines of code than the supplied value (Valid values: 0 to 10000).",
)
@click.option(
"--lines-of-code-gt",
type=int,
help="Filter to sources with greater lines of code than the supplied value (Valid values: 0 to 10000).",
)
@click.option(
"--sources",
type=str,
help="A comma-separated list of code sources to filter results.",
)
@click.option(
"--languages",
type=str,
help="A comma-separated list of code languages to filter results.",
)
@click.option(
"--callback",
type=str,
help="callback function (returns JSONP)",
)
def search(
query: str,
page: int = 0,
per_page: int = 100,
pretty: bool = False,
lines_of_code_lt: t.Optional[int] = None,
lines_of_code_gt: t.Optional[int] = None,
languages: t.Optional[str] = None,
sources: t.Optional[str] = None,
callback: t.Optional[str] = None,
):
"""
Query the code index and (returns 100 results by default).
e.g., searchcode search "import module"
"""
languages = languages.split(",") if languages else None
sources = sources.split(",") if sources else None
results = sc.search(
query=query,
page=page,
per_page=per_page,
languages=languages,
sources=sources,
lines_of_code_lt=lines_of_code_lt,
lines_of_code_gt=lines_of_code_gt,
callback=callback,
)
(
__print_jsonp(jsonp=results)
if callback
else (
pprint(results)
if pretty
else __print_table(records=results["results"], ignore_keys=["lines"])
)
)
@cli.command()
@click.argument("id", type=int)
def code(id: int):
"""
Get the raw data from a code file.
e.g., searchcode code 4061576
"""
code_data = sc.code(id)
if code_data:
language = guess_language_all_methods(code=code_data)
syntax = Syntax(code=code_data, lexer=language, line_numbers=True)
rprint(syntax)
def __print_table(records: t.List[t.Dict], ignore_keys: t.List[str] = None) -> None:
"""
Creates a rich table from a list of dict objects,
ignoring specified keys.
:param records: List of dict objects.
:param ignore_keys: List of keys to exclude from the table.
:return: None. Prints the table using rich.
"""
if not records:
raise ValueError("Data must be a non-empty list of dict objects.")
ignore_keys = ignore_keys or []
# Collect all unique keys across all records, excluding ignored ones
all_keys = set()
for record in records:
all_keys.update(key for key in record.keys() if key not in ignore_keys)
columns = sorted(all_keys)
table = Table(box=box.ROUNDED, highlight=True, header_style="bold")
for index, column in enumerate(columns):
style = "dim" if index == 0 else None
table.add_column(column.capitalize(), style=style)
for record in records:
data = record
row = [str(data.get(column, "")) for column in columns]
table.add_row(*row)
rprint(table)
def __print_jsonp(jsonp: str) -> None:
"""
Pretty-prints a raw JSONP string.
:param jsonp: A complete JSONP string.
"""
syntax = Syntax(jsonp, "text", line_numbers=True)
rprint(syntax)