Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit655db55

Browse files
committed
fixes#36
1 parentd79f807 commit655db55

File tree

4 files changed

+269
-92
lines changed

4 files changed

+269
-92
lines changed

‎dialoghelper/_modidx.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
'dialoghelper.core._umsg': ('core.html#_umsg','dialoghelper/core.py'),
1010
'dialoghelper.core.add_html': ('core.html#add_html','dialoghelper/core.py'),
1111
'dialoghelper.core.add_msg': ('core.html#add_msg','dialoghelper/core.py'),
12+
'dialoghelper.core.ast_grep': ('core.html#ast_grep','dialoghelper/core.py'),
13+
'dialoghelper.core.ast_py': ('core.html#ast_py','dialoghelper/core.py'),
1214
'dialoghelper.core.call_endp': ('core.html#call_endp','dialoghelper/core.py'),
1315
'dialoghelper.core.curr_dialog': ('core.html#curr_dialog','dialoghelper/core.py'),
1416
'dialoghelper.core.del_msg': ('core.html#del_msg','dialoghelper/core.py'),

‎dialoghelper/core.py‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
# %% auto 0
44
__all__= ['Placements','empty','find_var','call_endp','find_dname','find_msg_id','curr_dialog','find_msgs','msg_idx',
5-
'read_msg','add_html','del_msg','run_msg','add_msg','update_msg','url2note','load_gist','gist_file',
6-
'import_string','is_usable_tool','mk_toollist','import_gist','tool_info','asdict']
5+
'read_msg','add_html','run_msg','add_msg','del_msg','update_msg','url2note','ast_py','ast_grep',
6+
'load_gist','gist_file','import_string','is_usable_tool','mk_toollist','import_gist','tool_info',
7+
'asdict']
78

89
# %% ../nbs/00_core.ipynb
910
importjson,importlib,linecache
@@ -106,14 +107,6 @@ def add_html(
106107
"Send HTML to the browser to be swapped into the DOM"
107108
call_endp('add_html_',dname,content=to_xml(content))
108109

109-
# %% ../nbs/00_core.ipynb
110-
defdel_msg(
111-
msgid:str=None,# id of message to delete
112-
dname:str=''# Running dialog to get info for; defaults to current dialog
113-
):
114-
"Delete a message from the dialog."
115-
call_endp('rm_msg_',dname,raiseex=True,msid=msgid)
116-
117110
# %% ../nbs/00_core.ipynb
118111
defrun_msg(
119112
msgid:str=None,# id of message to execute
@@ -148,19 +141,27 @@ def add_msg(
148141
time_run=time_run,is_exported=is_exported,skipped=skipped,pinned=pinned,
149142
i_collapsed=i_collapsed,o_collapsed=o_collapsed,heading_collapsed=heading_collapsed)
150143

144+
# %% ../nbs/00_core.ipynb
145+
defdel_msg(
146+
msgid:str=None,# id of message to delete
147+
dname:str=''# Running dialog to get info for; defaults to current dialog
148+
):
149+
"Delete a message from the dialog."
150+
call_endp('rm_msg_',dname,raiseex=True,msid=msgid)
151+
151152
# %% ../nbs/00_core.ipynb
152153
@delegates(add_msg)
153154
def_add_msg_unsafe(
154155
content:str,# Content of the message (i.e the message prompt, code, or note text)
155156
placement:str='add_after',# Can be 'add_after', 'add_before', 'at_start', 'at_end'
156157
msgid:str=None,# id of message that placement is relative to (if None, uses current message)
157158
run:bool=False,# For prompts, send it to the AI; for code, execute it (*DANGEROUS -- be careful of what you run!)
159+
dname:str='',# Running dialog to get info for; defaults to current dialog
158160
**kwargs
159161
):
160162
"""Add/update a message to the queue to show after code execution completes, and optionally run it. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
161163
*WARNING*--This can execute arbitrary code, so check carefully what you run!--*WARNING"""
162164
ifplacementnotin ('at_start','at_end')andnotmsgid:msgid=find_msg_id()
163-
dname=kwargs.pop('dname')
164165
returncall_endp(
165166
'add_relative_',dname,content=content,placement=placement,msgid=msgid,run=run,**kwargs)
166167

@@ -201,6 +202,25 @@ def url2note(
201202
res=read_url(url,as_md=True,extract_section=extract_section,selector=selector)
202203
returnadd_msg(res)
203204

205+
# %% ../nbs/00_core.ipynb
206+
defast_py(code:str):
207+
"Get an SgRoot root node for python `code`"
208+
fromast_grep_pyimportSgRoot
209+
returnSgRoot(code,"python").root()
210+
211+
# %% ../nbs/00_core.ipynb
212+
defast_grep(
213+
pattern:str,# ast-grep pattern to search
214+
path=".",# path to recursively search for files
215+
lang="python"# language to search/scan
216+
):
217+
"Use the `ast-grep` command to find `pattern` in `path`"
218+
importjson,subprocess
219+
cmd=f"ast-grep --pattern '{pattern}' --lang{lang} --json=compact"
220+
ifpath!=".":cmd=f"cd{path} &&{cmd}"
221+
res=subprocess.run(cmd,shell=True,capture_output=True,text=True)
222+
returnjson.loads(res.stdout)ifres.stdoutelseres.stderr
223+
204224
# %% ../nbs/00_core.ipynb
205225
defload_gist(gist_id:str):
206226
"Retrieve a gist"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp