Skip to content

Commit 5c22f83

Browse files
committed
Improved the choices
1 parent 069cbd3 commit 5c22f83

3 files changed

Lines changed: 75 additions & 18 deletions

File tree

partial_rendering.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class PartialRender:
1111
code_change: bool = False
1212

1313

14+
@dataclass
15+
class PartialRenderChoice:
16+
module: PlainModule | None = None
17+
frid: str | None = None
18+
msg: str | None = None
19+
20+
1421
def spec_change(plain_module: PlainModule) -> bool:
1522
if len(plain_module.required_modules) == 0:
1623
return plain_module if plain_module.has_plain_spec_changed() else None

plain2code.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import plain_spec
2020
from event_bus import EventBus
2121
from module_renderer import ModuleRenderer
22-
from partial_rendering import PartialRender, detect_partial_rendering
22+
from partial_rendering import PartialRender, PartialRenderChoice, detect_partial_rendering
2323
from plain2code_arguments import parse_arguments
2424
from plain2code_console import console
2525
from plain2code_events import RenderFailed
@@ -200,12 +200,18 @@ def _check_connection(codeplainAPI: codeplain_api.CodeplainAPI):
200200
)
201201

202202

203-
def get_partial_render_choice(plain_module: plain_modules.PlainModule, partial_render: PartialRender | None) -> str:
203+
def get_partial_render_choice(
204+
plain_module: plain_modules.PlainModule, partial_render: PartialRender | None
205+
) -> PartialRenderChoice:
206+
choices = dict[str, PartialRenderChoice | None]()
204207
print("--- Partial render detected ---")
205208
print(f"Target module: {plain_module.module_name}")
206-
print(f"Partially rendered module: {partial_render.module.module_name}")
207-
if partial_render.frid is not None:
209+
if partial_render.module.is_module_fully_rendered():
210+
print(f"Last fully rendered module: {partial_render.module.module_name}")
211+
else:
212+
print(f"Partially rendered module: {partial_render.module.module_name}")
208213
print(f"Last fully rendered FRID: {partial_render.frid}")
214+
209215
if partial_render.spec_change:
210216
print("Spec change: Yes")
211217
else:
@@ -216,30 +222,44 @@ def get_partial_render_choice(plain_module: plain_modules.PlainModule, partial_r
216222
else:
217223
print("Code change: No")
218224

219-
choice_idx = 1
220-
options = {f"{choice_idx}": "Re-render all"}
225+
choice_idx = 0
226+
first_module = plain_module.all_required_modules[0]
227+
choices[f"{choice_idx}"] = PartialRenderChoice(
228+
module=first_module,
229+
frid=None,
230+
msg=f"Re-render all (start from first module: {first_module.module_name})",
231+
)
221232
choice_idx += 1
222233
if partial_render.frid is not None:
223-
options[f"{choice_idx}"] = (
224-
f"Continue from FRID {partial_render.frid} of module {partial_render.module.module_name}"
234+
next_frid, next_module = plain_module.get_next_frid(partial_render.frid, partial_render.module.module_name)
235+
choices[f"{choice_idx}"] = PartialRenderChoice(
236+
module=next_module,
237+
frid=next_frid,
238+
msg=f"Continue from FRID {next_frid} of module {next_module.module_name}",
225239
)
226240
choice_idx += 1
227241

228242
if partial_render.spec_change:
229-
options[f"{choice_idx}"] = f"Re-render {partial_render.module.module_name} from start"
243+
choices[f"{choice_idx}"] = PartialRenderChoice(
244+
module=partial_render.module, frid=None, msg=f"Re-render {partial_render.module.module_name} from start"
245+
)
230246
choice_idx += 1
231247

232248
if partial_render.code_change:
233-
options[f"{choice_idx}"] = f"Re-render {partial_render.module.module_name} from start"
249+
choices[f"{choice_idx}"] = PartialRenderChoice(
250+
module=partial_render.module, frid=None, msg=f"Re-render {partial_render.module.module_name} from start"
251+
)
234252
choice_idx += 1
235253

236-
for key, value in options.items():
237-
print(f"{key}. {value}")
254+
choices[f"{choice_idx}"] = PartialRenderChoice(module=None, frid=None, msg="Quit")
255+
for key, pr_choice in choices.items():
256+
print(f"{key}. {pr_choice.msg}")
238257

239258
while True:
240259
selection = input("\nSelect an option: ").strip()
241-
if selection in options:
242-
return options[selection]
260+
if selection in choices:
261+
return choices[selection]
262+
243263
print("Invalid choice. Please try again.")
244264

245265

@@ -272,12 +292,10 @@ def render(args, run_state: RunState, event_bus: EventBus): # noqa: C901
272292
)
273293

274294
partial_render = detect_partial_rendering(plain_module)
275-
print("Partial render: ", partial_render)
276295
if partial_render is not None:
277296
choice = get_partial_render_choice(plain_module, partial_render)
278-
print(f"You selected: {choice}")
279-
280-
exit()
297+
if choice.module is None and choice.frid is None and choice.msg == "Quit":
298+
exit()
281299

282300
module_renderer = ModuleRenderer(
283301
codeplainAPI,

plain_modules.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,35 @@ def ensure_previous_frid_commits_exist(self, render_range: list[str], render_con
270270
# Verify commits exist for all previous FRIDs
271271
for prev_frid in previous_frids:
272272
self._ensure_frid_commit_exists(prev_frid, first_render_frid, render_conformance_tests)
273+
274+
def get_module_by_name(self, module_name: str) -> PlainModule:
275+
for module in self.all_required_modules:
276+
if module.module_name == module_name:
277+
return module
278+
279+
raise ModuleDoesNotExistError(f"Module {module_name} does not exist")
280+
281+
def get_next_module(self, module_name: str) -> PlainModule | None:
282+
for idx, module in enumerate(self.all_required_modules):
283+
if module.module_name == module_name and idx < len(self.all_required_modules) - 1:
284+
return self.all_required_modules[idx + 1]
285+
286+
return None
287+
288+
def get_next_frid(self, frid: str, module_name: str) -> tuple[str, PlainModule]:
289+
module = self.get_module_by_name(module_name)
290+
next_frid = plain_spec.get_next_frid(module.plain_source, frid)
291+
292+
if next_frid is None:
293+
next_module = self.get_next_module(module_name)
294+
return plain_spec.get_first_frid(next_module.plain_source), next_module
295+
296+
return next_frid, module
297+
298+
def is_module_fully_rendered(self) -> bool:
299+
frids = list(plain_spec.get_frids(self.plain_source))
300+
last_rendered_module, last_rendered_frid = git_utils.get_last_finished_frid(self.module_build_folder)
301+
if last_rendered_module is None or last_rendered_frid is None:
302+
return False
303+
304+
return last_rendered_frid == frids[-1]

0 commit comments

Comments
 (0)