Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 165 lines (147 sloc) 4.375 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
import logging
import re
import string
import time
from django.core.handlers.wsgi import WSGIRequest
from django.conf import settings
from utils.user_functions import extract_user_agent
from apps.statistics.rstats import RStats

class NullHandler(logging.Handler): #exists in python 3.1
    def emit(self, record):
        pass

def getlogger():
    logger = logging.getLogger('newsblur')
    return logger

def user(u, msg, request=None, warn_color=True):
    if not u:
        return debug(msg)
        
    platform = '------'
    time_elapsed = ""
    if isinstance(u, WSGIRequest) or request:
        if not request:
            request = u
            u = request.user
        platform = extract_user_agent(request)

        if hasattr(request, 'start_time'):
            seconds = time.time() - request.start_time
            color = '~FB'
            if warn_color:
                if seconds >= 1:
                    color = '~FR'
                elif seconds > .2:
                    color = '~SB~FK'
            time_elapsed = "[%s%.4ss~SB] " % (
                color,
                seconds,
            )
    is_premium = u.is_authenticated() and u.profile.is_premium
    premium = '*' if is_premium else ''
    username = cipher(unicode(u)) if settings.CIPHER_USERNAMES else unicode(u)
    info(' ---> [~FB~SN%-6s~SB] %s[%s%s] %s' % (platform, time_elapsed, username, premium, msg))
    page_load_paths = [
        "/reader/feed/",
        "/social/stories/",
        "/reader/river_stories/",
        "/social/river_stories/"
    ]
    if request:
        path = RStats.clean_path(request.path)
        if path in page_load_paths:
            RStats.add('page_load', duration=seconds)

def cipher(msg):
    shift = len(msg)
    in_alphabet = unicode(string.ascii_lowercase)
    out_alphabet = in_alphabet[shift:] + in_alphabet[:shift]
    translation_table = dict((ord(ic), oc) for ic, oc in zip(in_alphabet, out_alphabet))

    return msg.translate(translation_table)
    
def debug(msg):
    logger = getlogger()
    logger.debug(colorize(msg))

def info(msg):
    logger = getlogger()
    logger.info(colorize(msg))

def error(msg):
    logger = getlogger()
    logger.error(msg)
    
def colorize(msg):
    params = {
        r'\-\-\->' : '~FB~SB--->~FW',
        r'\*\*\*>' : '~FB~SB~BB--->~BT~FW',
        r'\[' : '~SB~FB[~SN~FM',
        r'AnonymousUser' : '~FBAnonymousUser',
        r'\*(\s*)~FB~SB\]' : r'~SN~FR*\1~FB~SB]',
        r'\]' : '~FB~SB]~FW~SN',
    }
    colors = {
        '~SB' : Style.BRIGHT,
        '~SN' : Style.NORMAL,
        '~SK' : Style.BLINK,
        '~SU' : Style.UNDERLINE,
        '~ST' : Style.RESET_ALL,
        '~FK': Fore.BLACK,
        '~FR': Fore.RED,
        '~FG': Fore.GREEN,
        '~FY': Fore.YELLOW,
        '~FB': Fore.BLUE,
        '~FM': Fore.MAGENTA,
        '~FC': Fore.CYAN,
        '~FW': Fore.WHITE,
        '~FT': Fore.RESET,
        '~BK': Back.BLACK,
        '~BR': Back.RED,
        '~BG': Back.GREEN,
        '~BY': Back.YELLOW,
        '~BB': Back.BLUE,
        '~BM': Back.MAGENTA,
        '~BC': Back.CYAN,
        '~BW': Back.WHITE,
        '~BT': Back.RESET,
    }
    for k, v in params.items():
        msg = re.sub(k, v, msg)
    msg = msg + '~ST~FW~BT'
    # msg = re.sub(r'(~[A-Z]{2})', r'%(\1)s', msg)
    for k, v in colors.items():
        msg = msg.replace(k, v)
    return msg
    
'''
This module generates ANSI character codes to printing colors to terminals.
See: http://en.wikipedia.org/wiki/ANSI_escape_code
'''

COLOR_ESC = '\033['

class AnsiCodes(object):
    def __init__(self, codes):
        for name in dir(codes):
            if not name.startswith('_'):
                value = getattr(codes, name)
                setattr(self, name, COLOR_ESC + str(value) + 'm')

class AnsiFore:
    BLACK = 30
    RED = 31
    GREEN = 32
    YELLOW = 33
    BLUE = 34
    MAGENTA = 35
    CYAN = 36
    WHITE = 37
    RESET = 39

class AnsiBack:
    BLACK = 40
    RED = 41
    GREEN = 42
    YELLOW = 43
    BLUE = 44
    MAGENTA = 45
    CYAN = 46
    WHITE = 47
    RESET = 49

class AnsiStyle:
    BRIGHT = 1
    DIM = 2
    UNDERLINE = 4
    BLINK = 5
    NORMAL = 22
    RESET_ALL = 0

Fore = AnsiCodes(AnsiFore)
Back = AnsiCodes(AnsiBack)
Style = AnsiCodes(AnsiStyle)
Something went wrong with that request. Please try again.