MCPcopy Create free account
hub / github.com/dbcli/mssql-cli / format_output

Method format_output

mssqlcli/mssql_cli.py:754–821  ·  view source on GitHub ↗
(title, cur, headers, status, settings)

Source from the content-addressed store, hash-verified

752
753 @staticmethod
754 def format_output(title, cur, headers, status, settings):
755 # pylint: disable=too-many-locals
756
757 output = []
758 expanded = (settings.expanded or settings.table_format == 'vertical')
759 table_format = ('vertical' if settings.expanded else
760 settings.table_format)
761 max_width = settings.max_width
762 case_function = settings.case_function
763 formatter = TabularOutputFormatter(format_name=table_format)
764
765 def format_array(val):
766 if val is None:
767 return settings.missingval
768 if not isinstance(val, list):
769 return val
770 return '{' + ','.join(text_type(format_array(e)) for e in val) + '}'
771
772 def format_arrays(data, headers, **_):
773 data = list(data)
774 for row in data:
775 row[:] = [
776 format_array(val) if isinstance(val, list) else val
777 for val in row
778 ]
779
780 return data, headers
781
782 output_kwargs = {
783 'sep_title': 'RECORD {n}',
784 'sep_character': '-',
785 'sep_length': (1, 25),
786 'missing_value': settings.missingval,
787 'integer_format': settings.dcmlfmt,
788 'float_format': settings.floatfmt,
789 'preprocessors': (format_numbers, format_arrays),
790 'disable_numparse': True,
791 'preserve_whitespace': True
792 }
793 if not settings.floatfmt:
794 output_kwargs['preprocessors'] = (align_decimals, )
795
796 if title:
797 output.append(title)
798
799 if cur:
800 headers = [case_function(x) for x in headers]
801 if max_width is not None:
802 cur = list(cur)
803 formatted = formatter.format_output(cur, headers, **output_kwargs)
804 if isinstance(formatted, text_type):
805 formatted = iter(formatted.splitlines())
806 first_line = next(formatted)
807 formatted = itertools.chain([first_line], formatted)
808
809 if (not expanded and max_width and len(
810 first_line) > max_width and headers):
811 formatted = formatter.format_output(

Calls

no outgoing calls