示例#1
0
 def test_safe_encode_same_encoding_different_cases(self):
     with mock.patch.object(encodeutils, "safe_decode", mock.Mock()):
         utf8 = encodeutils.safe_encode("foo\xf1bar", encoding="utf-8")
         self.assertEqual(
             encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
             encodeutils.safe_encode(utf8, "utf-8", "UTF-8"),
         )
         self.assertEqual(
             encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
             encodeutils.safe_encode(utf8, "utf-8", "utf-8"),
         )
         encodeutils.safe_decode.assert_has_calls([])
示例#2
0
 def test_safe_encode_same_encoding_different_cases(self):
     with mock.patch.object(encodeutils, "safe_decode", mock.Mock()):
         utf8 = encodeutils.safe_encode(
             six.u("foo\xf1bar"), encoding="utf-8")
         self.assertEqual(
             encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
             encodeutils.safe_encode(utf8, "utf-8", "UTF-8"),
         )
         self.assertEqual(
             encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
             encodeutils.safe_encode(utf8, "utf-8", "utf-8"),
         )
         encodeutils.safe_decode.assert_has_calls([])
示例#3
0
    def test_safe_encode_different_encodings(self):
        text = six.u("foo\xc3\xb1bar")
        result = encodeutils.safe_encode(
            text=text, incoming="utf-8", encoding="iso-8859-1")
        self.assertNotEqual(text, result)

        self.assertNotEqual(six.b("foo\xf1bar"), result)
示例#4
0
    def run(self, payload, algorithm, bit_length, mode):
        """Create and delete symmetric secret

        :param payload: The unecrypted data
        :param algorithm: the algorithm associated with the secret key
        :param bit_length: the big length of the secret key
        :param mode: the algorithm mode used with the secret key
        """
        from cryptography.hazmat.backends import default_backend
        from cryptography.hazmat.primitives import hashes
        from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

        payload = encodeutils.safe_encode(payload)
        salt = os.urandom(16)
        kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                         length=32,
                         salt=salt,
                         iterations=1000,
                         backend=default_backend())
        payload = base64.b64encode(kdf.derive(payload))
        payload = encodeutils.safe_decode(payload)
        expire_time = (dt.datetime.utcnow() + dt.timedelta(days=5))
        secret = self.admin_barbican.create_secret(
            expiration=expire_time.isoformat(),
            algorithm=algorithm,
            bit_length=bit_length,
            mode=mode,
            payload=payload,
            payload_content_type="application/octet-stream",
            payload_content_encoding="base64")
        self.admin_barbican.delete_secret(secret.secret_ref)
示例#5
0
    def test_safe_encode_different_encodings(self):
        text = six.u("foo\xc3\xb1bar")
        result = encodeutils.safe_encode(text=text,
                                         incoming="utf-8",
                                         encoding="iso-8859-1")
        self.assertNotEqual(text, result)

        self.assertNotEqual(six.b("foo\xf1bar"), result)
示例#6
0
 def test_safe_encode_py2(self):
     if six.PY2:
         # In Python 3, str.encode() doesn"t support anymore
         # text => text encodings like base64
         self.assertEqual(
             six.b("dGVzdA==\n"),
             encodeutils.safe_encode("test", encoding="base64"),
         )
     else:
         self.skipTest("Requires py2.x")
示例#7
0
 def test_safe_encode_py2(self):
     if six.PY2:
         # In Python 3, str.encode() doesn"t support anymore
         # text => text encodings like base64
         self.assertEqual(
             six.b("dGVzdA==\n"),
             encodeutils.safe_encode("test", encoding="base64"),
         )
     else:
         self.skipTest("Requires py2.x")
示例#8
0
 def test_bash_completion(self):
     old = open(os.path.join(RES_PATH, "rally.bash_completion"),
                "r").read().splitlines()
     new = cliutils._generate_bash_completion_script().splitlines()
     if old != new:
         for line in difflib.unified_diff(old, new):
             print(line)
         new_filename = "/tmp/rally.bash.new"
         with open(new_filename, "wb") as new_file:
             new_file.write(encodeutils.safe_encode("\n".join(new)))
         self.fail("bash completion script is outdated. "
                   "New script is located at %s "
                   "You may fix this by executing "
                   "`mv %s etc/rally.bash_completion`" %
                   (new_filename, new_filename))
示例#9
0
 def test_safe_encode_force_incoming_utf8_to_ascii(self):
     # Forcing incoming to ascii so it falls back to utf-8
     self.assertEqual(
         six.b("ni\xc3\xb1o"),
         encodeutils.safe_encode(six.b("ni\xc3\xb1o"), incoming="ascii"),
     )
示例#10
0
 def __init__(self, config):
     config_file = tempfile.NamedTemporaryFile(delete=False)
     config_file.write(encodeutils.safe_encode(json.dumps(config)))
     config_file.close()
     self.filename = config_file.name
示例#11
0
文件: cliutils.py 项目: sapcc/rally
def print_list(objs, fields, formatters=None, sortby_index=0,
               mixed_case_fields=None, field_labels=None,
               normalize_field_names=False,
               table_label=None, print_header=True, print_border=True,
               print_row_border=False,
               out=sys.stdout):
    """Print a list or objects as a table, one row per object.

    :param objs: iterable of :class:`Resource`
    :param fields: attributes that correspond to columns, in order
    :param formatters: `dict` of callables for field formatting
    :param sortby_index: index of the field for sorting table rows
    :param mixed_case_fields: fields corresponding to object attributes that
        have mixed case names (e.g., 'serverId')
    :param field_labels: Labels to use in the heading of the table, default to
        fields.
    :param normalize_field_names: If True, field names will be transformed,
        e.g. "Field Name" -> "field_name", otherwise they will be used
        unchanged.
    :param table_label: Label to use as header for the whole table.
    :param print_header: print table header.
    :param print_border: print table border.
    :param print_row_border: use border between rows
    :param out: stream to write output to.

    """
    formatters = formatters or {}
    mixed_case_fields = mixed_case_fields or []
    field_labels = field_labels or fields
    if len(field_labels) != len(fields):
        raise ValueError("Field labels list %(labels)s has different number of"
                         " elements than fields list %(fields)s"
                         % {"labels": field_labels, "fields": fields})

    kwargs = {}
    if sortby_index is not None:
        kwargs = {"sortby": field_labels[sortby_index]}

    if print_border and print_row_border:
        headers_horizontal_char = "="
        kwargs["hrules"] = prettytable.ALL
    else:
        headers_horizontal_char = "-"
    pt = prettytable.PrettyTable(field_labels)
    pt.align = "l"

    for o in objs:
        row = []
        for field in fields:
            if field in formatters:
                row.append(formatters[field](o))
            else:
                field_name = field

                if normalize_field_names:
                    if field_name not in mixed_case_fields:
                        field_name = field_name.lower()
                    field_name = field_name.replace(" ", "_").replace("-", "_")

                if isinstance(o, dict):
                    data = o.get(field_name, "")
                else:
                    data = getattr(o, field_name, "")
                row.append(data)
        pt.add_row(row)

    if not print_border or not print_header:
        pt.set_style(prettytable.PLAIN_COLUMNS)
        pt.left_padding_width = 0
        pt.right_padding_width = 1

    table_body = pt.get_string(header=print_header,
                               border=print_border,
                               **kwargs) + "\n"
    if print_border and print_row_border:
        table_body = table_body.split("\n", 3)
        table_body[2] = table_body[2].replace("-", headers_horizontal_char)
        table_body = "\n".join(table_body)

    table_header = ""

    if table_label:
        table_width = table_body.index("\n")
        table_header = make_table_header(
            table_label, table_width, horizontal_char=headers_horizontal_char)
        table_header += "\n"

    if table_header:
        out.write(encodeutils.safe_encode(table_header).decode())
    out.write(encodeutils.safe_encode(table_body).decode())
示例#12
0
文件: cliutils.py 项目: sapcc/rally
def print_dict(obj, fields=None, formatters=None, mixed_case_fields=False,
               normalize_field_names=False, property_label="Property",
               value_label="Value", table_label=None, print_header=True,
               print_border=True, wrap=0, out=sys.stdout):
    """Print dict as a table.

    :param obj: dict to print
    :param fields: `dict` of keys to print from d. Defaults to all keys
    :param formatters: `dict` of callables for field formatting
    :param mixed_case_fields: fields corresponding to object attributes that
        have mixed case names (e.g., 'serverId')
    :param normalize_field_names: If True, field names will be transformed,
        e.g. "Field Name" -> "field_name", otherwise they will be used
        unchanged.
    :param property_label: label of "property" column
    :param value_label: label of "value" column
    :param table_label: Label to use as header for the whole table.
    :param print_header: print table header.
    :param print_border: print table border.
    :param out: stream to write output to.
    """
    formatters = formatters or {}
    mixed_case_fields = mixed_case_fields or []
    if not fields:
        if isinstance(obj, dict):
            fields = sorted(obj.keys())
        else:
            fields = [name for name in dir(obj)
                      if (not name.startswith("_")
                          and not callable(getattr(obj, name)))]

    pt = prettytable.PrettyTable([property_label, value_label], caching=False)
    pt.align = "l"
    for field_name in fields:
        if field_name in formatters:
            data = formatters[field_name](obj)
        else:
            field = field_name
            if normalize_field_names:
                if field not in mixed_case_fields:
                    field = field_name.lower()
                field = field.replace(" ", "_").replace("-", "_")

            if isinstance(obj, dict):
                data = obj.get(field, "")
            else:
                data = getattr(obj, field, "")

        # convert dict to str to check length
        if isinstance(data, (dict, list)):
            data = json.dumps(data)
        if wrap > 0:
            data = textwrap.fill(str(data), wrap)
        # if value has a newline, add in multiple rows
        # e.g. fault with stacktrace
        if (data and isinstance(data, str)
                and (r"\n" in data or "\r" in data)):
            # "\r" would break the table, so remove it.
            if "\r" in data:
                data = data.replace("\r", "")
            lines = data.strip().split(r"\n")
            col1 = field_name
            for line in lines:
                pt.add_row([col1, line])
                col1 = ""
        else:
            if data is None:
                data = "-"
            pt.add_row([field_name, data])

    table_body = pt.get_string(header=print_header,
                               border=print_border) + "\n"

    table_header = ""

    if table_label:
        table_width = table_body.index("\n")
        table_header = make_table_header(table_label, table_width)
        table_header += "\n"

    if table_header:
        out.write(encodeutils.safe_encode(table_header).decode())
    out.write(encodeutils.safe_encode(table_body).decode())
示例#13
0
 def __init__(self, config):
     config_file = tempfile.NamedTemporaryFile(delete=False)
     config_file.write(encodeutils.safe_encode(json.dumps(config)))
     config_file.close()
     self.filename = config_file.name
示例#14
0
def print_list(objs, fields, formatters=None, sortby_index=0,
               mixed_case_fields=None, field_labels=None,
               normalize_field_names=False,
               table_label=None, print_header=True, print_border=True,
               out=sys.stdout):
    """Print a list or objects as a table, one row per object.

    :param objs: iterable of :class:`Resource`
    :param fields: attributes that correspond to columns, in order
    :param formatters: `dict` of callables for field formatting
    :param sortby_index: index of the field for sorting table rows
    :param mixed_case_fields: fields corresponding to object attributes that
        have mixed case names (e.g., 'serverId')
    :param field_labels: Labels to use in the heading of the table, default to
        fields.
    :param normalize_field_names: If True, field names will be transformed,
        e.g. "Field Name" -> "field_name", otherwise they will be used
        unchanged.
    :param table_label: Label to use as header for the whole table.
    :param print_header: print table header.
    :param print_border: print table border.
    :param out: stream to write output to.

    """
    formatters = formatters or {}
    mixed_case_fields = mixed_case_fields or []
    field_labels = field_labels or fields
    if len(field_labels) != len(fields):
        raise ValueError("Field labels list %(labels)s has different number of"
                         " elements than fields list %(fields)s"
                         % {"labels": field_labels, "fields": fields})

    if sortby_index is None:
        kwargs = {}
    else:
        kwargs = {"sortby": field_labels[sortby_index]}
    pt = prettytable.PrettyTable(field_labels)
    pt.align = "l"

    for o in objs:
        row = []
        for field in fields:
            if field in formatters:
                row.append(formatters[field](o))
            else:
                field_name = field

                if normalize_field_names:
                    if field_name not in mixed_case_fields:
                        field_name = field_name.lower()
                    field_name = field_name.replace(" ", "_").replace("-", "_")

                if isinstance(o, dict):
                    data = o.get(field_name, "")
                else:
                    data = getattr(o, field_name, "")
                row.append(data)
        pt.add_row(row)

    if not print_border or not print_header:
        pt.set_style(prettytable.PLAIN_COLUMNS)
        pt.left_padding_width = 0
        pt.right_padding_width = 1

    table_body = pt.get_string(header=print_header,
                               border=print_border,
                               **kwargs) + "\n"

    table_header = ""

    if table_label:
        table_width = table_body.index("\n")
        table_header = make_table_header(table_label, table_width)
        table_header += "\n"

    if six.PY3:
        if table_header:
            out.write(encodeutils.safe_encode(table_header).decode())
        out.write(encodeutils.safe_encode(table_body).decode())
    else:
        if table_header:
            out.write(encodeutils.safe_encode(table_header))
        out.write(encodeutils.safe_encode(table_body))
示例#15
0
def print_dict(obj, fields=None, formatters=None, mixed_case_fields=False,
               normalize_field_names=False, property_label="Property",
               value_label="Value", table_label=None, print_header=True,
               print_border=True, wrap=0, out=sys.stdout):
    """Print dict as a table.

    :param obj: dict to print
    :param fields: `dict` of keys to print from d. Defaults to all keys
    :param formatters: `dict` of callables for field formatting
    :param mixed_case_fields: fields corresponding to object attributes that
        have mixed case names (e.g., 'serverId')
    :param normalize_field_names: If True, field names will be transformed,
        e.g. "Field Name" -> "field_name", otherwise they will be used
        unchanged.
    :param property_label: label of "property" column
    :param value_label: label of "value" column
    :param table_label: Label to use as header for the whole table.
    :param print_header: print table header.
    :param print_border: print table border.
    :param out: stream to write output to.
    """
    formatters = formatters or {}
    mixed_case_fields = mixed_case_fields or []
    if not fields:
        if isinstance(obj, dict):
            fields = sorted(obj.keys())
        else:
            fields = [name for name in dir(obj)
                      if (not name.startswith("_") and
                          not callable(getattr(obj, name)))]

    pt = prettytable.PrettyTable([property_label, value_label], caching=False)
    pt.align = "l"
    for field_name in fields:
        if field_name in formatters:
            data = formatters[field_name](obj)
        else:
            field = field_name
            if normalize_field_names:
                if field not in mixed_case_fields:
                    field = field_name.lower()
                field = field.replace(" ", "_").replace("-", "_")

            if isinstance(obj, dict):
                data = obj.get(field, "")
            else:
                data = getattr(obj, field, "")

        # convert dict to str to check length
        if isinstance(data, (dict, list)):
            data = json.dumps(data)
        if wrap > 0:
            data = textwrap.fill(six.text_type(data), wrap)
        # if value has a newline, add in multiple rows
        # e.g. fault with stacktrace
        if (data and
                isinstance(data, six.string_types) and
                (r"\n" in data or "\r" in data)):
            # "\r" would break the table, so remove it.
            if "\r" in data:
                data = data.replace("\r", "")
            lines = data.strip().split(r"\n")
            col1 = field_name
            for line in lines:
                pt.add_row([col1, line])
                col1 = ""
        else:
            if data is None:
                data = "-"
            pt.add_row([field_name, data])

    table_body = pt.get_string(header=print_header,
                               border=print_border) + "\n"

    table_header = ""

    if table_label:
        table_width = table_body.index("\n")
        table_header = make_table_header(table_label, table_width)
        table_header += "\n"

    if six.PY3:
        if table_header:
            out.write(encodeutils.safe_encode(table_header).decode())
        out.write(encodeutils.safe_encode(table_body).decode())
    else:
        if table_header:
            out.write(encodeutils.safe_encode(table_header))
        out.write(encodeutils.safe_encode(table_body))
示例#16
0
 def test_safe_encode_force_incoming_utf8_to_ascii(self):
     # Forcing incoming to ascii so it falls back to utf-8
     self.assertEqual(
         six.b("ni\xc3\xb1o"),
         encodeutils.safe_encode(six.b("ni\xc3\xb1o"), incoming="ascii"),
     )