-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__app.py
More file actions
184 lines (160 loc) · 4.7 KB
/
__app.py
File metadata and controls
184 lines (160 loc) · 4.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Copyright (C) 2024 Ritchie Mwewa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import typing as t
import rich_click as click
from rich.console import Console
from rich.pretty import pprint
from rich.syntax import Syntax
from . import License
from .__lib import (
__pkg__,
__version__,
update_window_title,
clear_screen,
print_jsonp,
print_panels,
)
from .api import Searchcode
sc = Searchcode(user_agent=f"{__pkg__}-sdk/cli")
__all__ = ["cli"]
console = Console(highlight=True)
@click.group()
@click.version_option(version=__version__, package_name=__pkg__)
def cli():
"""
Searchcode
Simple, comprehensive code search.
"""
update_window_title(text="Source code search engine.")
@cli.command("license")
@click.option("--conditions", help="License terms and conditions.", is_flag=True)
@click.option("--warranty", help="License warranty.", is_flag=True)
@click.pass_context
def licence(
ctx: click.Context, conditions: t.Optional[bool], warranty: t.Optional[bool]
):
"""
Show license information
"""
clear_screen()
update_window_title(
text="Terms and Conditions" if conditions else "Warranty" if warranty else None
)
if conditions:
console.print(
License.terms_and_conditions,
justify="center",
)
elif warranty:
console.print(
License.warranty,
justify="center",
)
else:
click.echo(ctx.get_help())
@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 (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 (returns 100 results by default).
e.g., sc search "import module"
"""
clear_screen()
update_window_title(text=query)
with console.status(
f"Querying code index with search string: [green]{query}[/]..."
):
languages = languages.split(",") if languages else None
sources = sources.split(",") if sources else None
response = 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=response)
if callback
else (pprint(response) if pretty else print_panels(data=response.results))
)
@cli.command()
@click.argument("id", type=int)
def code(id: int):
"""
Get the raw data from a code file.
e.g., sc code 4061576
"""
clear_screen()
update_window_title(text=str(id))
with console.status(f"Fetching data for code file with ID: [cyan]{id}[/]..."):
data = sc.code(id)
lines = data.code
language = data.language
if lines:
syntax = Syntax(
code=lines, lexer=language, line_numbers=True, theme="dracula"
)
console.print(syntax)