codeplain 0.3.12.dev27__py3-none-any.whl → 0.3.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
plain_spec.py CHANGED
@@ -1,403 +1,23 @@
1
- import hashlib
2
- import json
3
- import uuid
4
- from typing import Optional
1
+ """Compatibility shim: the parser now lives in the ``plain-parser`` package.
5
2
 
6
- from liquid2.filter import with_context
3
+ The render-range helpers below stay here because they read codeplain's CLI
4
+ arguments (``--render-range`` / ``--render-from``); the FRID slicing they wrap
5
+ is ``plain_parser.plain_spec.get_frids_range``.
6
+ """
7
7
 
8
- from plain2code_exceptions import InvalidFridArgument, InvalidLiquidVariableName
9
-
10
- DEFINITIONS = "definitions"
11
- NON_FUNCTIONAL_REQUIREMENTS = "implementation reqs"
12
- TEST_REQUIREMENTS = "test reqs"
13
- FUNCTIONAL_REQUIREMENTS = "functional specs"
14
- ACCEPTANCE_TESTS = "acceptance_tests"
15
- ACCEPTANCE_TEST_HEADING = "acceptance tests"
16
-
17
- ALLOWED_SPECIFICATION_HEADINGS = [
18
- DEFINITIONS,
19
- NON_FUNCTIONAL_REQUIREMENTS,
20
- TEST_REQUIREMENTS,
21
- FUNCTIONAL_REQUIREMENTS,
22
- ACCEPTANCE_TEST_HEADING,
23
- ]
24
-
25
- ALLOWED_IMPORT_SPECIFICATION_HEADINGS = [DEFINITIONS, NON_FUNCTIONAL_REQUIREMENTS, TEST_REQUIREMENTS]
26
-
27
-
28
- def has_acceptance_tests(plain_source_tree) -> bool:
29
- """Return True if any functional requirement in the plain source defines acceptance tests."""
30
- functional_requirements = plain_source_tree.get(FUNCTIONAL_REQUIREMENTS) or []
31
- for functional_requirement in functional_requirements:
32
- if functional_requirement.get(ACCEPTANCE_TESTS):
33
- return True
34
- return False
35
-
36
-
37
- def collect_specification_linked_resources(specification, specification_heading, linked_resources_list):
38
- linked_resources = []
39
- if "linked_resources" in specification:
40
- linked_resources.extend(specification["linked_resources"])
41
-
42
- for resource in linked_resources:
43
- resource_found = False
44
- for resource_map in linked_resources_list:
45
- if resource["text"] == resource_map["text"]:
46
- if resource["target"] != resource_map["target"]:
47
- raise Exception(
48
- f"The file {resource['target']} is linked to multiple linked resources with the same text: {resource['text']}"
49
- )
50
- elif resource["target"] == resource_map["target"]:
51
- if resource["text"] != resource_map["text"]:
52
- raise Exception(
53
- f"The linked resource {resource['text']} is linked to multiple files: {resource_map['target']}"
54
- )
55
- else:
56
- continue
57
-
58
- if resource_found:
59
- raise Exception(
60
- "Duplicate linked resource found: " + resource["text"] + " (" + resource["target"] + ")"
61
- )
62
-
63
- resource_found = True
64
- resource_map["sections"].append(specification_heading)
65
-
66
- if not resource_found:
67
- linked_resources_list.append(
68
- {"text": resource["text"], "target": resource["target"], "sections": [specification_heading]}
69
- )
70
-
71
-
72
- def collect_linked_resources_in_section(
73
- section, linked_resources_list, specifications_list, include_acceptance_tests, frid=Optional[str]
74
- ):
75
- # When should we collect specification headings in the current section. Either when one of the following holds true:
76
- # - frid wasn't specified
77
- # - section has no ID (and that's exactly when it's the root section)
78
- # - section has ID, frid was specified and the specified frid inside the current section tree
79
- should_collect_specification_headings_in_current_section = (
80
- frid is None or "ID" not in section or frid.startswith(section["ID"])
81
- )
82
- if should_collect_specification_headings_in_current_section:
83
- specifications_to_collect = list(set([DEFINITIONS, NON_FUNCTIONAL_REQUIREMENTS, TEST_REQUIREMENTS]))
84
- if specifications_list:
85
- specifications_to_collect = list(set(specifications_to_collect) & set(specifications_list))
86
- for specification_heading in specifications_to_collect:
87
- if specification_heading in section:
88
- for requirement in section[specification_heading]:
89
- collect_specification_linked_resources(requirement, specification_heading, linked_resources_list)
90
-
91
- if FUNCTIONAL_REQUIREMENTS in section and (
92
- not specifications_list or FUNCTIONAL_REQUIREMENTS in specifications_list
93
- ):
94
- functional_requirement_count = 0
95
- for requirement in section[FUNCTIONAL_REQUIREMENTS]:
96
- collect_specification_linked_resources(requirement, FUNCTIONAL_REQUIREMENTS, linked_resources_list)
97
-
98
- functional_requirement_count += 1
99
- section_id = section.get("ID", None)
100
- current_frid = get_current_frid(section_id, functional_requirement_count)
101
-
102
- if ACCEPTANCE_TESTS in requirement and include_acceptance_tests and (frid is None or frid == current_frid):
103
- for acceptance_test in requirement[ACCEPTANCE_TESTS]:
104
- collect_specification_linked_resources(
105
- acceptance_test, FUNCTIONAL_REQUIREMENTS, linked_resources_list
106
- )
107
-
108
- if frid is not None and current_frid == frid:
109
- # here, we rely on the fact that FRIDs are incrementing. And if we have reached the current FRID, we should
110
- # not collect any FRIDs after the current one.
111
- return True
112
-
113
- if "sections" in section:
114
- for subsection in section["sections"]:
115
- if collect_linked_resources_in_section(
116
- subsection, linked_resources_list, specifications_list, include_acceptance_tests, frid
117
- ):
118
- return True
119
-
120
- return False
121
-
122
-
123
- # TODO: check if this function can be refactored to return the list of linked resources instead of modifying it in place
124
- def collect_linked_resources(
125
- plain_source_tree, linked_resources_list, specifications_list, include_acceptance_tests, frid=None
126
- ):
127
-
128
- if not isinstance(plain_source_tree, dict):
129
- raise ValueError("[plain_source_tree must be a dictionary.")
130
-
131
- if frid is not None:
132
- functional_requirements = get_frids(plain_source_tree)
133
- if frid not in functional_requirements:
134
- raise ValueError(f"frid {frid} does not exist.")
135
-
136
- result = collect_linked_resources_in_section(
137
- plain_source_tree, linked_resources_list, specifications_list, include_acceptance_tests, frid
138
- )
139
-
140
- # Sort linked_resources_list by the "text" field
141
- linked_resources_list.sort(key=lambda x: x["text"])
142
-
143
- return result
144
-
145
-
146
- def get_frids(plain_source_tree):
147
- if FUNCTIONAL_REQUIREMENTS in plain_source_tree:
148
- for functional_requirement_id in range(1, len(plain_source_tree[FUNCTIONAL_REQUIREMENTS]) + 1):
149
- if "ID" in plain_source_tree:
150
- yield plain_source_tree["ID"] + "." + str(functional_requirement_id)
151
- else:
152
- yield str(functional_requirement_id)
153
-
154
- if "sections" in plain_source_tree:
155
- for section in plain_source_tree["sections"]:
156
- yield from get_frids(section)
157
-
158
-
159
- def get_first_frid(plain_source_tree):
160
- return next(get_frids(plain_source_tree), None)
161
-
162
-
163
- def get_current_frid(section_id: Optional[str], functional_requirement_count: int) -> str:
164
- if section_id is None:
165
- return str(functional_requirement_count)
166
- else:
167
- return section_id + "." + str(functional_requirement_count)
168
-
169
-
170
- def get_next_frid(plain_source_tree, frid):
171
- functional_requirements = get_frids(plain_source_tree)
172
- for temp_frid in functional_requirements:
173
- if temp_frid == frid:
174
- return next(functional_requirements, None)
175
-
176
- raise Exception(f"Functionality {frid} does not exist.")
177
-
178
-
179
- def get_previous_frid(plain_source_tree, frid):
180
- previous_frid = None
181
- for temp_frid in get_frids(plain_source_tree):
182
- if temp_frid == frid:
183
- return previous_frid
184
-
185
- previous_frid = temp_frid
186
-
187
- raise Exception(f"Functionality {frid} does not exist.")
188
-
189
-
190
- def get_frids_before(plain_source_tree, target_frid: str) -> list[str]:
191
- """
192
- Get all FRIDs that appear before the target FRID in the specification.
193
-
194
- Args:
195
- plain_source_tree: The plain source tree
196
- target_frid: The FRID to find predecessors for
197
-
198
- Returns:
199
- List of FRIDs that appear before target_frid, in order
200
- """
201
- previous_frids = []
202
- for frid in get_frids(plain_source_tree):
203
- if frid == target_frid:
204
- break
205
- previous_frids.append(frid)
206
-
207
- return previous_frids
208
-
209
-
210
- def get_specification_item_markdown(specification_item, code_variables, replace_code_variables):
211
- markdown = specification_item["markdown"]
212
- if "code_variables" in specification_item:
213
- for code_variable in specification_item["code_variables"]:
214
- if code_variable["name"] in code_variables:
215
- if code_variables[code_variable["name"]] != code_variable["value"]:
216
- raise Exception(
217
- f"Code variable {code_variable['name']} has multiple values: {code_variables[code_variable['name']]} and {code_variable['value']}"
218
- )
219
- else:
220
- code_variables[code_variable["name"]] = code_variable["value"]
221
-
222
- if replace_code_variables:
223
- markdown = markdown.replace(f"{{{{ {code_variable['name']} }}}}", code_variable["value"])
224
-
225
- return markdown
226
-
227
-
228
- def get_specifications_from_plain_source_tree(
229
- frid,
230
- plain_source_tree,
231
- definitions,
232
- non_functional_requirements,
233
- test_requirements,
234
- functional_requirements,
235
- acceptance_tests,
236
- code_variables,
237
- replace_code_variables,
238
- section_id=None,
239
- ):
240
- return_frid = None
241
- if FUNCTIONAL_REQUIREMENTS in plain_source_tree and len(plain_source_tree[FUNCTIONAL_REQUIREMENTS]) > 0:
242
- functional_requirement_count = 0
243
- for functional_requirement in plain_source_tree[FUNCTIONAL_REQUIREMENTS]:
244
- functional_requirement_count += 1
245
- if section_id is None:
246
- current_frid = str(functional_requirement_count)
247
- else:
248
- current_frid = section_id + "." + str(functional_requirement_count)
249
-
250
- functional_requirements.append(
251
- get_specification_item_markdown(functional_requirement, code_variables, replace_code_variables)
252
- )
253
-
254
- if current_frid == frid:
255
- if ACCEPTANCE_TESTS in functional_requirement:
256
- for acceptance_test in functional_requirement[ACCEPTANCE_TESTS]:
257
- acceptance_tests.append(
258
- get_specification_item_markdown(acceptance_test, code_variables, replace_code_variables)
259
- )
260
-
261
- return_frid = current_frid
262
- break
263
-
264
- if "sections" in plain_source_tree:
265
- for section in plain_source_tree["sections"]:
266
- sub_frid = get_specifications_from_plain_source_tree(
267
- frid,
268
- section,
269
- definitions,
270
- non_functional_requirements,
271
- test_requirements,
272
- functional_requirements,
273
- acceptance_tests,
274
- code_variables,
275
- replace_code_variables,
276
- section["ID"],
277
- )
278
- if sub_frid is not None:
279
- return_frid = sub_frid
280
- break
281
-
282
- if return_frid is not None:
283
- if DEFINITIONS in plain_source_tree and plain_source_tree[DEFINITIONS] is not None:
284
- definitions[0:0] = [
285
- get_specification_item_markdown(specification, code_variables, replace_code_variables)
286
- for specification in plain_source_tree[DEFINITIONS]
287
- ]
288
- if (
289
- NON_FUNCTIONAL_REQUIREMENTS in plain_source_tree
290
- and plain_source_tree[NON_FUNCTIONAL_REQUIREMENTS] is not None
291
- ):
292
- non_functional_requirements[0:0] = [
293
- get_specification_item_markdown(specification, code_variables, replace_code_variables)
294
- for specification in plain_source_tree[NON_FUNCTIONAL_REQUIREMENTS]
295
- ]
296
- if TEST_REQUIREMENTS in plain_source_tree and plain_source_tree[TEST_REQUIREMENTS] is not None:
297
- test_requirements[0:0] = [
298
- get_specification_item_markdown(specification, code_variables, replace_code_variables)
299
- for specification in plain_source_tree[TEST_REQUIREMENTS]
300
- ]
301
-
302
- return return_frid
303
-
304
-
305
- def get_specifications_for_frid(plain_source_tree, frid, replace_code_variables=True):
306
- definitions = []
307
- non_functional_requirements = []
308
- test_requirements = []
309
- functional_requirements = []
310
- acceptance_tests = []
311
-
312
- code_variables = {}
313
-
314
- result = get_specifications_from_plain_source_tree(
315
- frid,
316
- plain_source_tree,
317
- definitions,
318
- non_functional_requirements,
319
- test_requirements,
320
- functional_requirements,
321
- acceptance_tests,
322
- code_variables,
323
- replace_code_variables,
324
- )
325
- if result is None:
326
- raise Exception(f"Functionality {frid} does not exist.")
327
-
328
- specifications = {
329
- DEFINITIONS: definitions,
330
- NON_FUNCTIONAL_REQUIREMENTS: non_functional_requirements,
331
- TEST_REQUIREMENTS: test_requirements,
332
- FUNCTIONAL_REQUIREMENTS: functional_requirements,
333
- }
334
-
335
- if acceptance_tests:
336
- specifications[ACCEPTANCE_TESTS] = acceptance_tests
337
-
338
- if code_variables:
339
- return specifications, code_variables
340
- else:
341
- return specifications, None
342
-
343
-
344
- @with_context
345
- def code_variable_liquid_filter(value, *, context):
346
- if len(context.scope) == 0:
347
- raise Exception("Invalid use of code_variable filter!")
348
-
349
- if "code_variables" in context.globals:
350
- code_variables = context.globals["code_variables"]
351
-
352
- variable = next(iter(context.scope.items()))
353
-
354
- unique_str = uuid.uuid4().hex
355
-
356
- code_variables[unique_str] = {variable[0]: value}
357
-
358
- return unique_str
359
- else:
360
-
361
- return value
362
-
363
-
364
- @with_context
365
- def prohibited_chars_liquid_filter(value, prohibited_chars, *, context):
366
- if not isinstance(value, str):
367
- value = str(value)
368
-
369
- if len(context.scope) == 0:
370
- raise Exception("Invalid use of prohibited_chars filter!")
371
-
372
- variable = next(iter(context.scope.items()))
373
- variable_name = variable[0]
374
-
375
- for char in prohibited_chars:
376
- if char in value:
377
- raise InvalidLiquidVariableName(
378
- f"'{char}' is not a valid character for variable '{variable_name}' (value: '{value}')."
379
- )
380
-
381
- return value
382
-
383
-
384
- def hash_text(text):
385
- return hashlib.sha256(text.encode()).hexdigest()
386
-
387
-
388
- def get_hash_value(specifications):
389
- return hash_text(json.dumps(specifications, indent=4))
8
+ from plain_parser.plain_spec import * # noqa: F401,F403
9
+ from plain_parser.plain_spec import get_frids_range
390
10
 
391
11
 
392
12
  def get_render_range(render_range, plain_source):
393
13
  render_range = render_range.split(",")
394
14
  range_end = render_range[1] if len(render_range) == 2 else render_range[0]
395
15
 
396
- return _get_frids_range(plain_source, render_range[0], range_end)
16
+ return get_frids_range(plain_source, render_range[0], range_end)
397
17
 
398
18
 
399
19
  def get_render_range_from(start, plain_source):
400
- return _get_frids_range(plain_source, start)
20
+ return get_frids_range(plain_source, start)
401
21
 
402
22
 
403
23
  def compute_render_range(args, plain_source_tree):
@@ -415,27 +35,3 @@ def compute_render_range(args, plain_source_tree):
415
35
  elif args.render_from:
416
36
  return get_render_range_from(args.render_from, plain_source_tree)
417
37
  return None
418
-
419
-
420
- def _get_frids_range(plain_source, start, end=None):
421
- frids = list(get_frids(plain_source))
422
-
423
- start = str(start)
424
-
425
- if start not in frids:
426
- raise InvalidFridArgument(f"Invalid start functionality ID: {start}. Valid IDs are: {frids}.")
427
-
428
- if end is not None:
429
- end = str(end)
430
- if end not in frids:
431
- raise InvalidFridArgument(f"Invalid end functionality ID: {end}. Valid IDs are: {frids}.")
432
-
433
- end_idx = frids.index(end) + 1
434
- else:
435
- end_idx = len(frids)
436
-
437
- start_idx = frids.index(start)
438
- if start_idx >= end_idx:
439
- raise InvalidFridArgument(f"Start functionality ID: {start} must be before end functionality ID: {end}.")
440
-
441
- return frids[start_idx:end_idx]
@@ -10,7 +10,13 @@ class ExitWithError(BaseAction):
10
10
  SUCCESSFUL_OUTCOME = "error_handled"
11
11
 
12
12
  def execute(self, render_context: RenderContext, previous_action_payload: Any | None):
13
- console.error(previous_action_payload)
13
+ # The stop reason is the error payload of the failed action, or the message stored when the
14
+ # error was dispatched from a state callback. The log and the TUI both show this text.
15
+ error_message = RenderError.get_display_message(
16
+ previous_action_payload, fallback_message=render_context.last_error_message
17
+ )
18
+ render_context.last_error_message = error_message
19
+ console.error(error_message)
14
20
 
15
21
  render_context.codeplain_api.fail_functional_requirement(
16
22
  render_context.frid_context.frid,
@@ -27,9 +33,4 @@ class ExitWithError(BaseAction):
27
33
  if render_context.run_state.render_id is not None:
28
34
  console.info(f"Render ID: {render_context.run_state.render_id}")
29
35
 
30
- return (
31
- self.SUCCESSFUL_OUTCOME,
32
- RenderError.encode(
33
- message=render_context.last_error_message or "Unknown error",
34
- ).to_payload(),
35
- )
36
+ return self.SUCCESSFUL_OUTCOME, RenderError.encode(message=error_message).to_payload()
@@ -7,7 +7,7 @@ from plain2code_console import RETRY_COLOR, console
7
7
  from plain2code_exceptions import FunctionalRequirementTooComplex
8
8
  from render_machine.actions.base_action import BaseAction
9
9
  from render_machine.implementation_code_helpers import ImplementationCodeHelpers
10
- from render_machine.render_context import RenderContext
10
+ from render_machine.render_context import MAX_UNITTEST_FIX_ATTEMPTS, RenderContext
11
11
  from render_machine.render_types import RenderError
12
12
 
13
13
  MAX_CODE_GENERATION_RETRIES = 2
@@ -20,9 +20,16 @@ class RenderFunctionalRequirement(BaseAction):
20
20
 
21
21
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
22
22
  if render_context.frid_context.functional_requirement_render_attempts >= MAX_CODE_GENERATION_RETRIES:
23
- error_msg = f"Unittests could not be fixed after rendering the functionality {render_context.frid_context.frid} for the {MAX_CODE_GENERATION_RETRIES} times."
23
+ # A render attempt is only restarted once its unit tests used up every fix round.
24
+ error_msg = (
25
+ f"Functionality {render_context.frid_context.frid}: unit tests still fail after "
26
+ f"{MAX_CODE_GENERATION_RETRIES} render attempts, {MAX_UNITTEST_FIX_ATTEMPTS} fix rounds each."
27
+ )
28
+ last_output_path = render_context.script_execution_history.latest_unit_test_output_path
29
+ if last_output_path:
30
+ error_msg += f" Last unit tests output: {last_output_path}"
24
31
  render_context.last_error_message = error_msg
25
- return self.ITERATION_LIMIT_EXCEEDED_OUTCOME, None
32
+ return self.ITERATION_LIMIT_EXCEEDED_OUTCOME, RenderError.encode(message=error_msg).to_payload()
26
33
 
27
34
  render_context.frid_context.functional_requirement_render_attempts += 1
28
35
 
@@ -67,14 +67,11 @@ class CodeRenderer:
67
67
  self.render_context.run_state.current_render_state = self.render_context.state
68
68
  self.render_context.script_execution_history.should_update_script_outputs = False
69
69
 
70
- self.render_context.previous_action_payload = previous_action_payload
71
-
72
70
  outcome, previous_action_payload = self.action_map[self.render_context.state].execute(
73
71
  self.render_context, previous_action_payload
74
72
  )
75
73
 
76
74
  if self.render_context.state == States.RENDER_FAILED.value:
77
- self.render_context.last_error_message = previous_action_payload
78
75
  self.render_context.event_bus.publish(RenderModuleFailed(module_name=self.render_context.module_name))
79
76
  break
80
77
 
@@ -113,12 +113,11 @@ class RenderContext:
113
113
  self.machine = machine
114
114
 
115
115
  def dispatch_error(self, error_message: str):
116
- """Log error, store it, and dispatch HANDLE_ERROR trigger.
116
+ """Store the error and dispatch HANDLE_ERROR trigger. ExitWithError logs it and the TUI displays it.
117
117
 
118
118
  Args:
119
119
  error_message: The error message to log and display to the user.
120
120
  """
121
- console.error(error_message)
122
121
  self.last_error_message = error_message
123
122
  self.machine.dispatch(triggers.HANDLE_ERROR)
124
123
 
@@ -259,17 +259,15 @@ class RenderError:
259
259
  """Decode error from action payload.
260
260
 
261
261
  Expects standardized format: {"error": {"message": ..., "type": ..., "details": ...}}
262
+ Any other payload is not an error (e.g. the payload of the action that ran before an error was
263
+ dispatched from a state callback) and decodes to None.
262
264
  """
263
- if payload is None:
265
+ if not isinstance(payload, dict) or "error" not in payload:
264
266
  return None
265
267
 
266
- if isinstance(payload, dict) and "error" in payload:
267
- error_data = payload["error"]
268
- return cls(
269
- message=error_data.get("message", "Unknown error"),
270
- error_type=error_data.get("type"),
271
- details=error_data.get("details"),
272
- )
273
-
274
- # Unexpected format - log and return generic error
275
- return cls(message=f"Unexpected error format: {type(payload).__name__}")
268
+ error_data = payload["error"]
269
+ return cls(
270
+ message=error_data.get("message", "Unknown error"),
271
+ error_type=error_data.get("type"),
272
+ details=error_data.get("details"),
273
+ )
@@ -87,3 +87,23 @@ def test_mixed_sources(project):
87
87
  assert srcs["conformance_tests_dest"] == "cli"
88
88
  assert srcs["build_folder"] == "config"
89
89
  assert srcs["build_dest"] == "default"
90
+
91
+
92
+ def test_job_tag_from_cli_and_config(project):
93
+ plain_file = os.path.join(project, "module.plain")
94
+ assert parse_arguments([plain_file]).job_tag is None
95
+ assert parse_arguments([plain_file, "--job-tag", "nightly"]).job_tag == "nightly"
96
+
97
+ (Path(project) / "config.yaml").write_text("job-tag: 123\n")
98
+ assert parse_arguments([plain_file]).job_tag == "123"
99
+ assert parse_arguments([plain_file, "--job-tag", "from_cli"]).job_tag == "from_cli"
100
+
101
+
102
+ def test_job_tag_too_long_is_rejected(project):
103
+ plain_file = os.path.join(project, "module.plain")
104
+ with pytest.raises(SystemExit):
105
+ parse_arguments([plain_file, "--job-tag", "x" * 301])
106
+
107
+ (Path(project) / "config.yaml").write_text(f"job-tag: {'x' * 301}\n")
108
+ with pytest.raises(SystemExit):
109
+ parse_arguments([plain_file])