Repository: ansible/ansible-lint

Issue:
Have to use ansible-lint --nocolor on Mac in order it to get it working
# Issue Type
- Bug report
# Ansible and Ansible Lint details

ansible --version
ansible 1.9.4
  configured module search path = /usr/share/ansible

ansible-lint --version
ansible-lint 3.2.1
- ansible installation method: pip, OS package
- ansible-lint installation method: pip, OS package
  
  pip list | grep -i ansible
  ansible (1.9.4)
  ansible-lint (3.2.1)
# Desired Behaviour

I expected ansible-lint to work right after installation on a simple playbook
# Actual Behaviour (Bug report only)

Right after installation, I tried ansible-lint on a playbook.yml file and got the following stack trace. After debugging for a while, I found I can turn off the color and get it working. By default it should work out of the box. Maybe change the default to nocolor? 

ansible-lint zookeeper.yml
Traceback (most recent call last):
  File "/usr/local/bin/ansible-lint", line 135, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/usr/local/bin/ansible-lint", line 125, in main
    print(formatter.format(match, options.colored))
  File "/Library/Python/2.7/site-packages/ansiblelint/formatters/**init**.py", line 15, in format
    stringc(match.linenumber, 'cyan'),
  File "/Library/Python/2.7/site-packages/ansible/color.py", line 69, in stringc
    return "\033["+codeCodes[color]+"m"+text+"\033[0m"
TypeError: cannot concatenate 'str' and 'int' objects



Discussion:
Has anyone got anisble-lint to work with color on Mac? 

As far as I can tell, it's nothing to do with OS X, it's that ansible 1.9's `stringc` doesn't work it if you pass it an `int` (like linenumber). I'll wrap the linenumbers in `str`

I've release 3.2.2rc1 for testing - if you confirm it works I'll commit the change.

`pip install --pre --no-deps --upgrade ansible-lint`

should do the upgrade


Resolving patch:
diff --git a/lib/ansiblelint/formatters/__init__.py b/lib/ansiblelint/formatters/__init__.py
--- a/lib/ansiblelint/formatters/__init__.py
+++ b/lib/ansiblelint/formatters/__init__.py
@@ -1,7 +1,7 @@
 try:
-    from ansible.color import stringc
+    from ansible import color
 except ImportError:
-    from ansible.utils.color import stringc
+    from ansible.utils import color
 
 
 class Formatter(object):
@@ -9,11 +9,12 @@ class Formatter(object):
     def format(self, match, colored=False):
         formatstr = u"{0} {1}\n{2}:{3}\n{4}\n"
         if colored:
-            return formatstr.format(stringc(u"[{0}]".format(match.rule.id), 'bright red'),
-                                    stringc(match.message, 'red'),
-                                    stringc(match.filename, 'blue'),
-                                    stringc(match.linenumber, 'cyan'),
-                                    stringc(match.line, 'purple'))
+            color.ANSIBLE_COLOR = True
+            return formatstr.format(color.stringc(u"[{0}]".format(match.rule.id), 'bright red'),
+                                    color.stringc(match.message, 'red'),
+                                    color.stringc(match.filename, 'blue'),
+                                    color.stringc(str(match.linenumber), 'cyan'),
+                                    color.stringc(str(match.line), 'purple'))
         else:
             return formatstr.format(match.rule.id,
                                     match.message,
@@ -27,9 +28,10 @@ class QuietFormatter(object):
     def format(self, match, colored=False):
         formatstr = u"{0} {1}:{2}"
         if colored:
-            return formatstr.format(stringc(u"[{0}]".format(match.rule.id), 'bright red'),
-                                    stringc(match.filename, 'blue'),
-                                    stringc(match.linenumber, 'cyan'))
+            color.ANSIBLE_COLOR = True
+            return formatstr.format(color.stringc(u"[{0}]".format(match.rule.id), 'bright red'),
+                                    color.stringc(match.filename, 'blue'),
+                                    color.stringc(str(match.linenumber), 'cyan'))
         else:
             return formatstr.format(match.rule.id, match.filename,
                                     match.linenumber)
@@ -40,10 +42,11 @@ class ParseableFormatter(object):
     def format(self, match, colored=False):
         formatstr = u"{0}:{1}: [{2}] {3}"
         if colored:
-            return formatstr.format(stringc(match.filename, 'blue'),
-                                    stringc(match.linenumber, 'cyan'),
-                                    stringc(u"E{0}".format(match.rule.id), 'bright red'),
-                                    stringc(match.message, 'red'))
+            color.ANSIBLE_COLOR = True
+            return formatstr.format(color.stringc(match.filename, 'blue'),
+                                    color.stringc(str(match.linenumber), 'cyan'),
+                                    color.stringc(u"E{0}".format(match.rule.id), 'bright red'),
+                                    color.stringc(match.message, 'red'))
         else:
             return formatstr.format(match.filename,
                                     match.linenumber,
diff --git a/lib/ansiblelint/version.py b/lib/ansiblelint/version.py
--- a/lib/ansiblelint/version.py
+++ b/lib/ansiblelint/version.py
@@ -1 +1 @@
-__version__ = '3.2.1'
+__version__ = '3.2.2'


Test patch:
diff --git a/test/TestFormatter.py b/test/TestFormatter.py
new file mode 100644
--- /dev/null
+++ b/test/TestFormatter.py
@@ -0,0 +1,45 @@
+# Copyright (c) 2016 Will Thames <will@thames.id.au>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+import os
+import unittest
+
+from ansiblelint import Match, AnsibleLintRule
+from ansiblelint.formatters import Formatter
+
+
+class TestFormatter(unittest.TestCase):
+
+    def setUp(self):
+        self.rule = AnsibleLintRule()
+        self.rule.id = "TCF0001"
+        self.formatter = Formatter()
+
+    def test_format_coloured_string(self):
+        match = Match(1, "hello", "filename.yml", self.rule, "message")
+        result = self.formatter.format(match, True)
+
+    def test_unicode_format_string(self):
+        match = Match(1, "hello", "filename.yml", self.rule, u'\U0001f427')
+        result = self.formatter.format(match, False)
+
+    def test_dict_format_line(self):
+        match = Match(1, {'hello': 'world'}, "filename.yml", self.rule, "xyz")
+        result = self.formatter.format(match, True)