Skip to content

Commit

Permalink
Merge pull request #129 from Karandash8/128-corner-case-of-numbers-re…
Browse files Browse the repository at this point in the history
…ndering-as-strings

128 corner case of numbers rendering as strings
  • Loading branch information
Karandash8 authored Oct 25, 2024
2 parents 2698a1d + 38d1024 commit 6b118eb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,8 @@ python -m twine upload dist/*
python -m cProfile -o profile.pstats main.py --root-dir <path>
gprof2dot log.pstats [-z <function>] | dot -Tsvg -o profile.svg
```

### Code coverage HTML report
```
pytest --cov --cov-report=html:coverage_re tests/*
```
32 changes: 22 additions & 10 deletions make_argocd_fly/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
from yaml import SafeDumper

from make_argocd_fly.exceptions import MissingSourceResourcesError

log = logging.getLogger(__name__)


class SafeDumper(yaml.SafeDumper):
class YamlDumper(SafeDumper):
def increase_indent(self, flow=False, *args, **kwargs):
return super().increase_indent(flow=flow, indentless=False)


def str_presenter(dumper, data):
"""configures yaml for dumping multiline strings
Ref: https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data"""
if data.count('\n') > 0:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
def represent_str(dumper, data):
"""configures yaml for dumping multiline strings
Ref: https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data"""
if data.count('\n') > 0:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
if data.startswith('0'):
try:
int(data[1:])
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='\'')
except (SyntaxError, ValueError):
pass

return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')


yaml.add_representer(str, represent_str, Dumper=YamlDumper)


yaml.add_representer(str, str_presenter, Dumper=SafeDumper)
class YamlLoader(SafeLoader):
pass


class ResourceViewer:
Expand Down Expand Up @@ -138,13 +150,13 @@ async def _write_resource(self, file_path: str, resource_yml: str) -> None:
os.makedirs(path, exist_ok=True)

try:
yaml_obj = yaml.load(resource_yml, Loader=SafeLoader)
yaml_obj = yaml.load(resource_yml, Loader=YamlLoader)
except yaml.composer.ComposerError:
log.error('Error parsing yaml to write as file {}. Yaml:\n{}'.format(file_path, resource_yml))
raise

with open(os.path.join(self.output_dir_abs_path, file_path), 'w') as f:
yaml.dump(yaml_obj, f, Dumper=SafeDumper,
yaml.dump(yaml_obj, f, Dumper=YamlDumper,
default_flow_style=False,
sort_keys=False,
allow_unicode=True,
Expand Down
4 changes: 2 additions & 2 deletions make_argocd_fly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def latest_version_check():
return

if current_version < latest_version:
log.warning('You are running {} ({}) but there is a newer version of the package available ({})'.format(package_name, current_version,
latest_version))
log.warning('You are running {} ({}) but there is a newer version of the package available ({}).'.format(package_name, current_version,
latest_version))
confirm_dialog()
else:
log.info('You are using the latest version of {} ({})'.format(package_name, current_version))
5 changes: 5 additions & 0 deletions tests/manual/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ vars:
double_reference_version: ${reference_version}
reference_version: ${app[version]}
resource: Deployment_thanos.yml
number: 01239 # numbers cannot start with 0, it will be quoted
number_oct: 0123
number_string: "01239"
number_string_2: "123"
number_oct_string: "0123"
app:
resource: ${resource}
version: 0.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ data:
"test": "json"
}
ip: 127.0.0.1
number: '01239'
number_oct: 83
number_string: '01239'
number_string_2: '123'
number_oct_string: '0123'
boolean_string: 'yes'
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ data:
"test": "json_management"
}
ip: 127.0.0.1
number: '01239'
number_oct: 83
number_string: '01239'
number_string_2: '123'
number_oct_string: '0123'
boolean_string: 'yes'
6 changes: 6 additions & 0 deletions tests/manual/source/app_5/configmap.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ data:
{% endfilter %}

ip: {{ 'localhost' | dig }}
number: {{ number }}
number_oct: {{ number_oct }}
number_string: "{{ number_string }}"
number_string_2: "{{ number_string_2 }}"
number_oct_string: "{{ number_oct_string }}"
boolean_string: "yes"

0 comments on commit 6b118eb

Please sign in to comment.