<commit_before>from nevow import tags, compy, inevow, flat
from ldaptor.protocols.ldap import ldapsyntax, distinguishedname
from ldaptor import interfaces

def keyvalue(context, data):
    """
    Render items in a mapping using patterns found in the children
    of the element.
    
    Keyvalue recognizes the following patterns:

    header: Rendered at the start, before the first item. If multiple
        header patterns are provided they are rendered together in the
        order they were defined.
            
    footer: Just like the header only renderer at the end, after the
        last item.
    
    item: Rendered once for each item in the sequence. Can contain
        subpatterns key and value.

        If multiple item patterns are provided then the pattern is
        cycled in the order defined.
        
    divider: Rendered once between each item in the sequence. Multiple
        divider patterns are cycled.

    empty: Rendered instead of item and divider patterns when the
        sequence contains no items.

    Example:
    
    <table nevow:render="sequence" nevow:data="peopleSeq">
      <tr nevow:pattern="header">
        <th>name</th>
        <th>email</th>
      </tr>
      <tr nevow:pattern="item" class="odd">
        <td>name goes here</td>
        <td>email goes here</td>
      </tr>
      <tr nevow:pattern="item" class="even">
        <td>name goes here</td>
        <td>email goes here</td>
      </tr>
      <tr nevow:pattern="empty">
        <td colspan="2"><em>they've all gone!</em></td>
      </tr>
    </table>

    """
    headers = context.allPatterns('header')
    item = context.patternGenerator('item')
    divider = context.patternGenerator('divider', default=tags.invisible)
    content = []
    for key, value in data.items():
        content.append(item(data=(key, value)))
        content.append(divider(data=(key, value)))
    if not content:
        content = context.allPatterns('empty')
    else:
        ## No divider after the last thing.
        del content[-1]
    footers = context.allPatterns('footer')
    return context.tag.clear()[ headers, content, footers ]

def keyvalue_item(context, data):
    key, value = data

    k = context.patternGenerator('key')
    v = context.patternGenerator('value')

    return context.tag.clear()[ k(data=key), v(data=value) ]

class LDAPEntryContainer(object):
    __implements__ = inevow.IContainer

    def __init__(self, original):
        self.original = original

    def child(self, context, name):
        if name == 'dn':
            return self.original.dn
        elif name == 'attributes':
            return self.original
        else:
            return None

compy.registerAdapter(LDAPEntryContainer, ldapsyntax.LDAPEntryWithClient, inevow.IContainer)

def dnSerializer(original, context):
    return flat.serialize(str(original), context)

compy.registerAdapter(dnSerializer,
                      distinguishedname.DistinguishedName,
                      inevow.ISerializable)

def entrySerializer(original, context):
    ul = tags.ul()
    for a,l in original.items():
	if len(l)==0:
	    ul[tags.li[a, ': none']]
	elif len(l)==1:
            for attr in l:
                first = attr
                break
	    ul[tags.li[a, ': ', first]]
	else:
            li=tags.li[a, ':']
            ul[li]
            liul=tags.ul()
            li[liul]
	    for i in l:
		liul[tags.li[i]]
    return flat.serialize(ul, context)

compy.registerAdapter(entrySerializer,
                      interfaces.ILDAPEntry,
                      inevow.ISerializable)<commit_msg>Refactor to follow new nevow APIs: ISerializable is deprecated.<commit_after>from nevow import tags, compy, inevow, flat
from ldaptor.protocols.ldap import ldapsyntax, distinguishedname
from ldaptor import interfaces

def keyvalue(context, data):
    """
    Render items in a mapping using patterns found in the children
    of the element.
    
    Keyvalue recognizes the following patterns:

    header: Rendered at the start, before the first item. If multiple
        header patterns are provided they are rendered together in the
        order they were defined.
            
    footer: Just like the header only renderer at the end, after the
        last item.
    
    item: Rendered once for each item in the sequence. Can contain
        subpatterns key and value.

        If multiple item patterns are provided then the pattern is
        cycled in the order defined.
        
    divider: Rendered once between each item in the sequence. Multiple
        divider patterns are cycled.

    empty: Rendered instead of item and divider patterns when the
        sequence contains no items.

    Example:
    
    <table nevow:render="sequence" nevow:data="peopleSeq">
      <tr nevow:pattern="header">
        <th>name</th>
        <th>email</th>
      </tr>
      <tr nevow:pattern="item" class="odd">
        <td>name goes here</td>
        <td>email goes here</td>
      </tr>
      <tr nevow:pattern="item" class="even">
        <td>name goes here</td>
        <td>email goes here</td>
      </tr>
      <tr nevow:pattern="empty">
        <td colspan="2"><em>they've all gone!</em></td>
      </tr>
    </table>

    """
    headers = context.allPatterns('header')
    item = context.patternGenerator('item')
    divider = context.patternGenerator('divider', default=tags.invisible)
    content = []
    for key, value in data.items():
        content.append(item(data=(key, value)))
        content.append(divider(data=(key, value)))
    if not content:
        content = context.allPatterns('empty')
    else:
        ## No divider after the last thing.
        del content[-1]
    footers = context.allPatterns('footer')
    return context.tag.clear()[ headers, content, footers ]

def keyvalue_item(context, data):
    key, value = data

    k = context.patternGenerator('key')
    v = context.patternGenerator('value')

    return context.tag.clear()[ k(data=key), v(data=value) ]

class LDAPEntryContainer(object):
    __implements__ = inevow.IContainer

    def __init__(self, original):
        self.original = original

    def child(self, context, name):
        if name == 'dn':
            return self.original.dn
        elif name == 'attributes':
            return self.original
        else:
            return None

compy.registerAdapter(LDAPEntryContainer, ldapsyntax.LDAPEntryWithClient, inevow.IContainer)

def dnSerializer(original, context):
    return flat.serialize(str(original), context)

flat.registerFlattener(dnSerializer,
                       distinguishedname.DistinguishedName)

def entrySerializer(original, context):
    ul = tags.ul()
    for a,l in original.items():
	if len(l)==0:
	    ul[tags.li[a, ': none']]
	elif len(l)==1:
            for attr in l:
                first = attr
                break
	    ul[tags.li[a, ': ', first]]
	else:
            li=tags.li[a, ':']
            ul[li]
            liul=tags.ul()
            li[liul]
	    for i in l:
		liul[tags.li[i]]
    return flat.serialize(ul, context)

flat.registerFlattener(entrySerializer,
                       interfaces.ILDAPEntry)
{'fraction_non_alphanumeric': 0.10480715483510342, 'fraction_numerical': 0.0011179429849077697, 'mean_word_length': 2.457487922705314, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, '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': '3464259', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2049, 'n_words': 609}