Example #1
0
def as_wrapping_formatters(objs, fields, field_labels, formatters, no_wrap=None, no_wrap_fields=[]):
    """This function is the entry point for building the "best guess"
       word wrapping formatters.  A best guess formatter guesses what the best
       columns widths should be for the table celldata.  It does this by collecting
       various stats on the celldata (min, max average width of column celldata) and from
       this celldata decides the desired widths and the minimum widths.

       Given a list of formatters and the list of objects (objs),  this function
       first determines if we need to augment the passed formatters with word wrapping
       formatters.  If the no_wrap parameter or global no_wrap flag is set,
       then we do not build wrapping formatters.  If any of the formatters within formatters
       is a word wrapping formatter, then it is assumed no more wrapping is required.

    :param objs:
    :param fields:
    :param field_labels:
    :param formatters:
    :param no_wrap:
    :param no_wrap_fields:
    :return: When no wrapping is required, the formatters parameter is returned
              -- effectively a NOOP in this case

              When wrapping is required, best-guess word wrapping formatters are returned
              with original parameter formatters embedded in the word wrapping formatters
    """
    no_wrap = is_nowrap_set(no_wrap)

    if not needs_wrapping_formatters(formatters, no_wrap):
        return formatters

    format_spec = build_best_guess_formatters_using_average_widths(objs, fields, field_labels, formatters, no_wrap_fields)

    formatters = build_wrapping_formatters(objs, fields, field_labels, format_spec)

    return formatters
def set_no_wrap_on_formatters(no_wrap, formatters):
    """
       Purpose of this function is to temporarily force
       the no_wrap setting for the formatters parameter.
       returns orig_no_wrap_settings defined for each formatter
       Use unset_no_wrap_on_formatters(orig_no_wrap_settings) to undo what
       this function does
    """
    # handle easy case:
    if not formatters:
        return {}

    formatter_no_wrap_settings = {}

    global_orig_no_wrap = is_nowrap_set()
    set_no_wrap(no_wrap)

    for k, f in formatters.items():
        if WrapperFormatter.is_wrapper_formatter(f):
            formatter_no_wrap_settings[k] = (f.wrapper_formatter.no_wrap,
                                             f.wrapper_formatter)
            f.wrapper_formatter.no_wrap = no_wrap

    return {
        "global_orig_no_wrap": global_orig_no_wrap,
        "formatter_no_wrap_settings": formatter_no_wrap_settings
    }
Example #3
0
def _simpleTestHarness(no_wrap):

    from cgtsclient.common import utils

    def testFormatter(event):
        return "*{}".format(event["state"])

    def buildFormatter(field, width):
        def f(dict):
            if field == 'number':
                return dict[field]
            return "{}".format(dict[field]).replace("_", " ")
        return {"formatter": f, "wrapperFormatter": width}

    set_no_wrap(no_wrap)

    field_labels = ['Time Stamp', 'State', 'Event Log ID', 'Reason Text',
                    'Entity Instance ID', 'Severity', 'Number']
    fields = ['timestamp', 'state', 'event_log_id', 'reason_text',
              'entity_instance_id', 'severity', 'number']

    formatterSpecX = {"timestamp": 10,
                      "state": 8,
                      "event_log_id": 70,
                      "reason_text": 30,
                      "entity_instance_id": 30,
                      "severity": 12,
                      "number": 4}

    formatterSpec = {}
    for f in fields:
        formatterSpec[f] = buildFormatter(f, formatterSpecX[f])

    logs = []
    for i in range(0, 30):
        log = {}
        for f in fields:
            if f == 'number':
                log[f] = i
            else:
                log[f] = "{}{}".format(f, i)
        logs.append(utils.objectify(log))

    formatterSpec = formatterSpecX

    formatters = build_wrapping_formatters(logs, fields, field_labels, formatterSpec)

    utils.print_list(logs, fields, field_labels, formatters=formatters, sortby=6,
                     reversesort=True, no_wrap_fields=['entity_instance_id'])

    print("nowrap = {}".format(is_nowrap_set()))
Example #4
0
def needs_wrapping_formatters(formatters, no_wrap=None):
    no_wrap = is_nowrap_set(no_wrap)
    if no_wrap:
        return False

    # handle easy case:
    if not formatters:
        return True

    # If we have at least one wrapping formatter,
    # then we assume we don't need to wrap
    for f in formatters.values():
        if WrapperFormatter.is_wrapper_formatter(f):
            return False

    # looks like we need wrapping
    return True