Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for yaml swallowing quotes #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/main/python/ultimate_source_of_accounts/account_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@
FILENAME = "accounts"


def represent_unicode(dumper, data, style=None):
if not dumper.default_style and data.isdigit():
style = "'"
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style=style)


class MyDumper(yaml.Dumper):
pass

# Always use quotes when the string contains only digits
MyDumper.add_representer(str, represent_unicode)


def get_converted_aws_accounts(accounts):
"""Return converted AWS account data.

Produces a dictionary of "file name" -> "file content" pairs. When
uploading to S3, these become "S3 key" -> "S3 value" pairs.
"""
try:
yaml_data = yaml.dump(accounts, indent=2, default_flow_style=False)
yaml_data = yaml.dump(accounts, indent=2, default_flow_style=False,
Dumper=MyDumper)
json_data = json.dumps(accounts, sort_keys=True, indent=2)
except Exception as exc:
raise Exception("Failed to convert to yaml and json: {0}".format(exc))
Expand Down