Skip to content

Commit

Permalink
Updated to always support multibyte.
Browse files Browse the repository at this point in the history
  • Loading branch information
toriiico committed Jan 17, 2019
1 parent ec941c9 commit fa74105
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 35 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Options:
to YAML.
-n, --no-flip Perform other operations but do not flip the
output format.
-m, --multibyte Fix garbled characters in multibyte text.
--version Show the version and exit.
--help Show this message and exit.
```
Expand Down
10 changes: 5 additions & 5 deletions cfn_flip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def dump_yaml(data, clean_up=False, long_form=False):
)


def to_json(template, clean_up=False, is_multibyte=False):
def to_json(template, clean_up=False):
"""
Assume the input is YAML and convert to JSON
"""
Expand All @@ -57,7 +57,7 @@ def to_json(template, clean_up=False, is_multibyte=False):
if clean_up:
data = clean(data)

return dump_json(data, is_multibyte)
return dump_json(data)


def to_yaml(template, clean_up=False, long_form=False):
Expand All @@ -73,7 +73,7 @@ def to_yaml(template, clean_up=False, long_form=False):
return dump_yaml(data, clean_up, long_form)


def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=False, long_form=False, is_multibyte=False):
def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=False, long_form=False):
"""
Figure out the input format and convert the data to the opposing output format
"""
Expand Down Expand Up @@ -108,9 +108,9 @@ def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=Fals
# Finished!
if out_format == "json":
if sys.version[0] == "3":
return dump_json(data, is_multibyte)
return dump_json(data)
else:
return dump_json(data, is_multibyte).encode('utf-8')
return dump_json(data).encode('utf-8')


return dump_yaml(data, clean_up, long_form)
5 changes: 1 addition & 4 deletions cfn_flip/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@click.option("--clean", "-c", is_flag=True, help="Performs some opinionated cleanup on your template.")
@click.option("--long", "-l", is_flag=True, help="Use long-form syntax for functions when converting to YAML.")
@click.option("--no-flip", "-n", is_flag=True, help="Perform other operations but do not flip the output format.")
@click.option("--multibyte", "-m", is_flag=True, help="Fix garbled characters in multibyte text.")
@click.argument("input", type=click.File("r"), default=sys.stdin)
@click.argument("output", type=click.File("w"), default=sys.stdout)
@click.version_option(message='AWS Cloudformation Template Flip, Version %(version)s')
Expand All @@ -40,7 +39,6 @@ def main(ctx, **kwargs):
no_flip = kwargs.pop('no_flip')
clean = kwargs.pop('clean')
long_form = kwargs.pop('long')
is_multibyte = kwargs.pop('multibyte')
input_file = kwargs.pop('input')
output_file = kwargs.pop('output')

Expand All @@ -61,8 +59,7 @@ def main(ctx, **kwargs):
out_format=out_format,
clean_up=clean,
no_flip=no_flip,
long_form=long_form,
is_multibyte=is_multibyte
long_form=long_form
))
except Exception as e:
raise click.ClickException("{}".format(e))
4 changes: 2 additions & 2 deletions cfn_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def load_json(source):
return json.loads(source, object_pairs_hook=ODict)


def dump_json(source, is_multibyte=False):
def dump_json(source):
return json.dumps(source, indent=4, cls=DateTimeAwareJsonEncoder,
separators=(',', ': '), ensure_ascii=(not is_multibyte))
separators=(',', ': '), ensure_ascii=False)


def load_yaml(source):
Expand Down
22 changes: 0 additions & 22 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,3 @@ def test_specified_yaml_output_overrides_j_flag(tmpdir):
assert not result.exception
assert result.exit_code == 0
assert file_output.read() == file_standard


def test_cli_to_multibyte_json(tmpdir):
file_expected = open('examples/test_multibyte.yaml', 'r').read()
file_output = tmpdir.join('test_multibyte_export.yaml')

runner = CliRunner()
result = runner.invoke(main.main, ['-m', 'examples/test_multibyte.json', file_output.strpath])
assert not result.exception
assert result.exit_code == 0
assert file_output.read() == file_expected


def test_cli_to_multibyte_yaml(tmpdir):
file_expected = open('examples/test_multibyte.json', 'r').read()
file_output = tmpdir.join('test_multibyte_export.json')

runner = CliRunner()
result = runner.invoke(main.main, ['-m', 'examples/test_multibyte.yaml', file_output.strpath])
assert not result.exception
assert result.exit_code == 0
assert file_output.read() == file_expected
2 changes: 1 addition & 1 deletion tests/test_flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_flip_to_multibyte_yaml(multibyte_yaml, parsed_multibyte_json):
Test that load multibyte file performs correctly
"""

actual = cfn_flip.to_json(multibyte_yaml, is_multibyte=True)
actual = cfn_flip.to_json(multibyte_yaml)
assert load_json(actual) == parsed_multibyte_json


Expand Down

0 comments on commit fa74105

Please sign in to comment.