pycontribs / python-vagrant

Python bindings for interacting with Vagrant virtual machines.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ANSI escape characters are printed when used with python 2.7 in MobaXTerm

jorge-lavin opened this issue · comments

Hi

I was using your module and came across the following issue, using Python 2.7 (the one that them provide as plugin) some ansi characters are printed when used print with an attribute of a Vagrant() object

import vagrant

v = vagrant.Vagrant(quiet_stdout=False)
boxes = v.box_list()
print [box.name for box in boxes]

It produces the following output

['\x1b[0matf/centos_server_dev (virtualbox, 0)\x1b[0m']

The following snippet can be used to solve the problem, which may be used to implement __repr__ and / or __str___ methods for the object:

import vagrant

import re
ansi_escape = re.compile(r'\x1b[^m]*m')
v = vagrant.Vagrant(quiet_stdout=False)
boxes = v.box_list()
print [ansi_escape.sub('', box.name) for box in boxes]

produces a more readable output

['atf/centos_server_dev (virtualbox, 0)']

I haven't encountered this problem before. I'm running Terminal.app on Mac OS X 10.10 Yosemite. Vagrant version 1.7.2. What version are you running? Any suggestions for how I can replicate this bug on my machine?

I am leaning toward using vagrant box list --machine-readable (introduced in Vagrant 1.4), since the human readable output is not well defined and changes regularly, making parsing a challenge. If the machine readable output does not produce the ANSI escape codes, that would kill two birds with one stone.

Can you tell me if the --machine-readable flag produces the ANSI escape codes for you? If not, I'll go ahead with the machine readable approach.

This should be fixed in version 0.5.5, which parses the --machine-readable output from vagrant.