Skip to content

Commit

Permalink
support for sexadecimal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
lindt committed Oct 10, 2017
1 parent 4211202 commit d6838a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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
"""

0 comments on commit d6838a6

Please sign in to comment.