lib/util/log.py
def smart_truncate(basestr, maxlen):
    '''
    Truncates a string while trying to maintain human-readability. The logic is
    quite limited. It can only truncate a few characters intelligently. If that
    still isn't enough, certain parts will be arbitrarily removed...
    '''
    assert isinstance(basestr, str), \
        'basestr must be a string: %r' % basestr
    assert isinstance(maxlen, int) and maxlen > 0, \
        'invalid maximum length: %r' % maxlen

    # If it looks like a path, strip away the common prefix
    has_sep = os.path.sep and os.path.sep in basestr
    has_altsep = os.path.altsep and os.path.altsep in basestr
    if has_sep or has_altsep:
        # Yes, looks like a path
        basestr = strip_common_path_prefix(basestr)

    if len(basestr) <= maxlen:
        return basestr

    name_components = delimit_name_components(basestr)

    # Algorithm notes:
    # Loop through the name components, first applying soft truncation from
    # start to end. If, at any point, the length condition is satisfied, return
    # the reconstructed list. If, at the end, the length condition is not
    # satisfied, start again, applying hard truncation. Finally, if that
    # doesn't work, start dropping components altogether
    current_components = name_components[:]

    def get_current_length():
        ''' Returns the sum of the length of each component. '''
        return sum(map(len, current_components))

    def get_reconstructed():
        ''' Returns the string reconstructed from joining the components. '''
        return ''.join(current_components)

    def apply_truncate_until_satisfied(truncatefn):
        '''
        Applies the specified truncation function to the current working
        components until the length requirement is satisfied. Returns `True` if
        successful, and `False` if it was not enough. Either way, the current
        component state is updated after the application.
        '''
        for i, component in enumerate(current_components):
            truncated_component = truncatefn(component)
            current_components[i] = truncated_component
            if get_current_length() <= maxlen:
                return True
        return False

    if apply_truncate_until_satisfied(truncate_word_soft):
        return get_reconstructed()

    assert get_current_length() > maxlen, \
        '[internal] soft-truncate loop did not terminate properly, ' \
        'length condition already satisfied, currently at: %r' % \
        get_current_length()

    if apply_truncate_until_satisfied(truncate_word_hard):
        return get_reconstructed()

    assert get_current_length() > maxlen, \
        '[internal] hard-truncate loop did not terminate properly, ' \
        'length condition already satisfied, currently at: %r' % \
        get_current_length()

    # Well.... no choice but to remove components
    while current_components:
        del current_components[0]
        if current_components:
            # Also remove the non-word component following it
            del current_components[0]
        if get_current_length() <= maxlen:
            return get_reconstructed()

    assert False, \
        '[internal] failed to truncate, even after removing all components...'
    return ''

    Truncates a string while trying to maintain human-readability. The logic is
    quite limited. It can only truncate a few characters intelligently. If that
    still isn't enough, certain parts will be arbitrarily removed...
    
Truncates a string while trying to maintain human-readability. The logic is
quite limited. It can only truncate a few characters intelligently. If that
still isn't enough, certain parts will be arbitrarily removed
Truncates a string while trying to maintain human - readability . The logic is quite limited . It can only truncate a few characters intelligently . If that still isn ' t enough certain parts will be arbitrarily removed
def smart_truncate(basestr, maxlen):
    assert isinstance(basestr, str), \
        'basestr must be a string: %r' % basestr
    assert isinstance(maxlen, int) and maxlen > 0, \
        'invalid maximum length: %r' % maxlen
    has_sep = os.path.sep and os.path.sep in basestr
    has_altsep = os.path.altsep and os.path.altsep in basestr
    if has_sep or has_altsep:
        basestr = strip_common_path_prefix(basestr)
    if len(basestr) <= maxlen:
        return basestr
    name_components = delimit_name_components(basestr)
    current_components = name_components[:]
    def get_current_length():
        return sum(map(len, current_components))
    def get_reconstructed():
        return ''.join(current_components)
    def apply_truncate_until_satisfied(truncatefn):
        for i, component in enumerate(current_components):
            truncated_component = truncatefn(component)
            current_components[i] = truncated_component
            if get_current_length() <= maxlen:
                return True
        return False
    if apply_truncate_until_satisfied(truncate_word_soft):
        return get_reconstructed()
    assert get_current_length() > maxlen, \
        '[internal] soft-truncate loop did not terminate properly, ' \
        'length condition already satisfied, currently at: %r' % \
        get_current_length()
    if apply_truncate_until_satisfied(truncate_word_hard):
        return get_reconstructed()
    assert get_current_length() > maxlen, \
        '[internal] hard-truncate loop did not terminate properly, ' \
        'length condition already satisfied, currently at: %r' % \
        get_current_length()
    while current_components:
        del current_components[0]
        if current_components:
            del current_components[0]
        if get_current_length() <= maxlen:
            return get_reconstructed()
    assert False, \
        '[internal] failed to truncate, even after removing all components...'
    return ''
def smart_truncate ( basestr , maxlen ) : assert isinstance ( basestr , str ) , 'basestr must be a string: %r' % basestr assert isinstance ( maxlen , int ) and maxlen > 0 , 'invalid maximum length: %r' % maxlen has_sep = os . path . sep and os . path . sep in basestr has_altsep = os . path . altsep and os . path . altsep in basestr if has_sep or has_altsep : basestr = strip_common_path_prefix ( basestr ) if len ( basestr ) <= maxlen : return basestr name_components = delimit_name_components ( basestr ) current_components = name_components [ : ] def get_current_length ( ) : ''' Returns the sum of the length of each component. ''' return sum ( map ( len , current_components ) ) def get_reconstructed ( ) : ''' Returns the string reconstructed from joining the components. ''' return '' . join ( current_components ) def apply_truncate_until_satisfied ( truncatefn ) : '''
        Applies the specified truncation function to the current working
        components until the length requirement is satisfied. Returns `True` if
        successful, and `False` if it was not enough. Either way, the current
        component state is updated after the application.
        ''' for i , component in enumerate ( current_components ) : truncated_component = truncatefn ( component ) current_components [ i ] = truncated_component if get_current_length ( ) <= maxlen : return True return False if apply_truncate_until_satisfied ( truncate_word_soft ) : return get_reconstructed ( ) assert get_current_length ( ) > maxlen , '[internal] soft-truncate loop did not terminate properly, ' 'length condition already satisfied, currently at: %r' % get_current_length ( ) if apply_truncate_until_satisfied ( truncate_word_hard ) : return get_reconstructed ( ) assert get_current_length ( ) > maxlen , '[internal] hard-truncate loop did not terminate properly, ' 'length condition already satisfied, currently at: %r' % get_current_length ( ) while current_components : del current_components [ 0 ] if current_components : del current_components [ 0 ] if get_current_length ( ) <= maxlen : return get_reconstructed ( ) assert False , '[internal] failed to truncate, even after removing all components...' return ''
Truncates a string while trying to maintain human-readability.
Truncates a string while trying to maintain human - readability .
'''
    Truncates a string while trying to maintain human-readability. The logic is
    quite limited. It can only truncate a few characters intelligently. If that
    still isn't enough, certain parts will be arbitrarily removed...
    ''' # If it looks like a path, strip away the common prefix # Yes, looks like a path # Algorithm notes: # Loop through the name components, first applying soft truncation from # start to end. If, at any point, the length condition is satisfied, return # the reconstructed list. If, at the end, the length condition is not # satisfied, start again, applying hard truncation. Finally, if that # doesn't work, start dropping components altogether ''' Returns the sum of the length of each component. ''' ''' Returns the string reconstructed from joining the components. ''' '''
        Applies the specified truncation function to the current working
        components until the length requirement is satisfied. Returns `True` if
        successful, and `False` if it was not enough. Either way, the current
        component state is updated after the application.
        ''' # Well.... no choice but to remove components # Also remove the non-word component following it
basestr
maxlen