2626 def generate(gen: protogen.Plugin):
2727 for f in gen.files_to_generate:
2828 g = gen.new_generated_file(
29- f.proto.name.replace(".proto", ".py"),
29+ f.proto.name.replace(".proto", ".py"),
3030 f.py_import_path,
3131 )
3232 g.P("# Generated code ahead.")
@@ -49,10 +49,11 @@ def generate(gen: protogen.Plugin):
4949
5050"""
5151
52+ import contextlib
5253import enum
5354import keyword
5455import sys
55- from typing import BinaryIO , Callable , Dict , List , Optional , Set , Tuple
56+ from typing import BinaryIO , Callable , Dict , List , Iterator , Optional , Set , Tuple
5657from operator import ior
5758from functools import reduce
5859
@@ -1483,6 +1484,34 @@ def set_indent(self, level: int) -> int:
14831484 self ._indent = level
14841485 return old
14851486
1487+ @contextlib .contextmanager
1488+ def indent (self , level : int = 4 ) -> Iterator [None ]:
1489+ """Increments the indentation within a contextmanager block.
1490+
1491+ Increases the indentation level for all calls to :func:`P` within a
1492+ contextmanager block. When the enclosed block exits, the indentation
1493+ level will be restored to its prior level.
1494+
1495+ Arguments
1496+ ---------
1497+ level : int
1498+ The amount to increment the indentation level by.
1499+
1500+ Example
1501+ -------
1502+ >>> g.P("class Myclass:")
1503+ >>> with g.indent():
1504+ ... g.P("def __init__():")
1505+ ... with g.indent():
1506+ ... g.P("pass")
1507+ """
1508+ old = self ._indent
1509+ self ._indent += level
1510+ try :
1511+ yield
1512+ finally :
1513+ self ._indent = old
1514+
14861515 def P (self , * args ):
14871516 """Add a new line to the output buffer.
14881517
0 commit comments