From d6838a64c286b9c60bcf1d1dd2e08a190fac8bbd Mon Sep 17 00:00:00 2001 From: stefan Date: Tue, 10 Oct 2017 16:58:45 +0200 Subject: [PATCH] support for sexadecimal numbers --- Rakefile | 2 +- compose_format/__init__.py | 13 +++++++++++++ features/format.feature | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 328f3bc..125c7ec 100644 --- a/Rakefile +++ b/Rakefile @@ -14,7 +14,7 @@ end desc 'Checks style' task audit: :rubocop task :audit do - ignores = %w(D100 D101 D102 D103 D104 E501 I201) + ignores = %w(D100 D101 D102 D103 D104 E501 I201 N806) FILES = FileList[%w(bin/compose_format compose_format/*.py setup.py)] sh "flake8 --ignore='#{ignores * ','}' #{FILES}" diff --git a/compose_format/__init__.py b/compose_format/__init__.py index afa26bc..5223715 100755 --- a/compose_format/__init__.py +++ b/compose_format/__init__.py @@ -1,5 +1,6 @@ from ruamel.yaml import RoundTripDumper, RoundTripLoader, dump, load from ruamel.yaml.comments import CommentedMap, CommentedSeq +from ruamel.yaml.scalarstring import SingleQuotedScalarString class ComposeFormat: @@ -98,10 +99,22 @@ def reorder(data, strict=True): ComposeFormat.reorder(item, strict) return data if type(data) is CommentedSeq: + for i, value in enumerate(data): + data[i] = ComposeFormat.fix_sexadecimal_numbers(value) data.sort() return data return data + @staticmethod + def fix_sexadecimal_numbers(value): + import re + + SEXADECIMAL_NUMBER = '(?P\d+):(?P\d+)' + match = re.match(SEXADECIMAL_NUMBER, value) + if not match or int(match.group('left')) > 60 or int(match.group('right')) > 60: + return value + return SingleQuotedScalarString('{0}:{1}'.format(match.group('left'), match.group('right'))) + @staticmethod def order_map(keys): for key in ComposeFormat.ORDERS.keys(): diff --git a/features/format.feature b/features/format.feature index a1de579..363f0f1 100644 --- a/features/format.feature +++ b/features/format.feature @@ -179,3 +179,30 @@ Feature: Format options: tag: '{{.Name}}' """ + + Scenario: Sexadecimal Number Support + Given a file named "compose.yml" with: + """ + version: "2" + services: + service: + image: image + ports: + - '10' + - '59:59' + - 60:60 + - 61:61 + """ + When I run `bin/compose_format compose.yml` + Then it should pass with exactly: + """ + version: '2' + services: + service: + image: image + ports: + - '10' + - '59:59' + - '60:60' + - 61:61 + """