<commit_before># Copyright (c) 2007-2009 gocept gmbh & co. kg
# See also LICENSE.txt

import logging
import lxml.etree
import rwproperty
import zope.component
import zope.interface
import zope.location.location
import zope.proxy
import zope.security.proxy

import zeit.cms.connector
import zeit.cms.content.adapter
import zeit.cms.content.interfaces
import zeit.cms.content.property
import zeit.cms.content.xmlsupport
import zeit.cms.interfaces
import zeit.cms.repository.interfaces
import zeit.cms.syndication.interfaces


log = logging.getLogger(__name__)


class Feed(zeit.cms.content.xmlsupport.XMLContentBase):

    zope.interface.implements(
        zeit.cms.syndication.interfaces.IFeed,
        zeit.cms.interfaces.IAsset)

    title = zeit.cms.content.property.ObjectPathProperty('.title')
    object_limit = zeit.cms.content.property.ObjectPathProperty(
        '.object_limit')

    default_template = u"""\
        <channel
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:py="http://codespeak.net/lxml/objectify/pytype">
          <title/>
          <container/>
        </channel>
    """

    def __init__(self, *args, **kwargs):
        super(Feed, self).__init__(*args, **kwargs)
        if not self.object_limit:
            self.object_limit = 50

    @property
    def xml_source(self):
        """return source of feed."""
        # BBB deprecated
        return lxml.etree.tostring(self.xml, pretty_print=True)

    def keys(self):
        for entry in self.iterentries():
            yield entry.get('href')

    def insert(self, position, content):
        content = zeit.cms.interfaces.ICMSContent(content)
        unique_id = content.uniqueId
        if unique_id is None:
            raise ValueError('Cannot add objects without uniqueId.')
        pin_map = self.pin_map()
        entry = lxml.objectify.E.block(href=unique_id)
        self.entries.insert(position, entry)
        while self.object_limit and len(self) > self.object_limit:
            last = list(self.keys())[-1]
            self._remove_by_id(last)
        self.updateMetadata(content)
        self.restorePinning(pin_map)
        self._p_changed = True

    def remove(self, content):
        if not isinstance(zope.proxy.removeAllProxies(content), FakeEntry):
            content = zeit.cms.interfaces.ICMSContent(content)
        unique_id = content.uniqueId
        self._remove_by_id(unique_id)

    def updateOrder(self, order):
        entries = self.entry_map
        if set(order) != set(entries.keys()):
            raise ValueError("The order argument must contain the same "
                             "keys as the feed.")
        ordered = []
        for id in order:
            ordered.append(entries[id])
        self.entries.block = ordered
        self._p_changed = True

    def __len__(self):
        return len(list(self.keys()))

    def __iter__(self):
        repository = zope.component.getUtility(
            zeit.cms.repository.interfaces.IRepository)
        for unique_id in self.keys():
            try:
                yield repository.getContent(unique_id)
            except (KeyError, ValueError), e:
                entry = self.entry_map[unique_id]
                yield FakeEntry(unique_id, entry)

    def __contains__(self, obj):
        if not zeit.cms.interfaces.ICMSContent.providedBy(obj):
            return False
        return obj.uniqueId in self.entry_map

    def getPosition(self, content):
        content_id = content.uniqueId
        for id, key in enumerate(self.keys()):
            if key == content_id:
                return id + 1

    def updateMetadata(self, content):
        entry = self.entry_map[content.uniqueId]
        updater = zeit.cms.content.interfaces.IXMLReferenceUpdater(content)
        updater.update(entry)
<commit_msg>use adapter to get from unique_id to the content object instead of going through the repository<commit_after># Copyright (c) 2007-2009 gocept gmbh & co. kg
# See also LICENSE.txt

import logging
import lxml.etree
import rwproperty
import zope.component
import zope.interface
import zope.location.location
import zope.proxy
import zope.security.proxy

import zeit.cms.connector
import zeit.cms.content.adapter
import zeit.cms.content.interfaces
import zeit.cms.content.property
import zeit.cms.content.xmlsupport
import zeit.cms.interfaces
import zeit.cms.syndication.interfaces


log = logging.getLogger(__name__)


class Feed(zeit.cms.content.xmlsupport.XMLContentBase):

    zope.interface.implements(
        zeit.cms.syndication.interfaces.IFeed,
        zeit.cms.interfaces.IAsset)

    title = zeit.cms.content.property.ObjectPathProperty('.title')
    object_limit = zeit.cms.content.property.ObjectPathProperty(
        '.object_limit')

    default_template = u"""\
        <channel
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:py="http://codespeak.net/lxml/objectify/pytype">
          <title/>
          <container/>
        </channel>
    """

    def __init__(self, *args, **kwargs):
        super(Feed, self).__init__(*args, **kwargs)
        if not self.object_limit:
            self.object_limit = 50

    @property
    def xml_source(self):
        """return source of feed."""
        # BBB deprecated
        return lxml.etree.tostring(self.xml, pretty_print=True)

    def keys(self):
        for entry in self.iterentries():
            yield entry.get('href')

    def insert(self, position, content):
        content = zeit.cms.interfaces.ICMSContent(content)
        unique_id = content.uniqueId
        if unique_id is None:
            raise ValueError('Cannot add objects without uniqueId.')
        pin_map = self.pin_map()
        entry = lxml.objectify.E.block(href=unique_id)
        self.entries.insert(position, entry)
        while self.object_limit and len(self) > self.object_limit:
            last = list(self.keys())[-1]
            self._remove_by_id(last)
        self.updateMetadata(content)
        self.restorePinning(pin_map)
        self._p_changed = True

    def remove(self, content):
        if not isinstance(zope.proxy.removeAllProxies(content), FakeEntry):
            content = zeit.cms.interfaces.ICMSContent(content)
        unique_id = content.uniqueId
        self._remove_by_id(unique_id)

    def updateOrder(self, order):
        entries = self.entry_map
        if set(order) != set(entries.keys()):
            raise ValueError("The order argument must contain the same "
                             "keys as the feed.")
        ordered = []
        for id in order:
            ordered.append(entries[id])
        self.entries.block = ordered
        self._p_changed = True

    def __len__(self):
        return len(list(self.keys()))

    def __iter__(self):
        for unique_id in self.keys():
            try:
                yield zeit.cms.interfaces.ICMSContent(unique_id)
            except (KeyError, ValueError), e:
                entry = self.entry_map[unique_id]
                yield FakeEntry(unique_id, entry)

    def __contains__(self, obj):
        if not zeit.cms.interfaces.ICMSContent.providedBy(obj):
            return False
        return obj.uniqueId in self.entry_map

    def getPosition(self, content):
        content_id = content.uniqueId
        for id, key in enumerate(self.keys()):
            if key == content_id:
                return id + 1

    def updateMetadata(self, content):
        entry = self.entry_map[content.uniqueId]
        updater = zeit.cms.content.interfaces.IXMLReferenceUpdater(content)
        updater.update(entry)

{'fraction_non_alphanumeric': 0.10290305857957491, 'fraction_numerical': 0.005702436495593572, 'mean_word_length': 2.468314606741573, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 22}, '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': '6793645', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2255, 'n_words': 480}