<commit_before>from five import grok
from Products.CMFCore.utils import getToolByName
from Products.Five import browser
from persistent.dict import PersistentDict
from plone.app.dexterity.behaviors.metadata import ICategorization
from plone.directives import form
from plone.formwidget.contenttree.source import ObjPathSourceBinder
from plone.supermodel import model
from tn.plonebehavior.template import interfaces
from zope.annotation.interfaces import IAnnotations
from zope.annotation.interfaces import IAttributeAnnotatable
from zope.i18nmessageid import MessageFactory

import lxml.cssselect
import z3c.relationfield
import zope.component
import zope.globalrequest
import zope.interface
import zope.schema


_ = MessageFactory('tn.plonebehavior.template')


default_css_selector = u"#template-content"
default_xpath_selector = lxml.cssselect.CSSSelector(default_css_selector).path
template_configuration_key = 'tn.plonebehavior.template.TemplateConfiguration'
templating_key = 'tn.plonebehavior.template.Templating'
apply = lambda f: f()


class ITemplateConfiguration(model.Schema):

    form.omitted('html')
    html = zope.schema.SourceText(title=_(u'HTML'), readonly=True)

    form.fieldset(
        'template-configuration',
        label=_(u'Template configuration'),
        fields=['css'],
    )
    css = zope.schema.TextLine(
        title=_(u'CSS selector'),
        description=_(u'The CSS expression which selects the element where '
                      u'the content will go to.'),
        default=default_css_selector,
        required=False
    )

    form.omitted('xpath')
    xpath = zope.schema.TextLine(
        title=_(u'XPath selector'),
        description=_(u'The XPath expression which selects the element where '
                      u'the content will go to.  A CSS expression will '
                      u'override this.'),
        default=default_xpath_selector,
        required=False
    )


zope.interface.alsoProvides(ITemplateConfiguration, form.IFormFieldProvider)


class INullTemplateConfiguration(ITemplateConfiguration):
    """A template configuration for null templates.
    """


class TemplateConfiguration(object):
    """Store template configuration attributes.
    """
    grok.implements(ITemplateConfiguration)
    grok.adapts(IAttributeAnnotatable)

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

    @property
    def html(self):
        return unicode(interfaces.IHTML(self.context))

    @apply
    def xpath():
        def get(self):
            return self.annotations().get('xpath', default_xpath_selector)

        def set(self, value):
            if value:
                zope.interface.alsoProvides(self.context,
                                            interfaces.IPossibleTemplate)
            else:
                zope.interface.noLongerProvides(self.context,
                                                interfaces.IPossibleTemplate)
            self.annotations()['xpath'] = value
        return property(get, set)

    @apply
    def css():
        def get(self):
            return self.annotations().get('css', default_css_selector)

        def set(self, value):
            if value:
                xpath = lxml.cssselect.CSSSelector(value).path
                self.xpath = xpath
            else:
                self.xpath = None
            self.annotations()['css'] = value
        return property(get, set)

    def annotations(self):
        annotations = IAnnotations(self.context)
        if template_configuration_key not in annotations:
            annotations[template_configuration_key] = PersistentDict()
        return annotations[template_configuration_key]


possibleTemplates = ObjPathSourceBinder(dict(
    object_provides=interfaces.IPossibleTemplate.__identifier__
))


class NullTemplateConfiguration(object):
    """A null template configuration.

    This addresses the case of the content not having an associated
    template.  A NullTemplate object is used then, and this is the<commit_msg>Use selectable filter attributes instead of navtree query<commit_after>from five import grok
from Products.CMFCore.utils import getToolByName
from Products.Five import browser
from persistent.dict import PersistentDict
from plone.app.dexterity.behaviors.metadata import ICategorization
from plone.directives import form
from plone.formwidget.contenttree.source import ObjPathSourceBinder
from plone.supermodel import model
from tn.plonebehavior.template import interfaces
from zope.annotation.interfaces import IAnnotations
from zope.annotation.interfaces import IAttributeAnnotatable
from zope.i18nmessageid import MessageFactory

import lxml.cssselect
import z3c.relationfield
import zope.component
import zope.globalrequest
import zope.interface
import zope.schema


_ = MessageFactory('tn.plonebehavior.template')


default_css_selector = u"#template-content"
default_xpath_selector = lxml.cssselect.CSSSelector(default_css_selector).path
template_configuration_key = 'tn.plonebehavior.template.TemplateConfiguration'
templating_key = 'tn.plonebehavior.template.Templating'
apply = lambda f: f()


class ITemplateConfiguration(model.Schema):

    form.omitted('html')
    html = zope.schema.SourceText(title=_(u'HTML'), readonly=True)

    form.fieldset(
        'template-configuration',
        label=_(u'Template configuration'),
        fields=['css'],
    )
    css = zope.schema.TextLine(
        title=_(u'CSS selector'),
        description=_(u'The CSS expression which selects the element where '
                      u'the content will go to.'),
        default=default_css_selector,
        required=False
    )

    form.omitted('xpath')
    xpath = zope.schema.TextLine(
        title=_(u'XPath selector'),
        description=_(u'The XPath expression which selects the element where '
                      u'the content will go to.  A CSS expression will '
                      u'override this.'),
        default=default_xpath_selector,
        required=False
    )


zope.interface.alsoProvides(ITemplateConfiguration, form.IFormFieldProvider)


class INullTemplateConfiguration(ITemplateConfiguration):
    """A template configuration for null templates.
    """


class TemplateConfiguration(object):
    """Store template configuration attributes.
    """
    grok.implements(ITemplateConfiguration)
    grok.adapts(IAttributeAnnotatable)

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

    @property
    def html(self):
        return unicode(interfaces.IHTML(self.context))

    @apply
    def xpath():
        def get(self):
            return self.annotations().get('xpath', default_xpath_selector)

        def set(self, value):
            if value:
                zope.interface.alsoProvides(self.context,
                                            interfaces.IPossibleTemplate)
            else:
                zope.interface.noLongerProvides(self.context,
                                                interfaces.IPossibleTemplate)
            self.annotations()['xpath'] = value
        return property(get, set)

    @apply
    def css():
        def get(self):
            return self.annotations().get('css', default_css_selector)

        def set(self, value):
            if value:
                xpath = lxml.cssselect.CSSSelector(value).path
                self.xpath = xpath
            else:
                self.xpath = None
            self.annotations()['css'] = value
        return property(get, set)

    def annotations(self):
        annotations = IAnnotations(self.context)
        if template_configuration_key not in annotations:
            annotations[template_configuration_key] = PersistentDict()
        return annotations[template_configuration_key]


possibleTemplates = ObjPathSourceBinder(**dict(
    object_provides=interfaces.IPossibleTemplate.__identifier__
))


class NullTemplateConfiguration(object):
    """A null template configuration.

    This addresses the case of the content not having an associated
    template.  A NullTemplate object is used then, and this is the
{'fraction_non_alphanumeric': 0.08104768964665184, 'fraction_numerical': 0.0007412898443291327, 'mean_word_length': 2.9449317738791425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4030324', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2091, 'n_words': 504}