<commit_before>from twisted.internet import protocol, defer
from ldaptor.protocols.ldap import distinguishedname

try:
    from twisted.internet.utils import SRVConnector
except ImportError:
    from twisted.names.srvconnect import SRVConnector

class LDAPConnector(SRVConnector):
    def __init__(self, reactor, dn, factory,
		 overrides=None):
        if not isinstance(dn, distinguishedname.DistinguishedName):
            dn = distinguishedname.DistinguishedName(stringValue=dn)
        if overrides is None:
            overrides={}
        self.override = self._findOverRide(dn, overrides)

	domain = dn.getDomainName()
	SRVConnector.__init__(self, reactor,
                              'ldap', domain, factory)

    def __getstate__(self):
        r={}
        r.update(self.__dict__)
        r['connector'] = None
        return r

    def _findOverRide(self, dn, overrides):
        while True:
	    if overrides.has_key(dn):
		return overrides[dn]
            if dn == '':
                break
	    dn = dn.up()
	return None

    def _isQueryNeeded(self):
        """Is there both need and capability to do an SRV query."""
        if self.domain is None:
            # unable to query
            return False

        if self.override is None:
            return True

        assert not callable(self.override)
        overriddenHost, overriddenPort = self.override
        if overriddenHost is None:
            return True
        if overriddenPort is not None:
            return False
        return True

    def connect(self):
        if callable(self.override):
            self.override(self.factory)
        elif not self._isQueryNeeded():
            self.factory.doStart()
            self.factory.startedConnecting(self)
            self._reallyConnect()
	else:
	    SRVConnector.connect(self)

    def pickServer(self):
        if self.override is None:
            overriddenHost, overriddenPort = None, None
        else:
            overriddenHost, overriddenPort = self.override

	if (overriddenHost is not None
	    and (overriddenPort is not None
		 or self.domain is None)):
	    host = overriddenHost
	    port = overriddenPort
	else:
	    host, port = SRVConnector.pickServer(self)
	    if overriddenHost is not None:
		host = overriddenHost
	    if overriddenPort is not None:
		port = overriddenPort

        try:
            port = int(port)
        except ValueError:
            pass

	assert host is not None
	if port is None:
	    port = 389
	return host, port

class LDAPClientCreator(protocol.ClientCreator):
    def connect(self, dn, overrides=None):
        """Connect to remote host, return Deferred of resulting protocol instance."""
        d = defer.Deferred()
        f = protocol._InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
        c = LDAPConnector(self.reactor, dn, f, overrides=overrides)
        c.connect()
        return d

    def connectAnonymously(self, dn, overrides=None):
        """Connect to remote host and bind anonymously, return Deferred of resulting protocol instance."""
        d = self.connect(dn, overrides=overrides)

        def _bind(proto):
            d=proto.bind()
            d.addCallback(lambda _: proto)
            return d
        d.addCallback(_bind)
        return d
<commit_msg>Make LDAPConnector fail nicely on DNs with no equivalent DNS domain, and no override.<commit_after>from twisted.internet import protocol, defer
from ldaptor.protocols.ldap import distinguishedname

try:
    from twisted.internet.utils import SRVConnector
except ImportError:
    from twisted.names.srvconnect import SRVConnector

class LDAPConnector(SRVConnector):
    def __init__(self, reactor, dn, factory,
		 overrides=None):
        if not isinstance(dn, distinguishedname.DistinguishedName):
            dn = distinguishedname.DistinguishedName(stringValue=dn)
        if overrides is None:
            overrides={}
        self.override = self._findOverRide(dn, overrides)

	domain = dn.getDomainName()
	SRVConnector.__init__(self, reactor,
                              'ldap', domain, factory)

    def __getstate__(self):
        r={}
        r.update(self.__dict__)
        r['connector'] = None
        return r

    def _findOverRide(self, dn, overrides):
        while True:
	    if overrides.has_key(dn):
		return overrides[dn]
            if dn == '':
                break
	    dn = dn.up()
	return None

    def _isQueryNeeded(self):
        """Is there both need to do an SRV query."""
        if self.override is None:
            return True

        assert not callable(self.override)
        overriddenHost, overriddenPort = self.override
        if overriddenHost is None:
            return True
        if overriddenPort is not None:
            return False
        return True

    def connect(self):
        if callable(self.override):
            self.override(self.factory)
        elif not self._isQueryNeeded():
            self.factory.doStart()
            self.factory.startedConnecting(self)
            self._reallyConnect()
	else:
	    SRVConnector.connect(self)

    def pickServer(self):
        if self.override is None:
            overriddenHost, overriddenPort = None, None
        else:
            overriddenHost, overriddenPort = self.override

	if (overriddenHost is not None
	    and (overriddenPort is not None
		 or self.domain is None)):
	    host = overriddenHost
	    port = overriddenPort
	else:
	    host, port = SRVConnector.pickServer(self)
	    if overriddenHost is not None:
		host = overriddenHost
	    if overriddenPort is not None:
		port = overriddenPort

        try:
            port = int(port)
        except ValueError:
            pass

	assert host is not None
	if port is None:
	    port = 389
	return host, port

class LDAPClientCreator(protocol.ClientCreator):
    def connect(self, dn, overrides=None):
        """Connect to remote host, return Deferred of resulting protocol instance."""
        d = defer.Deferred()
        f = protocol._InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
        c = LDAPConnector(self.reactor, dn, f, overrides=overrides)
        c.connect()
        return d

    def connectAnonymously(self, dn, overrides=None):
        """Connect to remote host and bind anonymously, return Deferred of resulting protocol instance."""
        d = self.connect(dn, overrides=overrides)

        def _bind(proto):
            d=proto.bind()
            d.addCallback(lambda _: proto)
            return d
        d.addCallback(_bind)
        return d

{'fraction_non_alphanumeric': 0.0809415337889142, 'fraction_numerical': 0.0009111617312072893, 'mean_word_length': 2.324583543664816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4138486', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1829, 'n_words': 536}