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

import StringIO
import logging
import lxml.etree
import zeit.cms.interfaces
import zeit.connector.interfaces
import zeit.connector.resource


log = logging.getLogger(__name__)
# Marker object to indicate no add form.
SKIP_ADD = object()


class TypeDeclaration(object):

    interface = None
    interface_type = zeit.cms.interfaces.ICMSContentType

    # The "type" in DAV. If given the class will also implement IDAVContent
    type = None
    title = None
    addform = None
    register_as_type = True

    def __init__(self):
        if self.addform is None:
            package = '.'.join(self.__module__.split('.')[:-1])
            self.addform = package + '.Add'

    def content(self, resource):
        raise NotImplementedError

    def resource_body(self, content):
        raise NotImplementedError

    def resource_content_type(self, content):
        return None

    def resource_properties(self, content):
        try:
            return zeit.connector.interfaces.IWebDAVReadProperties(content)
        except TypeError:
            return zeit.connector.resource.WebDAVProperties()

    def resource(self, content):
        return zeit.connector.resource.Resource(
            content.uniqueId, content.__name__, self.type,
            data=self.resource_body(content),
            contentType=self.resource_content_type(content),
            properties=self.resource_properties(content))

    @property
    def type_identifier(self):
        if self.type is not None:
            return self.type
        assert self.interface is not None
        return u'%s.%s' % (self.interface.__module__, self.interface.__name__)


class XMLContentTypeDeclaration(TypeDeclaration):

    factory = None

    def content(self, resource):
        try:
            return self.factory(xml_source=resource.data)
        except lxml.etree.XMLSyntaxError, e:
            log.warning("Could not parse XML of %s: %s (%s)" % (
                resource.id, e.__class__.__name__, e))
            return None

    def resource_body(self, content):
        return StringIO.StringIO(
            zeit.cms.content.interfaces.IXMLSource(content))

    def resource_content_type(self, content):
        return 'text/xml'<commit_msg>Move get_type function from zeit.find where it belongs<commit_after># Copyright (c) 2009-2011 gocept gmbh & co. kg
# See also LICENSE.txt

import StringIO
import logging
import lxml.etree
import zeit.cms.interfaces
import zeit.connector.interfaces
import zeit.connector.resource
import zope.interface


log = logging.getLogger(__name__)
# Marker object to indicate no add form.
SKIP_ADD = object()


class TypeDeclaration(object):

    interface = None
    interface_type = zeit.cms.interfaces.ICMSContentType

    # The "type" in DAV. If given the class will also implement IDAVContent
    type = None
    title = None
    addform = None
    register_as_type = True

    def __init__(self):
        if self.addform is None:
            package = '.'.join(self.__module__.split('.')[:-1])
            self.addform = package + '.Add'

    def content(self, resource):
        raise NotImplementedError

    def resource_body(self, content):
        raise NotImplementedError

    def resource_content_type(self, content):
        return None

    def resource_properties(self, content):
        try:
            return zeit.connector.interfaces.IWebDAVReadProperties(content)
        except TypeError:
            return zeit.connector.resource.WebDAVProperties()

    def resource(self, content):
        return zeit.connector.resource.Resource(
            content.uniqueId, content.__name__, self.type,
            data=self.resource_body(content),
            contentType=self.resource_content_type(content),
            properties=self.resource_properties(content))

    @property
    def type_identifier(self):
        if self.type is not None:
            return self.type
        assert self.interface is not None
        return u'%s.%s' % (self.interface.__module__, self.interface.__name__)


class XMLContentTypeDeclaration(TypeDeclaration):

    factory = None

    def content(self, resource):
        try:
            return self.factory(xml_source=resource.data)
        except lxml.etree.XMLSyntaxError, e:
            log.warning("Could not parse XML of %s: %s (%s)" % (
                resource.id, e.__class__.__name__, e))
            return None

    def resource_body(self, content):
        return StringIO.StringIO(
            zeit.cms.content.interfaces.IXMLSource(content))

    def resource_content_type(self, content):
        return 'text/xml'


def get_type(self, content):
    for interface in zope.interface.providedBy(content):
        try:
            return interface.getTaggedValue('zeit.cms.type')
        except KeyError:
            continue
    return None
{'fraction_non_alphanumeric': 0.08848114169215086, 'fraction_numerical': 0.003669724770642202, 'mean_word_length': 2.694277108433735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '4699170', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1339, 'n_words': 349}