Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aerobs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Gattringer
aerobs
Commits
37c1d10f
Commit
37c1d10f
authored
1 year ago
by
Andreas Gattringer
Browse files
Options
Downloads
Patches
Plain Diff
imports: also handle imports in top-level try...except blocks
parent
73df413f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/helpers/imports.py
+21
-4
21 additions, 4 deletions
scripts/helpers/imports.py
with
21 additions
and
4 deletions
scripts/helpers/imports.py
+
21
−
4
View file @
37c1d10f
...
...
@@ -7,6 +7,25 @@ def get_module_names(node: ast.Import):
return
names
def
import_names
(
node
):
result
=
[]
if
isinstance
(
node
,
ast
.
Import
):
print
(
"
\t
"
,
"
,
"
.
join
(
get_module_names
(
node
)))
result
.
extend
(
get_module_names
(
node
))
elif
isinstance
(
node
,
ast
.
ImportFrom
):
print
(
"
\t
"
,
node
.
module
)
result
.
append
(
node
.
module
)
if
isinstance
(
node
,
ast
.
Try
):
for
child
in
ast
.
iter_child_nodes
(
node
):
result
.
extend
(
import_names
(
child
))
for
handler
in
node
.
handlers
:
for
child
in
ast
.
iter_child_nodes
(
handler
):
result
.
extend
(
import_names
(
child
))
return
result
def
get_imported_files
(
fn
,
modules
=
None
,
...
...
@@ -22,12 +41,10 @@ def get_imported_files(
current_modules
=
[]
with
open
(
fn
)
as
f
:
print
(
f
"
Reading
{
fn
}
"
)
root
=
ast
.
parse
(
f
.
read
(),
fn
)
for
node
in
ast
.
iter_child_nodes
(
root
):
if
isinstance
(
node
,
ast
.
Import
):
current_modules
.
extend
(
get_module_names
(
node
))
elif
isinstance
(
node
,
ast
.
ImportFrom
):
current_modules
.
append
(
node
.
module
)
current_modules
.
extend
(
import_names
(
node
))
current_modules
=
sorted
(
list
(
set
(
current_modules
)))
for
module
in
current_modules
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment