Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
my2to3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
nexedi
my2to3
Commits
d92b99eb
Commit
d92b99eb
authored
Jul 01, 2020
by
Bryton Lacquement
🚪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New helper: find_pattern.py
parent
62fdf45e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
0 deletions
+98
-0
helpers/find_pattern.py
helpers/find_pattern.py
+98
-0
No files found.
helpers/find_pattern.py
0 → 100644
View file @
d92b99eb
#!/usr/bin/env python
#http://svn.python.org/projects/sandbox/trunk/2to3/scripts/find_pattern.py
"""Script that makes determining PATTERN for a new fix much easier.
Figuring out exactly what PATTERN I want for a given fixer class is
getting tedious. This script will step through each possible subtree
for a given string, allowing you to select which one you want. It will
then try to figure out an appropriate pattern to match that tree. This
pattern will require some editing (it will be overly restrictive) but
should provide a solid base to work with and handle the tricky parts.
Usage:
python find_pattern.py "g.throw(E, V, T)"
This will step through each subtree in the parse. To reject a
candidate subtree, hit enter; to accept a candidate, hit "y" and
enter. The pattern will be spit out to stdout.
For example, the above will yield a succession of possible snippets,
skipping all leaf-only trees. I accept
'g.throw(E, V, T)'
This causes find_pattern to spit out
power< 'g' trailer< '.' 'throw' >
trailer< '(' arglist< 'E' ',' 'V' ',' 'T' > ')' > >
Some minor tweaks later, I'm left with
power< any trailer< '.' 'throw' >
trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' > >
which is exactly what I was after.
Larger snippets can be placed in a file (as opposed to a command-line
arg) and processed with the -f option.
"""
__author__
=
"Collin Winter <collinw@gmail.com>"
# Python imports
import
optparse
import
sys
from
StringIO
import
StringIO
# Local imports
from
lib2to3
import
pytree
from
lib2to3.pgen2
import
driver
from
lib2to3.pygram
import
python_symbols
,
python_grammar
driver
=
driver
.
Driver
(
python_grammar
,
convert
=
pytree
.
convert
)
def
main
(
args
):
parser
=
optparse
.
OptionParser
(
usage
=
"find_pattern.py [options] [string]"
)
parser
.
add_option
(
"-f"
,
"--file"
,
action
=
"store"
,
help
=
"Read a code snippet from the specified file"
)
# Parse command line arguments
options
,
args
=
parser
.
parse_args
(
args
)
if
options
.
file
:
tree
=
driver
.
parse_file
(
options
.
file
)
elif
len
(
args
)
>
1
:
tree
=
driver
.
parse_stream
(
StringIO
(
args
[
1
]
+
"
\
n
"
))
else
:
print
>>
sys
.
stderr
,
"You must specify an input file or an input string"
return
1
examine_tree
(
tree
)
return
0
def
examine_tree
(
tree
):
for
node
in
tree
.
post_order
():
if
isinstance
(
node
,
pytree
.
Leaf
):
continue
print
repr
(
str
(
node
))
verdict
=
raw_input
()
if
verdict
.
strip
():
print
find_pattern
(
node
)
return
def
find_pattern
(
node
):
if
isinstance
(
node
,
pytree
.
Leaf
):
return
repr
(
node
.
value
)
return
find_symbol
(
node
.
type
)
+
\
"< "
+
" "
.
join
(
find_pattern
(
n
)
for
n
in
node
.
children
)
+
" >"
def
find_symbol
(
sym
):
for
n
,
v
in
python_symbols
.
__dict__
.
items
():
if
v
==
sym
:
return
n
if
__name__
==
"__main__"
:
sys
.
exit
(
main
(
sys
.
argv
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment