def test_parse_csv(self): output_lines = [ 'Header,Header 2', 'header value 1,header with spaces value', 'MixEd CaSe ValUe,ALL CAPS VALUE', '"""double quote escaped value""","," escaped value', 'unicode,chårs', ] assert hammer.parse_csv(output_lines) == [ { 'header': 'header value 1', 'header-2': 'header with spaces value' }, { 'header': 'MixEd CaSe ValUe', 'header-2': 'ALL CAPS VALUE' }, { 'header': '"double quote escaped value"', 'header-2': ', escaped value' }, { 'header': 'unicode', 'header-2': 'chårs' }, ]
def test_parse_csv(self): output_lines = [ u'Header,Header 2', u'header value 1,header with spaces value', u'MixEd CaSe ValUe,ALL CAPS VALUE', u'"""double quote escaped value""","," escaped value', u'unicode,chårs', ] self.assertEqual( hammer.parse_csv(output_lines), [ { u'header': u'header value 1', u'header-2': u'header with spaces value', }, { u'header': u'MixEd CaSe ValUe', u'header-2': u'ALL CAPS VALUE', }, { u'header': u'"double quote escaped value"', u'header-2': u', escaped value', }, { u'header': u'unicode', u'header-2': u'chårs', }, ] )
def test_parsed_json_match_parsed_csv(self): """Output generated by: JSON: LANG=en_US.UTF-8 hammer -v -u admin -p changeme --output=json gpg info --id="160" --organization-id="1003" CSV: LANG=en_US.UTF-8 hammer -v -u admin -p changeme --output=csv gpg info --id="160" --organization-id="1003" """ json_output = """{ "ID": 160, "Name": "QUWTHo0WzF", "Organization": "ANtbiU", "Content": "qJxB1FX1UrssYiGGhRcZDF9eY8U" } """ csv_ouput_lines = [ "ID,Name,Organization,Content", "160,QUWTHo0WzF,ANtbiU,qJxB1FX1UrssYiGGhRcZDF9eY8U", ] assert hammer.parse_json(json_output) == hammer.parse_csv( csv_ouput_lines)[0]
def command( cmd, hostname=None, output_format=None, username=None, password=None, timeout=None, port=22, ): """Executes SSH command(s) on remote hostname. kwargs are passed through to get_connection :param str cmd: The command to run :param str output_format: json, csv or None :param int timeout: Time to wait for the ssh command to finish. :param connection_timeout: Time to wait for establishing the connection. """ client = get_client( hostname=hostname, username=username, password=password, port=port, ) result = client.execute(cmd, timeout=timeout) if output_format and result.status == 0: if output_format == 'csv': result.stdout = hammer.parse_csv( result.stdout) if result.stdout else {} if output_format == 'json': result.stdout = hammer.parse_json( result.stdout) if result.stdout else None return result
def test_parse_csv(self): output_lines = [ u'Header,Header 2', u'header value 1,header with spaces value', u'MixEd CaSe ValUe,ALL CAPS VALUE', u'"""double quote escaped value""","," escaped value', u'unicode,chårs', ] self.assertEqual(hammer.parse_csv(output_lines), [ { u'header': u'header value 1', u'header-2': u'header with spaces value', }, { u'header': u'MixEd CaSe ValUe', u'header-2': u'ALL CAPS VALUE', }, { u'header': u'"double quote escaped value"', u'header-2': u', escaped value', }, { u'header': u'unicode', u'header-2': u'chårs', }, ])
def __init__( self, stdout=None, stderr=None, return_code=0, output_format=None): self.stdout = stdout self.stderr = stderr self.return_code = return_code self.output_format = output_format # Does not make sense to return suspicious output if ($? <> 0) if output_format and self.return_code == 0: if output_format == 'csv': self.stdout = hammer.parse_csv(stdout) if stdout else {} if output_format == 'json': self.stdout = json.loads(stdout) if stdout else None
def test_parsed_json_match_parsed_csv(self): """ Output generated by: JSON: LANG=en_US.UTF-8 hammer -v -u admin -p changeme --output=json gpg info --id="160" --organization-id="1003" CSV: LANG=en_US.UTF-8 hammer -v -u admin -p changeme --output=csv gpg info --id="160" --organization-id="1003" """ json_output = u"""{ "ID": 160, "Name": "QUWTHo0WzF", "Organization": "ANtbiU", "Content": "qJxB1FX1UrssYiGGhRcZDF9eY8U" } """ csv_ouput_lines = [u"ID,Name,Organization,Content", u"160,QUWTHo0WzF,ANtbiU,qJxB1FX1UrssYiGGhRcZDF9eY8U" ] self.assertEqual(hammer.parse_json(json_output), hammer.parse_csv(csv_ouput_lines)[0])