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

from zope.cachedescriptors.property import Lazy as cachedproperty
import calendar
import datetime
import grokcore.component as grok
import pytz
import transaction
import zeit.addcentral.add
import zeit.addcentral.interfaces
import zeit.brightcove.interfaces
import zeit.cms.checkout.interfaces
import zeit.cms.content.interfaces
import zeit.cms.interfaces
import zeit.cms.tagging.interfaces
import zeit.cms.workflow.interfaces
import zeit.connector.interfaces
import zeit.connector.search
import zeit.content.video.interfaces
import zeit.content.video.playlist
import zeit.content.video.video
import zope.component
import zope.interface


def to_epoch(value):
    return calendar.timegm(value.utctimetuple())


class mapped(object):

    field = None

    def __init__(self, *path):
        assert path
        self.path = path

    def __get__(self, instance, class_):
        if instance is None:
            return self
        try:
            return self._get_from_dict(instance.data)
        except KeyError:
            if self.field is None:
                self._guess_field(class_)
            if self.field is not None:
                return self.field.default

    def _guess_field(self, class_):
        for key, value in class_.__dict__.items():
            if value is self:
                break
        else:
            return
        for interface in zope.interface.implementedBy(class_):
            try:
                field = interface[key]
            except KeyError:
                pass
            else:
                self.field = field
                break

    def _get_from_dict(self, value):
        for key in self.path:
            value = value[key]
        return value

    def __set__(self, instance, value):
        data = instance.data
        for key in self.path[:-1]:
            data = data.setdefault(key, {})
        data[self.path[-1]] = value


class mapped_bool(mapped):

    def _get_from_dict(self, value):
        value = super(mapped_bool, self)._get_from_dict(value)
        return value == '1'

    def __set__(self, instance, value):
        value = '1' if value else '0'
        super(mapped_bool, self).__set__(instance, value)


class mapped_keywords(mapped):
    """Maps the labels stored in brightcove to a list of tags.<commit_msg>worked around inconsistent data types sent by brightcove<commit_after># Copyright (c) 2010-2011 gocept gmbh & co. kg
# See also LICENSE.txt

from zope.cachedescriptors.property import Lazy as cachedproperty
import calendar
import datetime
import grokcore.component as grok
import pytz
import transaction
import zeit.addcentral.add
import zeit.addcentral.interfaces
import zeit.brightcove.interfaces
import zeit.cms.checkout.interfaces
import zeit.cms.content.interfaces
import zeit.cms.interfaces
import zeit.cms.tagging.interfaces
import zeit.cms.workflow.interfaces
import zeit.connector.interfaces
import zeit.connector.search
import zeit.content.video.interfaces
import zeit.content.video.playlist
import zeit.content.video.video
import zope.component
import zope.interface


def to_epoch(value):
    return calendar.timegm(value.utctimetuple())


class mapped(object):

    field = None

    def __init__(self, *path):
        assert path
        self.path = path

    def __get__(self, instance, class_):
        if instance is None:
            return self
        try:
            return self._get_from_dict(instance.data)
        except KeyError:
            if self.field is None:
                self._guess_field(class_)
            if self.field is not None:
                return self.field.default

    def _guess_field(self, class_):
        for key, value in class_.__dict__.items():
            if value is self:
                break
        else:
            return
        for interface in zope.interface.implementedBy(class_):
            try:
                field = interface[key]
            except KeyError:
                pass
            else:
                self.field = field
                break

    def _get_from_dict(self, value):
        for key in self.path:
            # XXX We've seen a value of None for the customFields dict sent by
            # Brightcove.
            if value is None:
                return
            value = value[key]
        return value

    def __set__(self, instance, value):
        data = instance.data
        for key in self.path[:-1]:
            data = data.setdefault(key, {})
        data[self.path[-1]] = value


class mapped_bool(mapped):

    def _get_from_dict(self, value):
        value = super(mapped_bool, self)._get_from_dict(value)
        return value == '1'

    def __set__(self, instance, value):
        value = '1' if value else '0'
        super(mapped_bool, self).__set__(instance, value)


class mapped_keywords(mapped):
    """Maps the labels stored in brightcove to a list of tags.
{'fraction_non_alphanumeric': 0.07871954902355546, 'fraction_numerical': 0.0052345480169116165, 'mean_word_length': 2.316421895861148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4989918', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1403, 'n_words': 409}