<commit_before>"""
Simple Python Varnish socket interface.
"""
import socket, re, string

# Hashlib is necessary to generate the sha256 hash for secret key authentication.
# There is an installable hashlib module for python 2.3 and 2.4 (I'm talking to you RHEL5)
try:
    import hashlib
    hashlib_loaded = True
except ImportError:
    hashlib_loaded = False

# varnish-admin-socket version
__version__ = '0.2.1'

## Varnish Admin Socket for executing varnishadm CLI commands
## Tested on varnish 3.0.0
class VarnishAdminSocket(object):
    """Varnish Administration Socket Library"""
    def __init__(self, **kwargs):
        """Initialise the Class, default some variables"""

        # Check kwargs for overrides
        self.host = kwargs.pop('host', '127.0.0.1')
        self.port = kwargs.pop('port', 6082)
        self.secret = kwargs.pop('secret', False)
        self.timeout = kwargs.pop('timeout', 5)
        self.compat = kwargs.pop('compat', False)
        self.secret_file = kwargs.pop('secret_file', False)

        # If auto_connect = True, attempt to connect on instantiation
        self.auto_connect = kwargs.pop('auto_connect', False)
        if self.auto_connect:
            self.connect()
        else:
            self.conn = False

    # Connect to the socket and attempt authentication if necessary
    def connect(self):
        """Make the socket connection"""

        # Determine if we were able to import hashlib. We can't do secret key authentication
        # without it.
        global hashlib_loaded

        # Try to use self.timeout, if we can't make it into an integer
        # default to 5
        try:
            local_timeout = int(self.timeout)
        except ValueError:
            local_timeout = 5


        # Connect to the socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setblocking(1)
        sock.settimeout(local_timeout)

        # Enforce integer for the port
        try:
            self.port = int(self.port)
        except ValueError:
            # Port couldn't be made an integer
            self.close()
            raise Exception('VarnishAdminSocket: Port could not be made an integer')
            return False

        # Make the connection
        sock.connect( (self.host, self.port) )

        # Store the socket makefile
        self.conn = sock.makefile()

        # Close the socket object now that we have makefile
        sock.close()

        # Compat mode is for Varnish 2.0.6
        # There wont be a banner with Varnish 2.0.6
        # just return True once we have the socket connection
        if self.compat:
            return True

        # Read the banner
        (code, response) = self.read()

        # If we get code 107, we need to try to authenticate
        if code == 107:
            # Hashlib needs to be loaded for secret key authentication
            # If it's not loaded, raise an error
            if not hashlib_loaded:
                e = "VarnishAdminSocket: The hashlib module must be available for secret key authentication"
                raise Exception(e)

            # Run get secret to try to load
            # the secret from a file if secret_file is set
            self.__get_secret()

            # Check to make sure we've defined a secret key<commit_msg>Remove compat code, drop support for 2.0<commit_after>"""
Simple Python Varnish socket interface.
"""
import socket, re, string

# Hashlib is necessary to generate the sha256 hash for secret key authentication.
# There is an installable hashlib module for python 2.3 and 2.4 (I'm talking to you RHEL5)
try:
    import hashlib
    hashlib_loaded = True
except ImportError:
    hashlib_loaded = False

# varnish-admin-socket version
__version__ = '0.2.1'

## Varnish Admin Socket for executing varnishadm CLI commands
## Tested on varnish 3.0.0
class VarnishAdminSocket(object):
    """Varnish Administration Socket Library"""
    def __init__(self, **kwargs):
        """Initialise the Class, default some variables"""

        # Check kwargs for overrides
        self.host = kwargs.pop('host', '127.0.0.1')
        self.port = kwargs.pop('port', 6082)
        self.secret = kwargs.pop('secret', False)
        self.timeout = kwargs.pop('timeout', 5)
        self.secret_file = kwargs.pop('secret_file', False)

        # If auto_connect = True, attempt to connect on instantiation
        self.auto_connect = kwargs.pop('auto_connect', False)
        if self.auto_connect:
            self.connect()
        else:
            self.conn = False

    # Connect to the socket and attempt authentication if necessary
    def connect(self):
        """Make the socket connection"""

        # Determine if we were able to import hashlib. We can't do secret key authentication
        # without it.
        global hashlib_loaded

        # Try to use self.timeout, if we can't make it into an integer
        # default to 5
        try:
            local_timeout = int(self.timeout)
        except ValueError:
            local_timeout = 5


        # Connect to the socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setblocking(1)
        sock.settimeout(local_timeout)

        # Enforce integer for the port
        try:
            self.port = int(self.port)
        except ValueError:
            # Port couldn't be made an integer
            self.close()
            raise Exception('VarnishAdminSocket: Port could not be made an integer')
            return False

        # Make the connection
        sock.connect( (self.host, self.port) )

        # Store the socket makefile
        self.conn = sock.makefile()

        # Close the socket object now that we have makefile
        sock.close()

        # Read the banner
        (code, response) = self.read()

        # If we get code 107, we need to try to authenticate
        if code == 107:
            # Hashlib needs to be loaded for secret key authentication
            # If it's not loaded, raise an error
            if not hashlib_loaded:
                e = "VarnishAdminSocket: The hashlib module must be available for secret key authentication"
                raise Exception(e)

            # Run get secret to try to load
            # the secret from a file if secret_file is set
            self.__get_secret()

            # Check to make sure we've defined a secret key
{'fraction_non_alphanumeric': 0.07617706866885657, 'fraction_numerical': 0.011888002502737369, 'mean_word_length': 2.1684836471754214, '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': '2625994', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1678, 'n_words': 649}