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.
- {codeplain-0.3.12.dev27.dist-info → codeplain-0.3.13.dist-info}/METADATA +8 -8
- {codeplain-0.3.12.dev27.dist-info → codeplain-0.3.13.dist-info}/RECORD +29 -31
- {codeplain-0.3.12.dev27.dist-info → codeplain-0.3.13.dist-info}/WHEEL +1 -1
- concept_utils.py +2 -220
- file_utils.py +23 -107
- module_renderer.py +2 -4
- plain2code.py +39 -6
- plain2code_arguments.py +30 -0
- plain2code_exceptions.py +12 -42
- plain2code_nodes.py +2 -118
- plain2code_state.py +6 -2
- plain2code_utils.py +1 -16
- plain_file.py +2 -870
- plain_spec.py +9 -413
- render_machine/actions/exit_with_error.py +8 -7
- render_machine/actions/render_functional_requirement.py +10 -3
- render_machine/code_renderer.py +0 -3
- render_machine/render_context.py +1 -2
- render_machine/render_types.py +9 -11
- tests/test_arg_provenance.py +20 -0
- tests/test_file_utils.py +67 -54
- tests/test_plain2code_state.py +5 -0
- tests/test_render_finished_report.py +1 -0
- tests/test_render_range.py +42 -0
- tests/test_render_stop_reason.py +210 -0
- tests/test_terminal_attachment.py +1 -0
- tests/test_user_error_message.py +36 -0
- tests/test_imports.py +0 -53
- tests/test_plainfile.py +0 -271
- tests/test_plainfileparser.py +0 -775
- tests/test_plainspec.py +0 -103
- tests/test_requires.py +0 -28
- {codeplain-0.3.12.dev27.dist-info → codeplain-0.3.13.dist-info}/entry_points.txt +0 -0
- {codeplain-0.3.12.dev27.dist-info → codeplain-0.3.13.dist-info}/licenses/LICENSE +0 -0
plain2code.py
CHANGED
|
@@ -59,6 +59,39 @@ from tui.plain2code_tui import Plain2CodeTUI
|
|
|
59
59
|
from tui.plain_module_render_choice_tui import PlainModuleRenderChoiceTUI
|
|
60
60
|
|
|
61
61
|
DEFAULT_TEMPLATE_DIRS = "standard_template_library"
|
|
62
|
+
|
|
63
|
+
# Search-order guidance appended to lookup failures raised by the parser, which only
|
|
64
|
+
# reports the missing file. The wording is the CLI's: it knows what the template
|
|
65
|
+
# directories are and how the user changes them.
|
|
66
|
+
TEMPLATE_NOT_FOUND_HINT = f"""
|
|
67
|
+
The required template could not be found. Templates are searched in the following order (highest to lowest precedence):
|
|
68
|
+
|
|
69
|
+
1. The directory containing your .plain file
|
|
70
|
+
2. The directory specified by --template-dir (if provided)
|
|
71
|
+
3. The built-in '{DEFAULT_TEMPLATE_DIRS}' directory
|
|
72
|
+
|
|
73
|
+
Please ensure that the missing template exists in one of these locations, or specify the correct --template-dir if using custom templates.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
RESOURCE_NOT_FOUND_HINT = f""" Resource files are searched in the following order (highest to lowest precedence):
|
|
77
|
+
|
|
78
|
+
1. The directory containing your .plain file
|
|
79
|
+
2. The directory specified by --template-dir (if provided)
|
|
80
|
+
3. The built-in '{DEFAULT_TEMPLATE_DIRS}' directory
|
|
81
|
+
|
|
82
|
+
Please ensure that the resource exists in one of these locations, or specify the correct --template-dir if using custom templates."""
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def user_error_message(e: BaseException) -> str:
|
|
86
|
+
"""Format an exception for the user, appending search-order guidance to lookup failures."""
|
|
87
|
+
message = str(e) if str(e) else repr(e)
|
|
88
|
+
if isinstance(e, TemplateNotFoundError):
|
|
89
|
+
return message + TEMPLATE_NOT_FOUND_HINT
|
|
90
|
+
if isinstance(e, FileNotFoundError):
|
|
91
|
+
return message + RESOURCE_NOT_FOUND_HINT
|
|
92
|
+
return message
|
|
93
|
+
|
|
94
|
+
|
|
62
95
|
RENDER_THREAD_SHUTDOWN_TIMEOUT = 0.7
|
|
63
96
|
|
|
64
97
|
# The terminal states a render can reach, as the API names them.
|
|
@@ -371,7 +404,7 @@ def render( # noqa: C901
|
|
|
371
404
|
except Exception as e:
|
|
372
405
|
run_state.set_render_succeeded(False)
|
|
373
406
|
render_error.append(e)
|
|
374
|
-
event_bus.publish(RenderFailed(error_message=
|
|
407
|
+
event_bus.publish(RenderFailed(error_message=user_error_message(e)))
|
|
375
408
|
|
|
376
409
|
if args.headless:
|
|
377
410
|
console.info(f"Render started. Render ID: {run_state.render_id}")
|
|
@@ -446,7 +479,7 @@ def main(): # noqa: C901
|
|
|
446
479
|
console.info("Full plain text:\n")
|
|
447
480
|
console.info(full_plain_source)
|
|
448
481
|
except Exception as e:
|
|
449
|
-
console.error(f"Error: {
|
|
482
|
+
console.error(f"Error: {user_error_message(e)}")
|
|
450
483
|
return
|
|
451
484
|
|
|
452
485
|
# Parse the plain file (and its required modules) once; reused by dry-run and rendering.
|
|
@@ -457,14 +490,14 @@ def main(): # noqa: C901
|
|
|
457
490
|
template_dirs,
|
|
458
491
|
)
|
|
459
492
|
except Exception as e:
|
|
460
|
-
console.error(f"Error: {
|
|
493
|
+
console.error(f"Error: {user_error_message(e)}")
|
|
461
494
|
return
|
|
462
495
|
|
|
463
496
|
if args.dry_run:
|
|
464
497
|
try:
|
|
465
498
|
validate_linked_resources(plain_module)
|
|
466
499
|
except Exception as e:
|
|
467
|
-
console.error(f"Error: {
|
|
500
|
+
console.error(f"Error: {user_error_message(e)}")
|
|
468
501
|
return
|
|
469
502
|
|
|
470
503
|
console.info("Printing dry run output...\n")
|
|
@@ -482,7 +515,7 @@ def main(): # noqa: C901
|
|
|
482
515
|
codeplainAPI = codeplain_api.CodeplainAPI(args.api_key, console)
|
|
483
516
|
codeplainAPI.api_url = args.api
|
|
484
517
|
|
|
485
|
-
run_state = RunState(spec_filename=args.filename, replay_with=args.replay_with)
|
|
518
|
+
run_state = RunState(spec_filename=args.filename, replay_with=args.replay_with, job_tag=args.job_tag)
|
|
486
519
|
|
|
487
520
|
# Without a terminal nobody can watch or drive the TUI, so render as --headless does:
|
|
488
521
|
# progress on stdout, and a non-zero exit status when the render stops.
|
|
@@ -517,7 +550,7 @@ def main(): # noqa: C901
|
|
|
517
550
|
error_message = "Keyboard interrupt"
|
|
518
551
|
interrupted = True
|
|
519
552
|
else:
|
|
520
|
-
error_message =
|
|
553
|
+
error_message = user_error_message(e)
|
|
521
554
|
|
|
522
555
|
if not isinstance(e, EXPECTED_EXCEPTIONS):
|
|
523
556
|
exc_info = sys.exc_info()
|
plain2code_arguments.py
CHANGED
|
@@ -19,6 +19,7 @@ CODEPLAIN_API_KEY = os.getenv("CODEPLAIN_API_KEY")
|
|
|
19
19
|
DEFAULT_BUILD_FOLDER = "plain_modules"
|
|
20
20
|
DEFAULT_BUILD_DEST = "dist"
|
|
21
21
|
DEFAULT_CONFORMANCE_TESTS_DEST = "dist_conformance_tests"
|
|
22
|
+
MAX_JOB_TAG_LENGTH = 300
|
|
22
23
|
|
|
23
24
|
UNIT_TESTS_SCRIPT_NAME = "unittests_script"
|
|
24
25
|
CONFORMANCE_TESTS_SCRIPT_NAME = "conformance_tests_script"
|
|
@@ -76,6 +77,15 @@ def non_empty_string(s):
|
|
|
76
77
|
return s
|
|
77
78
|
|
|
78
79
|
|
|
80
|
+
def job_tag_string(s):
|
|
81
|
+
s = str(s)
|
|
82
|
+
if not s:
|
|
83
|
+
raise argparse.ArgumentTypeError("The job tag cannot be empty.")
|
|
84
|
+
if len(s) > MAX_JOB_TAG_LENGTH:
|
|
85
|
+
raise argparse.ArgumentTypeError(f"The job tag cannot be longer than {MAX_JOB_TAG_LENGTH} characters.")
|
|
86
|
+
return s
|
|
87
|
+
|
|
88
|
+
|
|
79
89
|
def frid_string(s):
|
|
80
90
|
"""Validate that the FRID is an integer."""
|
|
81
91
|
if not s:
|
|
@@ -363,6 +373,15 @@ def create_parser():
|
|
|
363
373
|
help="",
|
|
364
374
|
)
|
|
365
375
|
|
|
376
|
+
_add_arg(
|
|
377
|
+
parser,
|
|
378
|
+
"--job-tag",
|
|
379
|
+
type=job_tag_string,
|
|
380
|
+
default=None,
|
|
381
|
+
help=f"Tag sent to the server with every request and attached to the render's LangSmith traces "
|
|
382
|
+
f"(max {MAX_JOB_TAG_LENGTH} characters).",
|
|
383
|
+
)
|
|
384
|
+
|
|
366
385
|
_add_arg(
|
|
367
386
|
parser,
|
|
368
387
|
"--template-dir",
|
|
@@ -450,6 +469,15 @@ def create_parser():
|
|
|
450
469
|
return parser
|
|
451
470
|
|
|
452
471
|
|
|
472
|
+
def _validate_job_tag(args, parser):
|
|
473
|
+
# Values from config.yaml bypass argparse's type conversion, so validate them here.
|
|
474
|
+
if args.job_tag is not None:
|
|
475
|
+
try:
|
|
476
|
+
args.job_tag = job_tag_string(args.job_tag)
|
|
477
|
+
except argparse.ArgumentTypeError as e:
|
|
478
|
+
parser.error(f"--job-tag: {e}")
|
|
479
|
+
|
|
480
|
+
|
|
453
481
|
def parse_arguments(command_line: Optional[Sequence[str]] = None):
|
|
454
482
|
parser = create_parser()
|
|
455
483
|
|
|
@@ -496,6 +524,8 @@ def parse_arguments(command_line: Optional[Sequence[str]] = None):
|
|
|
496
524
|
if not args.log_to_file and args.argument_sources.get("log_file_name") != "default":
|
|
497
525
|
parser.error("--log-file-name cannot be used when --log-to-file is False.")
|
|
498
526
|
|
|
527
|
+
_validate_job_tag(args, parser)
|
|
528
|
+
|
|
499
529
|
if args.full_plain and args.dry_run:
|
|
500
530
|
parser.error("--full-plain and --dry-run are mutually exclusive")
|
|
501
531
|
|
plain2code_exceptions.py
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
from plain_parser.exceptions import ( # noqa: F401
|
|
2
|
+
ImportedModuleWithFunctionalitiesError,
|
|
3
|
+
InvalidFridArgument,
|
|
4
|
+
InvalidLiquidVariableName,
|
|
5
|
+
MissingFunctionalitiesError,
|
|
6
|
+
ModuleDoesNotExistError,
|
|
7
|
+
PlainSyntaxError,
|
|
8
|
+
UnsupportedBase64Content,
|
|
9
|
+
UnsupportedResourceType,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
1
13
|
class FunctionalRequirementTooComplex(Exception):
|
|
2
14
|
def __init__(self, message, proposed_breakdown=None):
|
|
3
15
|
self.message = message
|
|
@@ -21,19 +33,6 @@ class MissingResource(Exception):
|
|
|
21
33
|
pass
|
|
22
34
|
|
|
23
35
|
|
|
24
|
-
class UnsupportedResourceType(Exception):
|
|
25
|
-
pass
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class PlainSyntaxError(Exception):
|
|
29
|
-
pass
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class UnsupportedBase64Content(Exception):
|
|
33
|
-
|
|
34
|
-
pass
|
|
35
|
-
|
|
36
|
-
|
|
37
36
|
class InternalClientError(Exception):
|
|
38
37
|
pass
|
|
39
38
|
|
|
@@ -50,24 +49,12 @@ class OutdatedClientVersion(Exception):
|
|
|
50
49
|
pass
|
|
51
50
|
|
|
52
51
|
|
|
53
|
-
class InvalidFridArgument(Exception):
|
|
54
|
-
pass
|
|
55
|
-
|
|
56
|
-
|
|
57
52
|
class InvalidGitRepositoryError(Exception):
|
|
58
53
|
"""Raised when the git repository is in an invalid state."""
|
|
59
54
|
|
|
60
55
|
pass
|
|
61
56
|
|
|
62
57
|
|
|
63
|
-
class InvalidLiquidVariableName(Exception):
|
|
64
|
-
pass
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class ModuleDoesNotExistError(Exception):
|
|
68
|
-
pass
|
|
69
|
-
|
|
70
|
-
|
|
71
58
|
class InternalServerError(Exception):
|
|
72
59
|
pass
|
|
73
60
|
|
|
@@ -78,23 +65,6 @@ class MissingPreviousFunctionalitiesError(Exception):
|
|
|
78
65
|
pass
|
|
79
66
|
|
|
80
67
|
|
|
81
|
-
class MissingFunctionalitiesError(Exception):
|
|
82
|
-
"""Raised when a module to be rendered has no functionalities specified at all."""
|
|
83
|
-
|
|
84
|
-
pass
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
class ImportedModuleWithFunctionalitiesError(Exception):
|
|
88
|
-
"""Raised when a module brought in via ``import`` contains functional specs.
|
|
89
|
-
|
|
90
|
-
This is a usage error, not a syntax error: the module is syntactically valid
|
|
91
|
-
and would be fine as a render target, but functionalities are not allowed in
|
|
92
|
-
the import role.
|
|
93
|
-
"""
|
|
94
|
-
|
|
95
|
-
pass
|
|
96
|
-
|
|
97
|
-
|
|
98
68
|
class NetworkConnectionError(Exception):
|
|
99
69
|
"""Raised when there is a network connectivity issue with the API server."""
|
|
100
70
|
|
plain2code_nodes.py
CHANGED
|
@@ -1,119 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
from typing import Mapping, Sequence, TextIO
|
|
1
|
+
"""Compatibility shim: the parser now lives in the ``plain-parser`` package."""
|
|
3
2
|
|
|
4
|
-
from
|
|
5
|
-
from liquid2.builtin import IncludeTag
|
|
6
|
-
from liquid2.builtin.tags.include_tag import IncludeNode
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Plain2CodeIncludeNode(IncludeNode):
|
|
10
|
-
def render_to_output(self, context: RenderContext, buffer: TextIO) -> int:
|
|
11
|
-
"""Render the node to the output buffer."""
|
|
12
|
-
name = self.name.evaluate(context)
|
|
13
|
-
whitespaces = 0
|
|
14
|
-
is_comment = False
|
|
15
|
-
i = self.token.start
|
|
16
|
-
while self.token.source[i] != "\n" and i >= 0:
|
|
17
|
-
if self.token.source[i] == " ":
|
|
18
|
-
whitespaces += 1
|
|
19
|
-
elif self.token.source[i] == ">":
|
|
20
|
-
is_comment = True
|
|
21
|
-
break
|
|
22
|
-
else:
|
|
23
|
-
whitespaces = 0
|
|
24
|
-
i -= 1
|
|
25
|
-
|
|
26
|
-
if is_comment:
|
|
27
|
-
return buffer.write(str(self))
|
|
28
|
-
|
|
29
|
-
try:
|
|
30
|
-
template = context.env.get_template(str(name), context=context, tag=self.tag, whitespaces=whitespaces)
|
|
31
|
-
except TemplateNotFoundError as err:
|
|
32
|
-
detail = str(err)
|
|
33
|
-
long_message = f"""Template not found: {detail}
|
|
34
|
-
The required template could not be found. Templates are searched in the following order (highest to lowest precedence):
|
|
35
|
-
|
|
36
|
-
1. The directory containing your .plain file
|
|
37
|
-
2. The directory specified by --template-dir (if provided)
|
|
38
|
-
3. The built-in 'standard_template_library' directory
|
|
39
|
-
|
|
40
|
-
Please ensure that the missing template exists in one of these locations, or specify the correct --template-dir if using custom templates.
|
|
41
|
-
"""
|
|
42
|
-
wrapped = TemplateNotFoundError(long_message)
|
|
43
|
-
wrapped.token = self.name.token
|
|
44
|
-
wrapped.template_name = context.template.full_name()
|
|
45
|
-
raise wrapped from err
|
|
46
|
-
|
|
47
|
-
namespace: dict[str, object] = dict(arg.evaluate(context) for arg in self.args)
|
|
48
|
-
|
|
49
|
-
character_count = 0
|
|
50
|
-
|
|
51
|
-
with context.extend(namespace, template=template):
|
|
52
|
-
if self.var:
|
|
53
|
-
val = self.var.evaluate(context)
|
|
54
|
-
key = self.alias or template.name.split(".")[0]
|
|
55
|
-
|
|
56
|
-
if isinstance(val, Sequence) and not isinstance(val, str):
|
|
57
|
-
context.raise_for_loop_limit(len(val))
|
|
58
|
-
for itm in val:
|
|
59
|
-
namespace[key] = itm
|
|
60
|
-
character_count += template.render_with_context(context, buffer, partial=True)
|
|
61
|
-
else:
|
|
62
|
-
namespace[key] = val
|
|
63
|
-
character_count = template.render_with_context(context, buffer, partial=True)
|
|
64
|
-
else:
|
|
65
|
-
|
|
66
|
-
character_count = template.render_with_context(context, buffer, partial=True)
|
|
67
|
-
|
|
68
|
-
return character_count
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
class Plain2CodeIncludeTag(IncludeTag):
|
|
72
|
-
node_class = Plain2CodeIncludeNode
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
class Plain2CodeLoaderMixin:
|
|
76
|
-
def __init__(self, *args, **kwargs):
|
|
77
|
-
if not hasattr(self, "get_source"):
|
|
78
|
-
raise NotImplementedError("Class must implement get_source")
|
|
79
|
-
super().__init__(*args, **kwargs)
|
|
80
|
-
|
|
81
|
-
def load(
|
|
82
|
-
self,
|
|
83
|
-
env: Environment,
|
|
84
|
-
name: str,
|
|
85
|
-
*,
|
|
86
|
-
globals: Mapping[str, object] | None = None,
|
|
87
|
-
context: RenderContext | None = None,
|
|
88
|
-
**kwargs: object,
|
|
89
|
-
) -> Template:
|
|
90
|
-
"""
|
|
91
|
-
Find and parse template source code.
|
|
92
|
-
|
|
93
|
-
Args:
|
|
94
|
-
env: The `Environment` attempting to load the template source text.
|
|
95
|
-
name: A name or identifier for a template's source text.
|
|
96
|
-
globals: A mapping of render context variables attached to the
|
|
97
|
-
resulting template.
|
|
98
|
-
context: An optional render context that can be used to narrow the template
|
|
99
|
-
source search space.
|
|
100
|
-
kwargs: Arbitrary arguments that can be used to narrow the template source
|
|
101
|
-
search space.
|
|
102
|
-
"""
|
|
103
|
-
source, full_name, uptodate, matter = self.get_source(env, name, context=context, **kwargs)
|
|
104
|
-
whitespaces = kwargs.get("whitespaces", 0)
|
|
105
|
-
assert isinstance(whitespaces, int)
|
|
106
|
-
source = source.rstrip().replace("\n", "\n" + " " * whitespaces)
|
|
107
|
-
|
|
108
|
-
path = Path(full_name)
|
|
109
|
-
|
|
110
|
-
template = env.from_string(
|
|
111
|
-
source,
|
|
112
|
-
name=path.name,
|
|
113
|
-
path=path,
|
|
114
|
-
globals=globals,
|
|
115
|
-
overlay_data=matter,
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
template.uptodate = uptodate
|
|
119
|
-
return template
|
|
3
|
+
from plain_parser.liquid_nodes import * # noqa: F401,F403
|
plain2code_state.py
CHANGED
|
@@ -8,7 +8,7 @@ from typing import Optional
|
|
|
8
8
|
class RunState:
|
|
9
9
|
"""Contains information about the identifiable state of the rendering process."""
|
|
10
10
|
|
|
11
|
-
def __init__(self, spec_filename: str, replay_with: Optional[str] = None):
|
|
11
|
+
def __init__(self, spec_filename: str, replay_with: Optional[str] = None, job_tag: Optional[str] = None):
|
|
12
12
|
self.replay: bool = replay_with is not None
|
|
13
13
|
self.render_succeeded: bool = False
|
|
14
14
|
self.render_cancelled: bool = False
|
|
@@ -19,6 +19,7 @@ class RunState:
|
|
|
19
19
|
else:
|
|
20
20
|
self.render_id: str = str(uuid.uuid4())
|
|
21
21
|
self.spec_filename: str = spec_filename
|
|
22
|
+
self.job_tag: Optional[str] = job_tag
|
|
22
23
|
self.call_count: int = 0
|
|
23
24
|
self.unittest_batch_id: int = 0
|
|
24
25
|
self.render_time_accumulated: int = 0
|
|
@@ -70,9 +71,12 @@ class RunState:
|
|
|
70
71
|
self.last_render_start_timestamp = time.monotonic()
|
|
71
72
|
|
|
72
73
|
def to_dict(self):
|
|
73
|
-
|
|
74
|
+
state = {
|
|
74
75
|
"render_id": self.render_id,
|
|
75
76
|
"call_count": self.call_count,
|
|
76
77
|
"replay": self.replay,
|
|
77
78
|
"spec_filename": self.spec_filename,
|
|
78
79
|
}
|
|
80
|
+
if self.job_tag is not None:
|
|
81
|
+
state["job_tag"] = self.job_tag
|
|
82
|
+
return state
|
plain2code_utils.py
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
from typing import Optional
|
|
3
|
-
|
|
4
|
-
#
|
|
5
|
-
MAX_BASE64_BLOB_LENGTH = 8192
|
|
6
|
-
|
|
7
|
-
# Matches a long contiguous base64 / base64url run, optionally preceded by a data: URI header.
|
|
8
|
-
_BASE64_BLOB_PATTERN = re.compile(
|
|
9
|
-
r"(?:data:[\w.+-]+/[\w.+-]+;base64,)?[A-Za-z0-9+/_-]{%d,}={0,2}" % MAX_BASE64_BLOB_LENGTH
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def find_large_base64_blob(text: str) -> Optional[str]:
|
|
14
|
-
"""Return the first contiguous base64 blob at or above the threshold, or None."""
|
|
15
|
-
match = _BASE64_BLOB_PATTERN.search(text)
|
|
16
|
-
return match.group(0) if match else None
|
|
1
|
+
from plain_parser.loaders import MAX_BASE64_BLOB_LENGTH, find_large_base64_blob # noqa: F401
|
|
17
2
|
|
|
18
3
|
|
|
19
4
|
def format_duration_hms(total_seconds: float) -> str:
|