<commit_before>from twisted.internet import reactor
from twisted.internet import defer
from ldaptor.protocols import pureldap
from ldaptor.protocols.ldap import ldapsyntax, distinguishedname
from ldaptor import generate_password, entry, interfaces
from ldaptor.apps.webui.uriquote import uriUnquote
from ldaptor import weave

import os, sets
from nevow import rend, inevow, loaders, url, tags
from formless import annotate, webform, iformless, configurable

def getEntry(ctx, dn):
    user = ctx.locate(inevow.ISession).getLoggedInRoot().loggedIn
    e=ldapsyntax.LDAPEntry(client=user.client, dn=dn)
    return e

def getEntryWithAttributes(ctx, dn, *attributes):
    e = getEntry(ctx, dn)
    d = e.fetch(*attributes)
    return d

class IPasswordChange(annotate.TypedInterface):
    def setPassword(self,
                    ctx=annotate.Context(),
                    newPassword=annotate.Password(required=True)):
        pass
    setPassword = annotate.autocallable(setPassword)

    def generateRandom(self,
                       ctx=annotate.Context()):
        pass
    generateRandom = annotate.autocallable(generateRandom)

class IServicePasswordChange(annotate.TypedInterface):
    def remove(self,
                      ctx=annotate.Context()):
        pass
    remove = annotate.autocallable(remove)

    def setServicePassword(self,
                           ctx=annotate.Context(),
                           newPassword=annotate.Password(required=True)):
        pass
    setServicePassword = annotate.autocallable(setServicePassword)

    def generateRandom(self,
                       ctx=annotate.Context()):
        pass
    generateRandom = annotate.autocallable(generateRandom)

class ServicePasswordChange(object):
    __implements__ = IServicePasswordChange

    def __init__(self, dn):
        super(ServicePasswordChange, self).__init__()
        self.dn = dn

    def _getServiceName(self, ctx):
        d = getEntryWithAttributes(ctx, self.dn, 'cn')
        def _cb(e):
            for cn in e.get('cn', []):
                return cn
            raise RuntimeError, \
                  "Service password entry has no attribute cn: %r" % e
        d.addCallback(_cb)
        return d

    def setServicePassword(self, ctx, newPassword):
        e = getEntry(ctx, self.dn)
        d = e.setPassword(newPassword)

        def _getName(_, ctx):
            d = self._getServiceName(ctx)
            return d
        d.addCallback(_getName, ctx)

        def _report(name):
            return 'Set password for service %r' % name
        d.addCallback(_report)

        return d

    def generateRandom(self, ctx):
        d=generate_password.generate(reactor)
        def _first(passwords):
            assert len(passwords)==1
            return passwords[0]
        d.addCallback(_first)

        def _setPass(newPassword, ctx):
            e = getEntry(ctx, self.dn)
            d = e.setPassword(newPassword)

            def _getName(_, ctx):
                d = self._getServiceName(ctx)
                return d
            d.addCallback(_getName, ctx)

            def _report(name, newPassword):
                return 'Service %r password set to %s' % (name, newPassword)
            d.addCallback(_report, newPassword)

            return d

        d.addCallback(_setPass, ctx)
        return d

    def remove(self, ctx):
        e = getEntry(ctx, self.dn)
        d = self._getServiceName(ctx)

        def _delete(name, e):
            d = e.delete()
            d.addCallback(lambda _: name)
            return d
        d.addCallback(_delete, e)

        def _report(name):
            return 'Removed service %r' % name
        d.addCallback(_report)

        return d

class IAddService(annotate.TypedInterface):
    def add(self,
            ctx=annotate.Context(),
            serviceName=annotate.String(required=True),
            newPassword=annotate.Password(required=False,
                                          description="Leave empty to generate random password."),
            ):
        pass
    add = annotate.autocallable(add)

class AddService(object):
    __implements__ = IAddService

    def __init__(self, dn):
        super(AddService, self).__init__()
        self.dn = dn<commit_msg>Refactor getServiceName out of ServicePasswordChange.<commit_after>from twisted.internet import reactor
from twisted.internet import defer
from ldaptor.protocols import pureldap
from ldaptor.protocols.ldap import ldapsyntax, distinguishedname
from ldaptor import generate_password, entry, interfaces
from ldaptor.apps.webui.uriquote import uriUnquote
from ldaptor import weave

import os, sets
from nevow import rend, inevow, loaders, url, tags
from formless import annotate, webform, iformless, configurable

def getEntry(ctx, dn):
    user = ctx.locate(inevow.ISession).getLoggedInRoot().loggedIn
    e=ldapsyntax.LDAPEntry(client=user.client, dn=dn)
    return e

def getEntryWithAttributes(ctx, dn, *attributes):
    e = getEntry(ctx, dn)
    d = e.fetch(*attributes)
    return d

def getServiceName(ctx, dn):
    d = getEntryWithAttributes(ctx, dn, 'cn')
    def _cb(e):
        for cn in e.get('cn', []):
            return cn
        raise RuntimeError, \
              "Service password entry has no attribute cn: %r" % e
    d.addCallback(_cb)
    return d

class IPasswordChange(annotate.TypedInterface):
    def setPassword(self,
                    ctx=annotate.Context(),
                    newPassword=annotate.Password(required=True)):
        pass
    setPassword = annotate.autocallable(setPassword)

    def generateRandom(self,
                       ctx=annotate.Context()):
        pass
    generateRandom = annotate.autocallable(generateRandom)

class IServicePasswordChange(annotate.TypedInterface):
    def remove(self,
                      ctx=annotate.Context()):
        pass
    remove = annotate.autocallable(remove)

    def setServicePassword(self,
                           ctx=annotate.Context(),
                           newPassword=annotate.Password(required=True)):
        pass
    setServicePassword = annotate.autocallable(setServicePassword)

    def generateRandom(self,
                       ctx=annotate.Context()):
        pass
    generateRandom = annotate.autocallable(generateRandom)

class ServicePasswordChange(object):
    __implements__ = IServicePasswordChange

    def __init__(self, dn):
        super(ServicePasswordChange, self).__init__()
        self.dn = dn

    def setServicePassword(self, ctx, newPassword):
        e = getEntry(ctx, self.dn)
        d = e.setPassword(<PASSWORD>)

        def _getName(_, ctx):
            d = getServiceName(ctx, self.dn)
            return d
        d.addCallback(_getName, ctx)

        def _report(name):
            return 'Set password for service %r' % name
        d.addCallback(_report)

        return d

    def generateRandom(self, ctx):
        d=generate_password.generate(reactor)
        def _first(passwords):
            assert len(passwords)==1
            return passwords[0]
        d.addCallback(_first)

        def _setPass(newPassword, ctx):
            e = getEntry(ctx, self.dn)
            d = e.setPassword(<PASSWORD>)

            def _getName(_, ctx):
                d = getServiceName(ctx, self.dn)
                return d
            d.addCallback(_getName, ctx)

            def _report(name, newPassword):
                return 'Service %r password set to %s' % (name, newPassword)
            d.addCallback(_report, newPassword)

            return d

        d.addCallback(_setPass, ctx)
        return d

    def remove(self, ctx):
        e = getEntry(ctx, self.dn)
        d = getServiceName(ctx, self.dn)

        def _delete(name, e):
            d = e.delete()
            d.addCallback(lambda _: name)
            return d
        d.addCallback(_delete, e)

        def _report(name):
            return 'Removed service %r' % name
        d.addCallback(_report)

        return d

class IAddService(annotate.TypedInterface):
    def add(self,
            ctx=annotate.Context(),
            serviceName=annotate.String(required=True),
            newPassword=annotate.Password(required=False,
                                          description="Leave empty to generate random password."),
            ):
        pass
    add = annotate.autocallable(add)

class AddService(object):
    __implements__ = IAddService

    def __init__(self, dn):
        super(AddService, self).__init__()
        self.dn = dn
{'fraction_non_alphanumeric': 0.08958553481272749, 'fraction_numerical': 0.0004696489374192791, 'mean_word_length': 2.2486651411136536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5509801', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2390, 'n_words': 564}