예제 #1
0
def HandleUnicodeError(log_header,
                       e,
                       guid,
                       title,
                       action='',
                       attempt=1,
                       content=None,
                       field=None,
                       attempt_max=3,
                       attempt_min=1):
    global lastUnicodeError
    object = ""
    e_type = e.__class__.__name__
    is_unicode = e_type.find("Unicode") > -1
    if is_unicode:
        content_type = e.object.__class__.__name__
        object = e.object[e.start - 20:e.start + 20]
    elif not content:
        content = "Not Provided"
        content_type = "N/A"
    else:
        content_type = content.__class__.__name__
    log_header += ': ' + e_type + ': {field}' + content_type + (
        ' <%s>' % action if action else '')
    save_header = log_header.replace('{field}', '') + ': ' + title
    log_header = log_header.format(field='%s: ' % field if field else '')

    new_error = lastUnicodeError != save_header

    if is_unicode:
        return_val = 1 if attempt < attempt_max else -1
        if new_error:
            log(save_header + '\n' + '-' * ANKNOTES.FORMATTING.LINE_LENGTH,
                'unicode',
                replace_newline=False)
            lastUnicodeError = save_header
        log(ANKNOTES.FORMATTING.TIMESTAMP_PAD + '\t - ' +
            (('Field %s' % field if field else 'Unknown Field') +
             ': ').ljust(20) + str_safe(object),
            'unicode',
            timestamp=False)
    else:
        return_val = 0
        if attempt is 1 and content:
            log_dump(content, log_header, 'NonUnicodeErrors')
    if (new_error and attempt >= attempt_min) or not is_unicode:
        log_error(log_header +
                  "\n -  Error: %s\n -   GUID: %s\n -  Title: %s%s" %
                  (str(e), guid, str_safe(title),
                   '' if not object else "\n - Object: %s" % str_safe(object)))
    return return_val
예제 #2
0
def try_print(full_path, content, prefix='', line_prefix=u'\n ', attempt=0, clear=False, timestamp=True, **kwargs):
    try:
        st = '[%s]: ' % datetime.now().strftime(ANKNOTES.DATE_FORMAT) if timestamp else ''
        print_content = line_prefix + (u' <%d>' % attempt if attempt > 0 else u'') + u' ' + st
        if attempt is 0:
            print_content += content
        elif attempt is 1:
            print_content += decode(content)
        elif attempt is 2:
            print_content += encode(content)
        elif attempt is 3:
            print_content = encode(print_content) + encode(content)
        elif attempt is 4:
            print_content = decode(print_content) + decode(content)
        elif attempt is 5:
            print_content += "Error printing content: " + str_safe(content)
        elif attempt is 6:
            print_content += "Error printing content: " + content[:10]
        elif attempt is 7:
            print_content += "Unable to print content."
        with open(full_path, 'w+' if clear else 'a+') as fileLog:
            print>> fileLog, print_content
    except Exception as e:
        if attempt < 8:
            try_print(full_path, content, prefix=prefix, line_prefix=line_prefix, attempt=attempt + 1,
                                  clear=clear)
        else:
            log("Try print error to %s: %s" % (os.path.split(full_path)[1], str(e)))
예제 #3
0
파일: error.py 프로젝트: holycrepe/anknotes
def HandleUnicodeError(log_header, e, guid, title, action='', attempt=1, content=None, field=None, attempt_max=3,
                       attempt_min=1):
    global lastUnicodeError
    object = ""
    e_type = e.__class__.__name__
    is_unicode = e_type.find("Unicode") > -1
    if is_unicode:
        content_type = e.object.__class__.__name__
        object = e.object[e.start - 20:e.start + 20]
    elif not content:
        content = "Not Provided"
        content_type = "N/A"
    else:
        content_type = content.__class__.__name__
    log_header += ': ' + e_type + ': {field}' + content_type + (' <%s>' % action if action else '')
    save_header = log_header.replace('{field}', '') + ': ' + title
    log_header = log_header.format(field='%s: ' % field if field else '')

    new_error = lastUnicodeError != save_header

    if is_unicode:
        return_val = 1 if attempt < attempt_max else -1
        if new_error:
            log(save_header + '\n' + '-' * ANKNOTES.FORMATTING.LINE_LENGTH, 'unicode', replace_newline=False)
            lastUnicodeError = save_header
        log(ANKNOTES.FORMATTING.TIMESTAMP_PAD + '\t - ' + (
            ('Field %s' % field if field else 'Unknown Field') + ': ').ljust(20) + str_safe(object), 'unicode',
            timestamp=False)
    else:
        return_val = 0
        if attempt is 1 and content:
            log_dump(content, log_header, 'NonUnicodeErrors')
    if (new_error and attempt >= attempt_min) or not is_unicode:
        log_error(log_header + "\n -  Error: %s\n -   GUID: %s\n -  Title: %s%s" % (
            str(e), guid, str_safe(title), '' if not object else "\n - Object: %s" % str_safe(object)))
    return return_val
예제 #4
0
def try_print(full_path,
              content,
              prefix='',
              line_prefix=u'\n ',
              attempt=0,
              clear=False,
              timestamp=True,
              **kwargs):
    try:
        st = '[%s]: ' % datetime.now().strftime(
            ANKNOTES.DATE_FORMAT) if timestamp else ''
        print_content = line_prefix + (u' <%d>' % attempt
                                       if attempt > 0 else u'') + u' ' + st
        if attempt is 0:
            print_content += content
        elif attempt is 1:
            print_content += decode(content)
        elif attempt is 2:
            print_content += encode(content)
        elif attempt is 3:
            print_content = encode(print_content) + encode(content)
        elif attempt is 4:
            print_content = decode(print_content) + decode(content)
        elif attempt is 5:
            print_content += "Error printing content: " + str_safe(content)
        elif attempt is 6:
            print_content += "Error printing content: " + content[:10]
        elif attempt is 7:
            print_content += "Unable to print content."
        with open(full_path, 'w+' if clear else 'a+') as fileLog:
            print >> fileLog, print_content
    except Exception as e:
        if attempt < 8:
            try_print(full_path,
                      content,
                      prefix=prefix,
                      line_prefix=line_prefix,
                      attempt=attempt + 1,
                      clear=clear)
        else:
            log("Try print error to %s: %s" %
                (os.path.split(full_path)[1], str(e)))