avocado.utils: Add astring library

Add string manipulation APIs to the avocado.utils namespace.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 157259c0
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2013-2014
# Authors: Lucas Meneghel Rodrigues <lmr@redhat.com>
"""
Operations with strings (conversion and sanitization).
The unusual name aims to avoid causing name clashes with the stdlib module
string. Even with the dot notation, people may try to do things like
import string
...
from avocado.utils import string
And not notice until their code starts failing.
"""
import re
def bitlist_to_string(data):
"""
Transform from bit list to ASCII string.
:param data: Bit list to be transformed
"""
result = []
pos = 0
c = 0
while pos < len(data):
c += data[pos] << (7 - (pos % 8))
if (pos % 8) == 7:
result.append(c)
c = 0
pos += 1
return ''.join([chr(c) for c in result])
def string_to_bitlist(data):
"""
Transform from ASCII string to bit list.
:param data: String to be transformed
"""
data = [ord(c) for c in data]
result = []
for ch in data:
i = 7
while i >= 0:
if ch & (1 << i) != 0:
result.append(1)
else:
result.append(0)
i -= 1
return result
def strip_console_codes(output, custom_codes=None):
"""
Remove the Linux console escape and control sequences from the console
output. Make the output readable and can be used for result check. Now
only remove some basic console codes using during boot up.
:param output: The output from Linux console
:type output: string
:param custom_codes: The codes added to the console codes which is not
covered in the default codes
:type output: string
:return: the string wihout any special codes
:rtype: string
"""
if "\x1b" not in output:
return output
old_word = ""
return_str = ""
index = 0
output = "\x1b[m%s" % output
console_codes = "%[G@8]|\[[@A-HJ-MPXa-hl-nqrsu\`]"
console_codes += "|\[[\d;]+[HJKgqnrm]|#8|\([B0UK]|\)"
if custom_codes is not None and custom_codes not in console_codes:
console_codes += "|%s" % custom_codes
while index < len(output):
tmp_index = 0
tmp_word = ""
while (len(re.findall("\x1b", tmp_word)) < 2
and index + tmp_index < len(output)):
tmp_word += output[index + tmp_index]
tmp_index += 1
tmp_word = re.sub("\x1b", "", tmp_word)
index += len(tmp_word) + 1
if tmp_word == old_word:
continue
try:
special_code = re.findall(console_codes, tmp_word)[0]
except IndexError:
if index + tmp_index < len(output):
raise ValueError("%s is not included in the known console "
"codes list %s" % (tmp_word, console_codes))
continue
if special_code == tmp_word:
continue
old_word = tmp_word
return_str += tmp_word[len(special_code):]
return return_str
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册