<commit_before># No shebang line, this module is meant to be imported
#
# This file is part of PyFarm.
# Copyright (C) 2008-2012 <NAME>
#
# PyFarm is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyFarm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PyFarm.  If not, see <http://www.gnu.org/licenses/>.

'''
minor network library to return information related to
domain name services
'''

import socket

from pyfarm import datatypes

def ip(hostname=None):
    '''returns the hostname of the current machine'''
    if hostname is None:
        hostname = socket.gethostname()

    try:
        addr = socket.gethostbyname(hostname)
        return addr

    except socket.gaierror, error:
        # in some circumstances os x will not be
        # able to query it's own hostname in dns because
        # the local dns cache is expecting <hostname>.local
        # to be looked up
        if datatypes.OS == datatypes.OperatingSystem.MAC:
            hostname = "%s.local" % hostname
            return socket.gethostbyname(hostname)

        # similar situation for windows when attempting to
        # perform lookups and the local dns cache takes
        # precedence over asking the dns server
        elif datatypes.OS == datatypes.OperatingSystem.WINDOWS:
            hostname = "%s." % hostname
            return socket.gethostbyname(hostname)

        raise
# end ip<commit_msg>added cache to dns lookups<commit_after># No shebang line, this module is meant to be imported
#
# This file is part of PyFarm.
# Copyright (C) 2008-2012 <NAME>
#
# PyFarm is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyFarm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PyFarm.  If not, see <http://www.gnu.org/licenses/>.

'''
minor network library to return information related to
domain name services
'''

import socket

from pyfarm import datatypes

DNS_CACHE = {}

def ip(hostname=None, cache=True):
    '''
    returns the hostname of the current machine

    :param boolean cache:
        if False do not returns results from cache if the
        hostname exists in DNS_CACHE
    '''
    if hostname is None:
        hostname = socket.gethostname()

    if cache and hostname in DNS_CACHE:
        return DNS_CACHE[hostname]

    try:
        address = socket.gethostbyname(hostname)

        if not cache or hostname not in DNS_CACHE:
            DNS_CACHE[hostname] = address

        return address

    except socket.gaierror, error:
        # in some circumstances os x will not be
        # able to query it's own hostname in dns because
        # the local dns cache is expecting <hostname>.local
        # to be looked up
        if datatypes.OS == datatypes.OperatingSystem.MAC:
            hostname = "%s.local" % hostname
            address =  socket.gethostbyname(hostname)

            if not cache or hostname not in DNS_CACHE:
                DNS_CACHE[hostname] = address

            return address
        # similar situation for windows when attempting to
        # perform lookups and the local dns cache takes
        # precedence over asking the dns server
        elif datatypes.OS == datatypes.OperatingSystem.WINDOWS:
            hostname = "%s." % hostname
            address = socket.gethostbyname(hostname)

            if not cache or hostname not in DNS_CACHE:
                DNS_CACHE[hostname] = address

            return address

        raise
# end ip
{'fraction_non_alphanumeric': 0.06140350877192982, 'fraction_numerical': 0.004155124653739612, 'mean_word_length': 2.7417962003454233, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '6577802', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1159, 'n_words': 520}