Skip to content

Commit

Permalink
Merge c3f6ef4 into d9d774b
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Gillett committed May 22, 2019
2 parents d9d774b + c3f6ef4 commit 529701a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/data/configs/dnf7/rhel-atomic-host.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ modules:
# for nodejs:10, ship all profiles
- name: nodejs
stream: 10
- name: something-else
stream: 1.10
6 changes: 6 additions & 0 deletions tests/ubiconfig/test_ubi.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def test_load_from_local():
assert isinstance(config, UbiConfig)


def test_load_from_local_decimal_integrity():
loader = ubi.get_loader(TEST_DATA_DIR)
config = loader.load('configs/dnf7/rhel-atomic-host.yaml')
assert config.modules.whitelist[2].stream == '1.10'


def test_load_from_nonyaml(tmpdir):
somefile = tmpdir.join('some-file.txt')
somefile.write('[oops, this is not valid yaml')
Expand Down
3 changes: 3 additions & 0 deletions ubiconfig/_impl/loaders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from .gitlab import GitlabLoader
from .local import LocalLoader
from ubiconfig.utils.yaml_dec_constructor import add_yaml_dec_constructor

__all__ = ['GitlabLoader', 'LocalLoader']

add_yaml_dec_constructor()
36 changes: 36 additions & 0 deletions ubiconfig/utils/yaml_dec_constructor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from yaml.constructor import SafeConstructor
from decimal import Decimal


def construct_yaml_dec(self, node):
"""
Replaces yaml's float constructor, which would remove any trailing zeros
from numbers using decimals, compromising data integrity.
"""

value = str(self.construct_scalar(node))
value = value.replace('_', '').lower()
sign = +1
if value[0] == '-':
sign = -1
if value[0] in '+-':
value = value[1:]
if value == '.inf':
return sign*self.inf_value
elif value == '.nan':
return self.nan_value
elif ':' in value:
digits = [Decimal(part) for part in value.split(':')]
digits.reverse()
base = 1
value = 0.0
for digit in digits:
value += digit*base
base *= 60
return sign*value
else:
return sign*Decimal(value)


def add_yaml_dec_constructor():
SafeConstructor.add_constructor(u'tag:yaml.org,2002:float', construct_yaml_dec)

0 comments on commit 529701a

Please sign in to comment.