Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Klöckner
pytools
Commits
70e7da8b
"examples/wave/wave-min-mpi.py" did not exist on "b1eb9add18000cd1678603cda35689a3097170fa"
Commit
70e7da8b
authored
9 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Make it easier to incorporate longer code blocks with py_codegen
parent
ee4b7de9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pytools/py_codegen.py
+34
-6
34 additions, 6 deletions
pytools/py_codegen.py
with
34 additions
and
6 deletions
pytools/py_codegen.py
+
34
−
6
View file @
70e7da8b
...
...
@@ -44,12 +44,8 @@ class PythonCodeGenerator(object):
self
.
level
=
0
def
extend
(
self
,
sub_generator
):
self
.
code
.
extend
(
sub_generator
.
code
)
def
extend_indent
(
self
,
sub_generator
):
with
Indentation
(
self
):
for
line
in
sub_generator
.
code
:
self
.
write
(
line
)
for
line
in
sub_generator
.
code
:
self
.
code
.
append
(
"
"
*
(
4
*
self
.
level
)
+
line
)
def
get
(
self
):
result
=
"
\n
"
.
join
(
self
.
code
)
...
...
@@ -64,6 +60,9 @@ class PythonCodeGenerator(object):
if
not
s
.
strip
():
self
.
code
.
append
(
""
)
else
:
if
"
\n
"
in
s
:
s
=
remove_common_indentation
(
s
)
for
l
in
s
.
split
(
"
\n
"
):
self
.
code
.
append
(
"
"
*
(
4
*
self
.
level
)
+
l
)
...
...
@@ -92,3 +91,32 @@ class PythonFunctionGenerator(PythonCodeGenerator):
func
=
result_dict
[
self
.
name
]
result_dict
[
"
_MODULE_SOURCE_CODE
"
]
=
source_text
return
func
# {{{ remove common indentation
def
remove_common_indentation
(
code
,
require_leading_newline
=
True
):
if
"
\n
"
not
in
code
:
return
code
if
require_leading_newline
and
not
code
.
startswith
(
"
\n
"
):
return
code
lines
=
code
.
split
(
"
\n
"
)
while
lines
[
0
].
strip
()
==
""
:
lines
.
pop
(
0
)
while
lines
[
-
1
].
strip
()
==
""
:
lines
.
pop
(
-
1
)
if
lines
:
base_indent
=
0
while
lines
[
0
][
base_indent
]
in
"
\t
"
:
base_indent
+=
1
for
line
in
lines
[
1
:]:
if
line
[:
base_indent
].
strip
():
raise
ValueError
(
"
inconsistent indentation
"
)
return
"
\n
"
.
join
(
line
[
base_indent
:]
for
line
in
lines
)
# }}}
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