<commit_before>#!/usr/bin/env python
'''
    greendns - non-blocking DNS support for Eventlet
'''

# Portions of this code taken from the gogreen project:
#   http://github.com/slideinc/gogreen
#
# Copyright (c) 2005-2010 Slide, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of the author nor the names of other
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from eventlet import patcher
from eventlet.green import _socket_nodns
from eventlet.green import time
from eventlet.green import select

__imports = []
for package in ('dns', 'dns.query', 'dns.exception', 'dns.inet',
                'dns.message', 'dns.rdatatype', 'dns.resolver', 
                'dns.reversename'):
    __imports.append('%(pkg)s = patcher.import_patched(\'%(pkg)s\', socket=_socket_nodns, time=time, select=select)' % dict(pkg=package))
exec '\n'.join(__imports)

socket = _socket_nodns

DNS_QUERY_TIMEOUT = 10.0

#
# Resolver instance used to perfrom DNS lookups.
#
class FakeAnswer(list):
   expiration = 0
class FakeRecord(object):
   pass

class ResolverProxy(object):
    def __init__(self, *args, **kwargs):
        self._resolver = None
        self._filename = kwargs.get('filename', '/etc/resolv.conf')
        self._hosts = {}
        if kwargs.pop('dev', False):
            self._load_etc_hosts()

    def _load_etc_hosts(self):
        fd = open('/etc/hosts', 'r')
        contents = fd.read()
        fd.close()
        contents = [line for line in contents.split('\n') if line and not line[0] == '#']
        for line in contents:
            line = line.replace('\t', ' ')
            parts = line.split(' ')
            parts = [p for p in parts if p]
            if not len(parts):
                continue
            ip = parts[0]
            for part in parts[1:]:
                self._hosts[part] = ip

    def clear(self):
        self._resolver = None

    def query(self, *args, **kwargs):
        if self._resolver is None:
            self._resolver = dns.resolver.Resolver(filename = self._filename)
            self._resolver.cache = dns.resolver.Cache()

        query = args[0]
        if query is None:
            args = list(args)
            query = args[0] = '0.0.0.0'
        if self._hosts and self._hosts.get(query):
            answer = FakeAnswer()
            record = FakeRecord()
            setattr(record, 'address', self._hosts[query])
            answer.append(record)
            return answer
        return self._resolver.query(*args, **kwargs)
#
# cache
#
resolver  = ResolverProxy()

def resolve(name):<commit_msg>Enabling /etc/hosts parsing by default in greendns, solves a few silly-seeming issues like not being able to look up localhost.<commit_after>#!/usr/bin/env python
'''
    greendns - non-blocking DNS support for Eventlet
'''

# Portions of this code taken from the gogreen project:
#   http://github.com/slideinc/gogreen
#
# Copyright (c) 2005-2010 Slide, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of the author nor the names of other
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from eventlet import patcher
from eventlet.green import _socket_nodns
from eventlet.green import time
from eventlet.green import select

__imports = []
for package in ('dns', 'dns.query', 'dns.exception', 'dns.inet',
                'dns.message', 'dns.rdatatype', 'dns.resolver', 
                'dns.reversename'):
    __imports.append('%(pkg)s = patcher.import_patched(\'%(pkg)s\', socket=_socket_nodns, time=time, select=select)' % dict(pkg=package))
exec '\n'.join(__imports)

socket = _socket_nodns

DNS_QUERY_TIMEOUT = 10.0

#
# Resolver instance used to perfrom DNS lookups.
#
class FakeAnswer(list):
   expiration = 0
class FakeRecord(object):
   pass

class ResolverProxy(object):
    def __init__(self, *args, **kwargs):
        self._resolver = None
        self._filename = kwargs.get('filename', '/etc/resolv.conf')
        self._hosts = {}
        if kwargs.pop('dev', False):
            self._load_etc_hosts()

    def _load_etc_hosts(self):
        try:
            fd = open('/etc/hosts', 'r')
            contents = fd.read()
            fd.close()
        except (IOError, OSError):
            return
        contents = [line for line in contents.split('\n') if line and not line[0] == '#']
        for line in contents:
            line = line.replace('\t', ' ')
            parts = line.split(' ')
            parts = [p for p in parts if p]
            if not len(parts):
                continue
            ip = parts[0]
            for part in parts[1:]:
                self._hosts[part] = ip

    def clear(self):
        self._resolver = None

    def query(self, *args, **kwargs):
        if self._resolver is None:
            self._resolver = dns.resolver.Resolver(filename = self._filename)
            self._resolver.cache = dns.resolver.Cache()

        query = args[0]
        if query is None:
            args = list(args)
            query = args[0] = '0.0.0.0'
        if self._hosts and self._hosts.get(query):
            answer = FakeAnswer()
            record = FakeRecord()
            setattr(record, 'address', self._hosts[query])
            answer.append(record)
            return answer
        return self._resolver.query(*args, **kwargs)
#
# cache
#
resolver  = ResolverProxy(dev=True)

def resolve(name):
{'fraction_non_alphanumeric': 0.09574861367837338, 'fraction_numerical': 0.005175600739371534, 'mean_word_length': 2.819294117647059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6173259', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2222, 'n_words': 833}