Beispiel #1
0
    def print_response(
        self,
        result,
        json=True,
        format=None,  # pylint: disable=redefined-builtin
        drop_fields=None,
        table_layout=None,
        single_item=False,
        header=True,
        csv=False,
        file=None,
    ):  # pylint: disable=redefined-builtin
        """print request response in chosen format"""
        if file is None:
            file = sys.stdout

        if format is not None:
            for item in result:
                print(format.format(**item), file=file)
        elif json:
            print(
                jsonlib.dumps(result,
                              indent=4,
                              sort_keys=True,
                              cls=pretty.CustomJsonEncoder),
                file=file,
            )
        elif csv:
            fields = []
            for field in table_layout:
                if isinstance(field, str):
                    fields.append(field)
                else:
                    fields.extend(field)

            writer = csvlib.DictWriter(file,
                                       extrasaction="ignore",
                                       fieldnames=fields)
            if header:
                writer.writeheader()
            for item in result:
                writer.writerow(item)
        else:
            if single_item:
                result = [result]

            pretty.print_table(
                result,
                drop_fields=drop_fields,
                table_layout=table_layout,
                header=header,
                file=file,
            )
Beispiel #2
0
    def print_response(self, result, json=True, format=None,   # pylint: disable=redefined-builtin
                       drop_fields=None, table_layout=None, single_item=False):
        """print request response in chosen format"""
        if format is not None:
            for item in result:
                print(format.format(**item))
        elif json:
            print(jsonlib.dumps(result, indent=4, sort_keys=True))
        else:
            if single_item:
                result = [result]

            pretty.print_table(result, drop_fields=drop_fields, table_layout=table_layout)
Beispiel #3
0
    def print_response(
            self,
            result,
            json=True,
            format=None,  # pylint: disable=redefined-builtin
            drop_fields=None,
            table_layout=None,
            single_item=False):
        """print request response in chosen format"""
        if format is not None:
            for item in result:
                print(format.format(**item))
        elif json:
            print(jsonlib.dumps(result, indent=4, sort_keys=True))
        else:
            if single_item:
                result = [result]

            pretty.print_table(result,
                               drop_fields=drop_fields,
                               table_layout=table_layout)