plain-parser 0.1.2.dev3__py3-none-any.whl → 0.1.2.dev5__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.
@@ -200,14 +200,14 @@ def sort_definitions(definitions: list[dict]) -> list[dict]:
200
200
  concept_graph.add_node(node)
201
201
 
202
202
  if not nx.is_directed_acyclic_graph(concept_graph):
203
- msg = "Found cycles in the concept graph. Cycles are not allowed."
203
+ msg = "Found cycles in the concept graph. Cycles are not allowed. "
204
204
  all_cycles = list(nx.simple_cycles(concept_graph))
205
205
 
206
206
  for cycle in all_cycles:
207
207
  cyclic_definitions = []
208
208
  cyclic_definitions.append(concept_definitions[cycle[0]]["markdown"])
209
209
  cyclic_definitions.append(concept_definitions[cycle[1]]["markdown"])
210
- msg += "Cyclic definitons:\n"
210
+ msg += "Cyclic definitions:\n"
211
211
  msg += "\n".join(cyclic_definitions)
212
212
  msg += "\n"
213
213
 
plain_parser/loaders.py CHANGED
@@ -3,10 +3,10 @@ import re
3
3
  from typing import Optional
4
4
 
5
5
  from liquid2 import Environment, FileSystemLoader, StrictUndefined
6
- from liquid2.exceptions import UndefinedError
6
+ from liquid2.exceptions import LiquidSyntaxError, UndefinedError
7
7
 
8
8
  from plain_parser import plain_spec
9
- from plain_parser.exceptions import UnsupportedBase64Content, UnsupportedResourceType
9
+ from plain_parser.exceptions import PlainSyntaxError, UnsupportedBase64Content, UnsupportedResourceType
10
10
  from plain_parser.liquid_nodes import Plain2CodeIncludeTag, Plain2CodeLoaderMixin
11
11
 
12
12
  MAX_BASE64_BLOB_LENGTH = 8192
@@ -36,6 +36,20 @@ def open_from(dirs, file_name):
36
36
  return None
37
37
 
38
38
 
39
+ def resolve_linked_resource(dirs, file_name):
40
+ """Return the first existing path for file_name across dirs, or None if not found.
41
+
42
+ Mirrors the search order used by open_from so that validating a linked resource and
43
+ loading it can never disagree.
44
+ """
45
+ for dir in dirs:
46
+ full_file_name = os.path.join(dir, file_name)
47
+ if os.path.exists(full_file_name):
48
+ return full_file_name
49
+
50
+ return None
51
+
52
+
39
53
  def load_linked_resources(template_dirs: list[str], resources_list, module_name: str):
40
54
  linked_resources = {}
41
55
 
@@ -81,7 +95,20 @@ class TrackingFileSystemLoader(Plain2CodeLoaderMixin, FileSystemLoader):
81
95
  return source
82
96
 
83
97
 
84
- def get_loaded_templates(source_path, plain_source):
98
+ def liquid_error_message(error: LiquidSyntaxError) -> str:
99
+ # Built from attributes: str() of some liquid2 syntax errors (e.g. an unterminated `{{ x`) raises ValueError.
100
+ template_name = error.template_name or None
101
+ token = error.token
102
+ if token is None or token.start < 0:
103
+ where = f" in {template_name}" if template_name else ""
104
+ else:
105
+ line = token.source[: token.start].count("\n") + 1
106
+ column = token.start - (token.source.rfind("\n", 0, token.start) + 1)
107
+ where = f" at {template_name}:{line}:{column}" if template_name else f" at {line}:{column}"
108
+ return f"Plain syntax error: Invalid Liquid{where}: {error.message}"
109
+
110
+
111
+ def get_loaded_templates(source_path, plain_source, template_name=None):
85
112
  # Render the plain source with Liquid templating engine
86
113
  # to identify the templates that are being loaded
87
114
 
@@ -92,9 +119,11 @@ def get_loaded_templates(source_path, plain_source):
92
119
  liquid_env.filters["code_variable"] = plain_spec.code_variable_liquid_filter
93
120
  liquid_env.filters["prohibited_chars"] = plain_spec.prohibited_chars_liquid_filter
94
121
 
95
- plain_source_template = liquid_env.from_string(plain_source)
96
122
  try:
123
+ plain_source_template = liquid_env.from_string(plain_source, name=template_name or "")
97
124
  plain_source = plain_source_template.render()
125
+ except LiquidSyntaxError as e:
126
+ raise PlainSyntaxError(liquid_error_message(e))
98
127
  except UndefinedError as e:
99
128
  raise Exception(f"Undefined liquid variable: {str(e)}")
100
129
 
@@ -88,7 +88,7 @@ def remove_quotes(token):
88
88
  token.children = tuple(new_children)
89
89
 
90
90
 
91
- def check_section_for_linked_resources(section):
91
+ def check_section_for_linked_resources(section, template_dirs):
92
92
  linked_resources = []
93
93
  for link in traverse(section, klass=Link):
94
94
  parsed_url = urlparse(link.node.target)
@@ -97,11 +97,19 @@ def check_section_for_linked_resources(section):
97
97
  f"Plain syntax error: Only relative links are allowed (text: {link.node.children[0].content}, target: {link.node.target})."
98
98
  )
99
99
 
100
- if not os.path.exists(link.node.target):
101
- raise PlainSyntaxError(f"Plain syntax error: Link {link.node.target} does not exist.")
100
+ resolved_target = loaders.resolve_linked_resource(template_dirs, link.node.target)
101
+ if resolved_target is None:
102
+ searched_dirs = "\n".join(f" {position}. {dir}" for position, dir in enumerate(template_dirs, start=1))
103
+ raise PlainSyntaxError(
104
+ f"Plain syntax error: Link {link.node.target} does not exist. "
105
+ f"Linked resources are looked up relative to the following directories "
106
+ f"(highest to lowest precedence):\n{searched_dirs}"
107
+ )
102
108
 
103
- if not os.path.isfile(link.node.target):
104
- raise PlainSyntaxError(f"Plain syntax error: Link {link.node.target} must be a file.")
109
+ if not os.path.isfile(resolved_target):
110
+ raise PlainSyntaxError(
111
+ f"Plain syntax error: Link {link.node.target} must be a file (resolved to {resolved_target})."
112
+ )
105
113
 
106
114
  if len(link.node.children) != 1:
107
115
  raise PlainSyntaxError(f"Plain syntax error: Link must have text specified (link: {link.node.target}).")
@@ -120,15 +128,15 @@ def check_section_for_linked_resources(section):
120
128
  section.linked_resources = linked_resources
121
129
 
122
130
 
123
- def check_for_linked_resources(plain_source):
131
+ def check_for_linked_resources(plain_source, template_dirs):
124
132
  for specification_heading in plain_spec.ALLOWED_SPECIFICATION_HEADINGS:
125
133
  if specification_heading in plain_source and hasattr(plain_source[specification_heading], "children"):
126
134
  for requirement in plain_source[specification_heading].children:
127
- check_section_for_linked_resources(requirement)
135
+ check_section_for_linked_resources(requirement, template_dirs)
128
136
 
129
137
  if hasattr(requirement, plain_spec.ACCEPTANCE_TESTS):
130
138
  for acceptance_test in requirement.acceptance_tests:
131
- check_section_for_linked_resources(acceptance_test)
139
+ check_section_for_linked_resources(acceptance_test, template_dirs)
132
140
 
133
141
 
134
142
  def process_section_code_variables(section, code_variables):
@@ -232,7 +240,7 @@ def _is_acceptance_test_heading(token) -> tuple[bool, str | None]:
232
240
  content = strong_children[0].content.strip()
233
241
  if content == plain_spec.ACCEPTANCE_TEST_HEADING:
234
242
  return True, None
235
- problem = f"Syntax error at line {token.line_number}: Invalid acceptance test heading (`{content}`). Expected: `{plain_spec.ACCEPTANCE_TEST_HEADING}.`"
243
+ problem = f"Syntax error at line {token.line_number}: Invalid acceptance test heading (`{content}`). Expected: `{plain_spec.ACCEPTANCE_TEST_HEADING}`."
236
244
  return False, problem
237
245
 
238
246
 
@@ -500,6 +508,7 @@ def parse_plain_source( # noqa: C901
500
508
  template_dirs: list[str],
501
509
  imported_modules: list[str],
502
510
  modules_trace: list[str],
511
+ module_name: str | None = None,
503
512
  ) -> PlainFileParseResult:
504
513
  plain_source_text = normalize_line_endings(plain_source_text)
505
514
 
@@ -519,7 +528,7 @@ def parse_plain_source( # noqa: C901
519
528
  else:
520
529
  required_concepts = list[str]()
521
530
 
522
- [_, loaded_templates] = loaders.get_loaded_templates(template_dirs, plain_source_text)
531
+ [_, loaded_templates] = loaders.get_loaded_templates(template_dirs, plain_source_text, module_name)
523
532
 
524
533
  plain_source_content = restore_stripped_lines(plain_source_text, plain_source_obj.content)
525
534
 
@@ -577,7 +586,7 @@ def parse_plain_source( # noqa: C901
577
586
 
578
587
  if specification_heading == plain_spec.DEFINITIONS and current_specification_heading is not None:
579
588
  raise PlainSyntaxError(
580
- f"Plain syntax error: Syntax error at line {token.line_number}: Definitions specification must be the first specification in the section (`{token_text}`)"
589
+ f"Plain syntax error: Syntax error at line {token.line_number}: Definitions specification must be the first specification in the section (`{token_text}`)"
581
590
  )
582
591
 
583
592
  current_specification_heading = specification_heading
@@ -594,7 +603,7 @@ def parse_plain_source( # noqa: C901
594
603
 
595
604
  else:
596
605
  raise PlainSyntaxError(
597
- f"Plain syntax error: Syntax error at line {token.line_number}: Invalid source structure (`{token_text}`)"
606
+ f"Plain syntax error: Syntax error at line {token.line_number}: Invalid source structure (`{token_text}`)"
598
607
  )
599
608
 
600
609
  # Acceptance tests are only allowed nested under functional specs. Reject them when
@@ -673,8 +682,8 @@ def read_module_plain_source(module_name: str, template_dirs: list[str]) -> str:
673
682
  if blob is not None:
674
683
  raise UnsupportedBase64Content(
675
684
  f"Module '{module_name}' contains a base64-encoded blob ({len(blob)} characters) "
676
- "inlined in the specification. This is not supported."
677
- "Remove the base64 data from the .plain file or if necessary,"
685
+ "inlined in the specification. This is not supported. "
686
+ "Remove the base64 data from the .plain file or if necessary, "
678
687
  "include the binary file path in the specification."
679
688
  )
680
689
 
@@ -696,6 +705,7 @@ def parse_plain_file(
696
705
  template_dirs,
697
706
  imported_modules,
698
707
  modules_trace + [module_name],
708
+ module_name=module_name,
699
709
  )
700
710
 
701
711
 
@@ -756,7 +766,7 @@ def process_required_modules(
756
766
  )
757
767
 
758
768
  marshalled_plain_source = validate_and_marshall_module(
759
- plain_file_parse_result, module_name, ancestor_exported_definitions, code_variables
769
+ plain_file_parse_result, module_name, ancestor_exported_definitions, code_variables, template_dirs
760
770
  )
761
771
  if module_name not in (chain_module_name for chain_module_name, _ in chain):
762
772
  chain.append((module_name, marshalled_plain_source))
@@ -793,6 +803,7 @@ def validate_and_marshall_module(
793
803
  module_name: str,
794
804
  exported_definitions: list[mistletoe.block_token.token],
795
805
  code_variables: dict,
806
+ template_dirs: list[str],
796
807
  ) -> dict:
797
808
  """Every check a module must pass after parsing, and its marshalled plain source tree."""
798
809
  if len(plain_file_parse_result.required_concepts) > 0:
@@ -826,7 +837,7 @@ def validate_and_marshall_module(
826
837
 
827
838
  process_acceptance_tests(plain_file_parse_result.plain_source)
828
839
 
829
- check_for_linked_resources(plain_file_parse_result.plain_source)
840
+ check_for_linked_resources(plain_file_parse_result.plain_source, template_dirs)
830
841
 
831
842
  marshalled_plain_source = marshall_plain_source(plain_file_parse_result.plain_source)
832
843
 
@@ -881,7 +892,7 @@ def _parse_module(
881
892
  )
882
893
 
883
894
  marshalled_plain_source = validate_and_marshall_module(
884
- plain_file_parse_result, module_name, exported_definitions, code_variables
895
+ plain_file_parse_result, module_name, exported_definitions, code_variables, template_dirs
885
896
  )
886
897
 
887
898
  return module_name, marshalled_plain_source, plain_file_parse_result.required_modules, chain
@@ -126,7 +126,7 @@ def collect_linked_resources(
126
126
  ):
127
127
 
128
128
  if not isinstance(plain_source_tree, dict):
129
- raise ValueError("[plain_source_tree must be a dictionary.")
129
+ raise ValueError("plain_source_tree must be a dictionary.")
130
130
 
131
131
  if frid is not None:
132
132
  functional_requirements = get_frids(plain_source_tree)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: plain-parser
3
- Version: 0.1.2.dev3
3
+ Version: 0.1.2.dev5
4
4
  Summary: Parser for ***plain specification files
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -0,0 +1,13 @@
1
+ plain_parser/__init__.py,sha256=RzRI_EVcrPnPT_BzoYtrw09-HUzKGrW4HT-GTd8GrTU,725
2
+ plain_parser/cli.py,sha256=oJL_23l_K3ZWpJLRROGjFo2UOsJv7uUoP0RbfL8oit4,4682
3
+ plain_parser/concept_utils.py,sha256=cgbgJANz5j4cBXbOxH8bdIClkcClTNbP1wbvhcm6WUM,8102
4
+ plain_parser/exceptions.py,sha256=WcHK5g9K6bsE-ZZBKWl0lZtiMTQ6fif8-zdEx9qjmRw,795
5
+ plain_parser/liquid_nodes.py,sha256=CYmujdPJMK9hLjfFPmnSoKL3Ps4sb-2OOhH-fZUssis,3917
6
+ plain_parser/loaders.py,sha256=o-I7u1NL8hZdTWztqwO7zw072aGxXTBBYSAFDBRG4q0,5136
7
+ plain_parser/plain_file.py,sha256=NnPB9-xp2BIwJIaKmZnjTVjYCZPhlh4RLakR1CV6tUU,39867
8
+ plain_parser/plain_spec.py,sha256=Z79Vi8bdRiOiM53QyWS8OLNSlGPrQObSZx11Eh8i8Mk,15308
9
+ plain_parser-0.1.2.dev5.dist-info/METADATA,sha256=48Pi2W98iN5Om2JxQT-N_weUPWc0DYaEy6VgYycf8cA,2681
10
+ plain_parser-0.1.2.dev5.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
11
+ plain_parser-0.1.2.dev5.dist-info/entry_points.txt,sha256=pA31GGUyYrmZSpH1CuSzhjrv8pX5ac5HI5hFop1B4eI,55
12
+ plain_parser-0.1.2.dev5.dist-info/licenses/LICENSE,sha256=QClWKncxgRjNUVOW8Axi7zK9HtsMdJEtUSMA4LEe8fo,1071
13
+ plain_parser-0.1.2.dev5.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- plain_parser/__init__.py,sha256=RzRI_EVcrPnPT_BzoYtrw09-HUzKGrW4HT-GTd8GrTU,725
2
- plain_parser/cli.py,sha256=oJL_23l_K3ZWpJLRROGjFo2UOsJv7uUoP0RbfL8oit4,4682
3
- plain_parser/concept_utils.py,sha256=BJzddKxrwg2KbV-58R74ihIJKcv-CGA4B5NZyhOixQ8,8100
4
- plain_parser/exceptions.py,sha256=WcHK5g9K6bsE-ZZBKWl0lZtiMTQ6fif8-zdEx9qjmRw,795
5
- plain_parser/liquid_nodes.py,sha256=CYmujdPJMK9hLjfFPmnSoKL3Ps4sb-2OOhH-fZUssis,3917
6
- plain_parser/loaders.py,sha256=5drZ7OOPJQjEitjWL5W5NfJ0CUatxKHU1UOwm_xhKKE,3850
7
- plain_parser/plain_file.py,sha256=cddrat_XLi72q00ZRAANjwIMMXrQnEUYdKaT8qo-y1s,39206
8
- plain_parser/plain_spec.py,sha256=31mLXDldi9zOXT9aK3QhD2mbxbph4ArFU_qgWYd2nwc,15309
9
- plain_parser-0.1.2.dev3.dist-info/METADATA,sha256=uh3mnC3PCi-4onNg4qrFD3sb_pvhIAyOUHTboAGH3xo,2681
10
- plain_parser-0.1.2.dev3.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
11
- plain_parser-0.1.2.dev3.dist-info/entry_points.txt,sha256=pA31GGUyYrmZSpH1CuSzhjrv8pX5ac5HI5hFop1B4eI,55
12
- plain_parser-0.1.2.dev3.dist-info/licenses/LICENSE,sha256=QClWKncxgRjNUVOW8Axi7zK9HtsMdJEtUSMA4LEe8fo,1071
13
- plain_parser-0.1.2.dev3.dist-info/RECORD,,