Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
A
astor
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
astor
Commits
bf780926
Kaydet (Commit)
bf780926
authored
May 19, 2015
tarafından
Patrick Maupin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Stripping functions that don't belong.
THIS REVISION WILL NOT WORK YET!
üst
12cdde63
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
275 deletions
+21
-275
file_util.py
astor/file_util.py
+0
-148
node_util.py
astor/node_util.py
+0
-127
tree_walk.py
astor/tree_walk.py
+21
-0
No files found.
astor/file_util.py
Dosyayı görüntüle @
bf780926
...
...
@@ -13,154 +13,6 @@ import ast
import
sys
class
NonExistent
(
object
):
pass
def
iter_node
(
node
,
name
=
''
,
list
=
list
,
getattr
=
getattr
,
isinstance
=
isinstance
,
enumerate
=
enumerate
,
missing
=
NonExistent
):
"""Iterates over an object:
- If the object has a _fields attribute,
it gets attributes in the order of this
and returns name, value pairs.
- Otherwise, if the object is a list instance,
it returns name, value pairs for each item
in the list, where the name is passed into
this function (defaults to blank).
"""
fields
=
getattr
(
node
,
'_fields'
,
None
)
if
fields
is
not
None
:
for
name
in
fields
:
value
=
getattr
(
node
,
name
,
missing
)
if
value
is
not
missing
:
yield
value
,
name
elif
isinstance
(
node
,
list
):
for
value
in
node
:
yield
value
,
name
def
dump
(
node
,
name
=
None
,
initial_indent
=
''
,
indentation
=
' '
,
maxline
=
120
,
maxmerged
=
80
,
iter_node
=
iter_node
,
special
=
ast
.
AST
,
list
=
list
,
isinstance
=
isinstance
,
type
=
type
,
len
=
len
):
"""Dumps an AST or similar structure:
- Pretty-prints with indentation
- Doesn't print line/column/ctx info
"""
def
dump
(
node
,
name
=
None
,
indent
=
''
):
level
=
indent
+
indentation
name
=
name
and
name
+
'='
or
''
values
=
list
(
iter_node
(
node
))
if
isinstance
(
node
,
list
):
prefix
,
suffix
=
'
%
s['
%
name
,
']'
elif
values
:
prefix
,
suffix
=
'
%
s
%
s('
%
(
name
,
type
(
node
)
.
__name__
),
')'
elif
isinstance
(
node
,
special
):
prefix
,
suffix
=
name
+
type
(
node
)
.
__name__
,
''
else
:
return
'
%
s
%
s'
%
(
name
,
repr
(
node
))
node
=
[
dump
(
a
,
b
,
level
)
for
a
,
b
in
values
if
b
!=
'ctx'
]
oneline
=
'
%
s
%
s
%
s'
%
(
prefix
,
', '
.
join
(
node
),
suffix
)
if
len
(
oneline
)
+
len
(
indent
)
<
maxline
:
return
'
%
s'
%
oneline
if
node
and
len
(
prefix
)
+
len
(
node
[
0
])
<
maxmerged
:
prefix
=
'
%
s
%
s,'
%
(
prefix
,
node
.
pop
(
0
))
node
=
(
',
\n
%
s'
%
level
)
.
join
(
node
)
.
lstrip
()
return
'
%
s
\n
%
s
%
s
%
s'
%
(
prefix
,
level
,
node
,
suffix
)
return
dump
(
node
,
name
,
initial_indent
)
class
MetaFlatten
(
type
):
"""This metaclass is used to flatten classes to remove
class hierarchy.
This makes it easier to manipulate classes (find
attributes in a single dict, etc.)
"""
def
__new__
(
clstype
,
name
,
bases
,
clsdict
):
newbases
=
(
object
,)
newdict
=
{}
for
base
in
reversed
(
bases
):
if
base
not
in
newbases
:
newdict
.
update
(
vars
(
base
))
newdict
.
update
(
clsdict
)
# Delegate the real work to type
return
type
.
__new__
(
clstype
,
name
,
newbases
,
newdict
)
MetaFlatten
=
MetaFlatten
(
'MetaFlatten'
,
(
object
,
),
{})
def
_getsymbol
(
mapping
,
map_dict
=
None
,
type
=
type
):
"""This function returns a closure that will map a
class type to its corresponding symbol, by looking
up the class name of an object.
"""
if
isinstance
(
mapping
,
str
):
mapping
=
mapping
.
split
()
mapping
=
list
(
zip
(
mapping
[
0
::
2
],
(
x
.
replace
(
'_'
,
' '
)
for
x
in
mapping
[
1
::
2
])))
mapping
=
dict
(((
getattr
(
ast
,
x
),
y
)
for
x
,
y
in
mapping
))
if
map_dict
is
not
None
:
map_dict
.
update
(
mapping
)
def
getsymbol
(
obj
,
fmt
=
'
%
s'
):
return
fmt
%
mapping
[
type
(
obj
)]
return
getsymbol
all_symbols
=
{}
get_boolop
=
_getsymbol
(
"""
And and Or or
"""
,
all_symbols
)
binops
=
"""
Add + Mult * LShift << BitAnd &
Sub - Div / RShift >> BitOr |
Mod
%
BitXor ^
FloorDiv //
Pow **
"""
if
sys
.
version_info
>=
(
3
,
5
):
binops
+=
"MatMult @"
get_binop
=
_getsymbol
(
binops
,
all_symbols
)
get_cmpop
=
_getsymbol
(
"""
Eq == Gt > GtE >= In in Is is
NotEq != Lt < LtE <= NotIn not_in IsNot is_not
"""
,
all_symbols
)
get_unaryop
=
_getsymbol
(
"""
UAdd + USub - Invert ~ Not not
"""
,
all_symbols
)
get_anyop
=
_getsymbol
(
all_symbols
)
class
ExplicitNodeVisitor
(
ast
.
NodeVisitor
):
"""This expands on the ast module's NodeVisitor class
to remove any implicit visits.
"""
def
abort_visit
(
node
):
# XXX: self?
msg
=
'No defined handler for node of type
%
s'
raise
AttributeError
(
msg
%
node
.
__class__
.
__name__
)
def
visit
(
self
,
node
,
abort
=
abort_visit
):
"""Visit a node."""
method
=
'visit_'
+
node
.
__class__
.
__name__
visitor
=
getattr
(
self
,
method
,
abort
)
return
visitor
(
node
)
def
parsefile
(
fname
):
with
open
(
fname
,
'r'
)
as
f
:
fstr
=
f
.
read
()
...
...
astor/node_util.py
Dosyayı görüntüle @
bf780926
...
...
@@ -74,130 +74,3 @@ def dump(node, name=None, initial_indent='', indentation=' ',
return
dump
(
node
,
name
,
initial_indent
)
class
MetaFlatten
(
type
):
"""This metaclass is used to flatten classes to remove
class hierarchy.
This makes it easier to manipulate classes (find
attributes in a single dict, etc.)
"""
def
__new__
(
clstype
,
name
,
bases
,
clsdict
):
newbases
=
(
object
,)
newdict
=
{}
for
base
in
reversed
(
bases
):
if
base
not
in
newbases
:
newdict
.
update
(
vars
(
base
))
newdict
.
update
(
clsdict
)
# Delegate the real work to type
return
type
.
__new__
(
clstype
,
name
,
newbases
,
newdict
)
MetaFlatten
=
MetaFlatten
(
'MetaFlatten'
,
(
object
,
),
{})
def
_getsymbol
(
mapping
,
map_dict
=
None
,
type
=
type
):
"""This function returns a closure that will map a
class type to its corresponding symbol, by looking
up the class name of an object.
"""
if
isinstance
(
mapping
,
str
):
mapping
=
mapping
.
split
()
mapping
=
list
(
zip
(
mapping
[
0
::
2
],
(
x
.
replace
(
'_'
,
' '
)
for
x
in
mapping
[
1
::
2
])))
mapping
=
dict
(((
getattr
(
ast
,
x
),
y
)
for
x
,
y
in
mapping
))
if
map_dict
is
not
None
:
map_dict
.
update
(
mapping
)
def
getsymbol
(
obj
,
fmt
=
'
%
s'
):
return
fmt
%
mapping
[
type
(
obj
)]
return
getsymbol
all_symbols
=
{}
get_boolop
=
_getsymbol
(
"""
And and Or or
"""
,
all_symbols
)
binops
=
"""
Add + Mult * LShift << BitAnd &
Sub - Div / RShift >> BitOr |
Mod
%
BitXor ^
FloorDiv //
Pow **
"""
if
sys
.
version_info
>=
(
3
,
5
):
binops
+=
"MatMult @"
get_binop
=
_getsymbol
(
binops
,
all_symbols
)
get_cmpop
=
_getsymbol
(
"""
Eq == Gt > GtE >= In in Is is
NotEq != Lt < LtE <= NotIn not_in IsNot is_not
"""
,
all_symbols
)
get_unaryop
=
_getsymbol
(
"""
UAdd + USub - Invert ~ Not not
"""
,
all_symbols
)
get_anyop
=
_getsymbol
(
all_symbols
)
class
ExplicitNodeVisitor
(
ast
.
NodeVisitor
):
"""This expands on the ast module's NodeVisitor class
to remove any implicit visits.
"""
def
abort_visit
(
node
):
# XXX: self?
msg
=
'No defined handler for node of type
%
s'
raise
AttributeError
(
msg
%
node
.
__class__
.
__name__
)
def
visit
(
self
,
node
,
abort
=
abort_visit
):
"""Visit a node."""
method
=
'visit_'
+
node
.
__class__
.
__name__
visitor
=
getattr
(
self
,
method
,
abort
)
return
visitor
(
node
)
def
parsefile
(
fname
):
with
open
(
fname
,
'r'
)
as
f
:
fstr
=
f
.
read
()
fstr
=
fstr
.
replace
(
'
\r\n
'
,
'
\n
'
)
.
replace
(
'
\r
'
,
'
\n
'
)
if
not
fstr
.
endswith
(
'
\n
'
):
fstr
+=
'
\n
'
return
ast
.
parse
(
fstr
,
filename
=
fname
)
class
CodeToAst
(
object
):
"""Given a module, or a function that was compiled as part
of a module, re-compile the module into an AST and extract
the sub-AST for the function. Allow caching to reduce
number of compiles.
"""
def
__init__
(
self
,
cache
=
None
):
self
.
cache
=
cache
or
{}
def
__call__
(
self
,
codeobj
):
cache
=
self
.
cache
fname
=
getattr
(
codeobj
,
'__file__'
,
None
)
if
fname
is
None
:
func_code
=
codeobj
.
__code__
fname
=
func_code
.
co_filename
linenum
=
func_code
.
co_firstlineno
key
=
fname
,
linenum
else
:
fname
=
key
=
fname
.
replace
(
'.pyc'
,
'.py'
)
result
=
cache
.
get
(
key
)
if
result
is
not
None
:
return
result
cache
[
fname
]
=
mod_ast
=
parsefile
(
fname
)
for
obj
in
mod_ast
.
body
:
if
not
isinstance
(
obj
,
ast
.
FunctionDef
):
continue
cache
[(
fname
,
obj
.
lineno
)]
=
obj
return
cache
[
key
]
codetoast
=
CodeToAst
()
astor/tree_walk.py
Dosyayı görüntüle @
bf780926
...
...
@@ -12,6 +12,27 @@ Copyright 2013 (c) Berker Peksag
from
.misc
import
MetaFlatten
,
iter_node
class
MetaFlatten
(
type
):
"""This metaclass is used to flatten classes to remove
class hierarchy.
This makes it easier to manipulate classes (find
attributes in a single dict, etc.)
"""
def
__new__
(
clstype
,
name
,
bases
,
clsdict
):
newbases
=
(
object
,)
newdict
=
{}
for
base
in
reversed
(
bases
):
if
base
not
in
newbases
:
newdict
.
update
(
vars
(
base
))
newdict
.
update
(
clsdict
)
# Delegate the real work to type
return
type
.
__new__
(
clstype
,
name
,
newbases
,
newdict
)
MetaFlatten
=
MetaFlatten
(
'MetaFlatten'
,
(
object
,
),
{})
class
TreeWalk
(
MetaFlatten
):
"""The TreeWalk class can be used as a superclass in order
to walk an AST or similar tree.
...
...
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