Skip to content

Commit 7aebf30

Browse files
committed
Import yaml representers from own class
1 parent 16fc6c6 commit 7aebf30

3 files changed

Lines changed: 38 additions & 41 deletions

File tree

scripts/spack_manifest/injection/modules.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,9 @@
1010
Projections,
1111
Specs
1212
)
13+
from scripts.spack_manifest.injection.yaml_representer import YamlExplicitFlowStyleSequence, yaml_explicit_flow_style_sequence_representer
1314

14-
# For sequence definitions, we keep it in the flow-style format (i.e., [a, b, c]) rather than block style
15-
# as it is more compact for reserved definitions.
16-
class YamlExplicitFlowStyleSequence(list[str]):
17-
pass
18-
19-
def yaml_explicit_flow_style_sequence_representer(dumper, data):
20-
"""
21-
Custom representer for YAML to ensure that some sequences are represented in flow style.
22-
This is necessary for sequences that are used as definitions in spack manifests.
23-
"""
24-
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)
25-
15+
# We represent reserved definitions as in flow-style sequences (eg. `[a]` rather than `- a`), so it is more compact.
2616
yaml.add_representer(YamlExplicitFlowStyleSequence, yaml_explicit_flow_style_sequence_representer)
2717

2818
##################
@@ -108,7 +98,6 @@ def inject_projections(
10898
# To start with, add the projections that are already defined in the manifest
10999
new_projections: dict[str, str] = dict(defined_projections_dict)
110100

111-
# if root_spec not in defined_projections:
112101
new_projections.update(
113102
generate_projection_for_root_spec_or_raise(manifest, root_spec, defined_projections_dict.get(root_spec))
114103
)

scripts/spack_manifest/injection/prerelease.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,23 @@
1111
Projections,
1212
Specs
1313
)
14+
from scripts.spack_manifest.injection.yaml_representer import (
15+
YamlExplicitFlowStyleSequence,
16+
YamlExplicitQuotedString,
17+
yaml_explicit_flow_style_sequence_representer,
18+
yaml_explicit_quoted_string_representer
19+
)
1420

1521

16-
# PyYaml by default dumps unquoted strings if they look unambiguous, and quoted strings otherwise.
17-
# PyYaml dumps '{name}/prX-Y' as a quoted str as it has '{' at the front and causes ambiguity
18-
# But 'ROOT_SPEC/.dependencies/prX-Y/VERSION-{hash:7}' is dumped as an unquoted str as it is unambiguous
19-
# So we need to wrap projections in a custom class that forces PyYaml to dump them as quoted strings.
20-
class YamlExplicitQuotedString(str):
21-
pass
22-
23-
def yaml_explicit_quoted_string_representer(dumper, data):
24-
"""
25-
Custom representer for YAML to ensure that some strings are quoted explicitly.
26-
This is necessary for strings that are used as projections in spack manifests.
27-
"""
28-
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="'")
29-
30-
# For sequence definitions, we keep it in the flow-style format (i.e., [a, b, c]) rather than block style
31-
# as it is more compact for reserved definitions.
32-
class YamlExplicitFlowStyleSequence(list[str]):
33-
pass
34-
35-
def yaml_explicit_flow_style_sequence_representer(dumper, data):
36-
"""
37-
Custom representer for YAML to ensure that some sequences are represented in flow style.
38-
This is necessary for sequences that are used as definitions in spack manifests.
39-
"""
40-
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)
41-
42-
22+
# The yaml representer sometimes dumps ambiguous strings in the case of projections like `{name}/...` as unquoted strings,
23+
# which is not handled by spack very well.
4324
yaml.add_representer(YamlExplicitQuotedString, yaml_explicit_quoted_string_representer)
25+
26+
# We represent reserved definitions as in flow-style sequences (eg. `[a]` rather than `- a`), so it is more compact.
4427
yaml.add_representer(YamlExplicitFlowStyleSequence, yaml_explicit_flow_style_sequence_representer)
4528

4629
### Actual methods begin here ###
4730

48-
4931
def inject_prerelease_information(
5032
manifest_path: str,
5133
version: str,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# For sequences of reserved definitions, we keep it in the flow-style format (i.e., [a, b, c]) rather than block style
2+
# as it is more compact for reserved definitions.
3+
class YamlExplicitFlowStyleSequence(list[str]):
4+
pass
5+
6+
def yaml_explicit_flow_style_sequence_representer(dumper, data):
7+
"""
8+
Custom representer for YAML to ensure that some sequences are represented in flow style.
9+
This is necessary for sequences that are used as definitions in spack manifests.
10+
"""
11+
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)
12+
13+
14+
# PyYaml by default dumps unquoted strings if they look unambiguous, and quoted strings otherwise.
15+
# PyYaml dumps '{name}/prX-Y' as a quoted str as it has '{' at the front and causes ambiguity
16+
# But 'ROOT_SPEC/.dependencies/prX-Y/VERSION-{hash:7}' is dumped as an unquoted str as it is unambiguous
17+
# So we need to wrap projections in a custom class that forces PyYaml to dump them as quoted strings.
18+
class YamlExplicitQuotedString(str):
19+
pass
20+
21+
def yaml_explicit_quoted_string_representer(dumper, data):
22+
"""
23+
Custom representer for YAML to ensure that some strings are quoted explicitly.
24+
This is necessary for strings that are used as projections in spack manifests.
25+
"""
26+
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="'")

0 commit comments

Comments
 (0)