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

support for sexadecimal numbers #5

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
13 changes: 13 additions & 0 deletions compose_format/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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<left>\d+):(?P<right>\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():
Expand Down
27 changes: 27 additions & 0 deletions features/format.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""