Skip to content

Commit

Permalink
fixup! Refactor parse_modular, docstrings and small type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-psb committed Dec 5, 2023
1 parent d34ad7e commit 19adc58
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pulp_rpm/app/modulemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import tempfile
import yaml
import collections

from jsonschema import Draft7Validator
from gettext import gettext as _ # noqa:F401
Expand Down Expand Up @@ -190,7 +191,7 @@ def parse_modular(file: str):
modulemd_obsoletes_all = []

for module in split_modulemd_file(file):
parsed_data = yaml.load(module, Loader=yaml.BaseLoader)
parsed_data = yaml.load(module, Loader=ModularYamlLoader)
# here we check the modulemd document as we don't store all info, so serializers
# are not enough then we only need to take required data from dict which is
# parsed by pyyaml library
Expand All @@ -217,3 +218,32 @@ def parse_modular(file: str):
logging.warning(f"Unknown modular document type found: {parsed_data.get('document')}")

return modulemd_all, modulemd_defaults_all, modulemd_obsoletes_all


class ModularYamlLoader(yaml.SafeLoader):
"""Custom Loader that preserve unquoted float in specific fields"""

# Field to preserve (will bypass yaml casting)
PRESERVED_FIELDS = ("name", "stream", "version", "context", "arch")

def construct_mapping(self, node, deep=False):
if not isinstance(node, yaml.MappingNode):
raise yaml.constructor.ConstructorError(
None, None, "expected a mapping node, but found %s" % node.id, node.start_mark
)
mapping = {}
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
if not isinstance(key, collections.abc.Hashable):
raise yaml.constructor.ConstructorError(
"while constructing a mapping",
node.start_mark,
"found unhashable key",
key_node.start_mark,
)
if key in ModularYamlLoader.PRESERVED_FIELDS:
value = value_node.value
else:
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
4 changes: 4 additions & 0 deletions pulp_rpm/tests/unit/test_modulemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
stream: 1.10
version: 20180730223407
context: deadbeef
static_context: true
arch: noarch
summary: Kangaroo 0.3 module
description: >-
Expand All @@ -35,6 +36,7 @@
stream: "1.10"
version: 20180704111719
context: deadbeef
static_context: false
arch: noarch
summary: Kangaroo 0.2 module
description: >-
Expand Down Expand Up @@ -74,10 +76,12 @@ def test_parse_modular_preserves_literal_unquoted_values_3285(tmp_path):
assert kangoroo1["stream"] == "1.10" # should not be 1.1
assert kangoroo1["version"] == "20180730223407"
assert kangoroo1["context"] == "deadbeef"
assert kangoroo1["static_context"] is True
assert kangoroo1["arch"] == "noarch"

assert kangoroo2["name"] == "kangaroo"
assert kangoroo2["stream"] == "1.10"
assert kangoroo2["version"] == "20180704111719"
assert kangoroo2["context"] == "deadbeef"
assert kangoroo2["static_context"] is False
assert kangoroo2["arch"] == "noarch"

0 comments on commit 19adc58

Please sign in to comment.