Skip to content

Commit

Permalink
chore: simplify ast.Node build
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jan 3, 2025
1 parent 702f3b0 commit 397390f
Showing 1 changed file with 5 additions and 47 deletions.
52 changes: 5 additions & 47 deletions launart/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,6 @@
from launart.service import Service


def _dummy_blocking(lineno: int, indent: int) -> ast.AsyncWith:
return ast.AsyncWith(
items=[
ast.withitem(
context_expr=ast.Call(
func=ast.Attribute(
value=ast.Name(
id="self",
ctx=ast.Load(),
lineno=lineno,
col_offset=19 + indent,
end_lineno=lineno,
end_col_offset=23 + indent,
),
attr="stage",
ctx=ast.Load(),
lineno=lineno,
col_offset=19 + indent,
end_lineno=lineno,
end_col_offset=29 + indent,
),
args=[
ast.Constant(
value="blocking",
lineno=lineno,
col_offset=30 + indent,
end_lineno=lineno,
end_col_offset=40 + indent,
)
],
keywords=[],
lineno=lineno,
col_offset=19 + indent,
end_lineno=lineno,
end_col_offset=41 + indent,
),
)
],
body=[ast.Pass(lineno=lineno + 1, col_offset=12 + indent, end_lineno=lineno + 1, end_col_offset=16 + indent)],
lineno=lineno,
col_offset=8 + indent,
end_lineno=lineno + 1,
end_col_offset=16 + indent,
)


def patch_launch(serv: Service) -> Callable[[Launart], Awaitable[None]]:
if serv.stages == {"preparing", "blocking", "cleanup"}:
return serv.launch
Expand Down Expand Up @@ -137,7 +91,11 @@ async def _launch(manager: Launart):
break
else:
raise ValueError("this component has no stage method.")
new = _dummy_blocking(_node.lineno + 1, node.col_offset - 4)

new = ast.parse("async with self.stage('blocking'):\n pass").body[0]
new.lineno = _node.lineno + 1
new.col_offset = node.col_offset + 4

for other in node.body[index:]:
for n in ast.walk(other):
if hasattr(n, "lineno"):
Expand Down

0 comments on commit 397390f

Please sign in to comment.