Exemplo n.º 1
0
    def __init__(self,
                 handlers=None,
                 headers=None,
                 user_agent='urllib2',
                 support_cookies=True,
                 support_gzip=True,
                 add_accept_headers=True,
                 keepalive=True,
                 timeout=None,
                 max_redirects=None,
                 max_read=None):

        if handlers is None:
            handlers = []
        if headers is None:
            headers = []
        if max_redirects is None:
            max_redirects = DEFAULT_MAX_REDIRECTS
        if max_read is None:
            max_read = -1

        # add cookie processor to handlers if we want cookie support
        if support_cookies:
            self.cookie_jar = cookielib.CookieJar()
            handlers.append(urllib2.HTTPCookieProcessor(self.cookie_jar))
        else:
            self.cookie_jar = None

        # optional headers to appear more like a real browser
        if user_agent is not None:
            try:
                user_agent = PRESET_USER_AGENTS[user_agent]
            except KeyError:
                pass
            headers.append(('User-Agent', user_agent))
        if support_gzip:
            headers.append(('Accept-Encoding', 'gzip, deflate'))
        if add_accept_headers:
            headers.extend((('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
                            ('Accept-Language', 'en-us,en;q=0.5'),
                            ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')))

        if keepalive:
            headers.append(('Connection', 'keep-alive'))

        # build the opener
        self.max_redirects = max_redirects
        self.max_read = max_read
        self.timeout = timeout
        self.opener = urllib2.build_opener(*handlers)
        self.opener.addheaders = self.headers = headers

        if etree is None:
            self.xml_parser = None
        else:
            self.xml_parser = etree.XMLParser(encoding=text.get_encoding(), ns_clean=True,
                                              recover=True, remove_blank_text=True, strip_cdata=False)
Exemplo n.º 2
0
def get_logger(name, level=None, stream=None, append=False, dir=None,
               max_files=None, perms=None, date_format=None, record_format=None):

    """Get a named logger configured to use the site log directory"""

    from gruntle.memebot.exceptions import trapped
    from gruntle.memebot.utils import text
    from django.conf import settings

    if level is None:
        level = settings.LOG_LEVEL
    if dir is None:
        dir = settings.LOG_DIR
    if max_files is None:
        max_files = settings.LOG_MAX_FILES
    if perms is None:
        perms = settings.LOG_PERMS
    if date_format is None:
        date_format = settings.LOG_DATE_FORMAT
    if record_format is None:
        record_format = settings.LOG_RECORD_FORMAT

    if not os.path.exists(dir):
        os.makedirs(dir)

    if append:
        file = os.path.join(dir, name + '.log')
        if not os.path.exists(file):
            fd = os.open(file, tempfile._text_openflags, perms)
            os.close(fd)

    else:
        datestamp = datetime.date.today().strftime('%Y%m%d')
        fmt = '%%0%dd' % len(str(max_files - 1))
        for i in xrange(max_files):
            file = os.path.join(dir, '.'.join((name, datestamp, fmt % i, 'log')))
            with trapped:
                fd = os.open(file, tempfile._text_openflags, perms)
                os.close(fd)
                break
        else:
            raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), file)

    logger = logging.Logger(binascii.hexlify(file), level=level)
    formatter = logging.Formatter(record_format, date_format)

    handler = logging.FileHandler(file, encoding=text.get_encoding())
    handler.setFormatter(formatter)
    handler.setLevel(level)
    logger.addHandler(handler)

    if stream is not None:
        handler = logging.StreamHandler(stream)
        handler.setFormatter(formatter)
        handler.setLevel(level)
        logger.addHandler(handler)

    class LogWrapper(object):

        """Add/change some functionality to stdlib's logger"""

        WRAP_FUNCS = 'debug', 'info', 'warn', 'warning', 'error', 'fatal', 'critical', 'exception'

        def __init__(self, name=None):
            self.name = name

        @property
        def prefix(self):
            if self.name:
                return text.decode(text.format('[%s] ', self.name))
            return u''

        def __getattribute__(self, key):
            try:
                return super(LogWrapper, self).__getattribute__(key)
            except AttributeError:
                wrapped_func = getattr(logger, key)
                if key in type(self).WRAP_FUNCS:

                    @functools.wraps(wrapped_func)
                    def wrapper_func(*args, **kwargs):
                        exc_info = kwargs.pop('exc_info', None)
                        output = self.prefix + text.decode(text.format(*args, **kwargs))
                        wrapped_func(output)
                        if exc_info is not None:
                            for line in traceback.format_exception(*exc_info):
                                wrapped_func(self.prefix + text.chomp(text.decode(line)))

                    return wrapper_func
                else:
                    return wrapped_func

        def get_named_logger(self, name):
            """Return named interface to this logger"""
            return type(self)(name)

        def __repr__(self):
            return '<Logger: %s%s>' % (name, ' (%s)' % self.name if self.name else '')

    return LogWrapper()
Exemplo n.º 3
0
def get_logger(name,
               level=None,
               stream=None,
               append=False,
               dir=None,
               max_files=None,
               perms=None,
               date_format=None,
               record_format=None):
    """Get a named logger configured to use the site log directory"""

    from gruntle.memebot.exceptions import trapped
    from gruntle.memebot.utils import text
    from django.conf import settings

    if level is None:
        level = settings.LOG_LEVEL
    if dir is None:
        dir = settings.LOG_DIR
    if max_files is None:
        max_files = settings.LOG_MAX_FILES
    if perms is None:
        perms = settings.LOG_PERMS
    if date_format is None:
        date_format = settings.LOG_DATE_FORMAT
    if record_format is None:
        record_format = settings.LOG_RECORD_FORMAT

    if not os.path.exists(dir):
        os.makedirs(dir)

    if append:
        file = os.path.join(dir, name + '.log')
        if not os.path.exists(file):
            fd = os.open(file, tempfile._text_openflags, perms)
            os.close(fd)

    else:
        datestamp = datetime.date.today().strftime('%Y%m%d')
        fmt = '%%0%dd' % len(str(max_files - 1))
        for i in xrange(max_files):
            file = os.path.join(dir, '.'.join(
                (name, datestamp, fmt % i, 'log')))
            with trapped:
                fd = os.open(file, tempfile._text_openflags, perms)
                os.close(fd)
                break
        else:
            raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), file)

    logger = logging.Logger(binascii.hexlify(file), level=level)
    formatter = logging.Formatter(record_format, date_format)

    handler = logging.FileHandler(file, encoding=text.get_encoding())
    handler.setFormatter(formatter)
    handler.setLevel(level)
    logger.addHandler(handler)

    if stream is not None:
        handler = logging.StreamHandler(stream)
        handler.setFormatter(formatter)
        handler.setLevel(level)
        logger.addHandler(handler)

    class LogWrapper(object):
        """Add/change some functionality to stdlib's logger"""

        WRAP_FUNCS = 'debug', 'info', 'warn', 'warning', 'error', 'fatal', 'critical', 'exception'

        def __init__(self, name=None):
            self.name = name

        @property
        def prefix(self):
            if self.name:
                return text.decode(text.format('[%s] ', self.name))
            return u''

        def __getattribute__(self, key):
            try:
                return super(LogWrapper, self).__getattribute__(key)
            except AttributeError:
                wrapped_func = getattr(logger, key)
                if key in type(self).WRAP_FUNCS:

                    @functools.wraps(wrapped_func)
                    def wrapper_func(*args, **kwargs):
                        exc_info = kwargs.pop('exc_info', None)
                        output = self.prefix + text.decode(
                            text.format(*args, **kwargs))
                        wrapped_func(output)
                        if exc_info is not None:
                            for line in traceback.format_exception(*exc_info):
                                wrapped_func(self.prefix +
                                             text.chomp(text.decode(line)))

                    return wrapper_func
                else:
                    return wrapped_func

        def get_named_logger(self, name):
            """Return named interface to this logger"""
            return type(self)(name)

        def __repr__(self):
            return '<Logger: %s%s>' % (name, ' (%s)' %
                                       self.name if self.name else '')

    return LogWrapper()