Пример #1
0
def ErrorViewDiscriminators(error_view, error=None, request=None, widget=None,
                            field=None, form=None, content=None):
    # pylint: disable=invalid-name,too-many-arguments
    """Error view discriminators"""
    adapter(get_specification(error),
            get_specification(request),
            get_specification(widget),
            get_specification(field),
            get_specification(form),
            get_specification(content))(error_view)
Пример #2
0
def WidgetValidatorDiscriminators(validator, context=None, request=None, view=None,
                                  field=None, widget=None):
    # pylint: disable=invalid-name,too-many-arguments
    """Widget validator discriminators"""
    adapter(
        get_specification(context),
        get_specification(request),
        get_specification(view),
        get_specification(field),
        get_specification(widget)
    )(validator)
Пример #3
0
    def __init__(self, descriptor, view_type=None):
        try:
            descriptor.__get__
        except AttributeError:
            raise TypeError(
                "NamedTemplateImplementation must be passed a descriptor.")
        self.descriptor = descriptor
        interface.implementer(INamedTemplate)(self)

        if view_type is not None:
            component.adapter(view_type)(self)
Пример #4
0
def WidgetsValidatorDiscriminators(validator, context=None, request=None, view=None,
                                   schema=None, manager=None):
    # pylint: disable=invalid-name,too-many-arguments
    """Widgets validator discriminators"""
    adapter(
        get_specification(context),
        get_specification(request),
        get_specification(view),
        get_specification(schema),
        get_specification(manager)
    )(validator)
Пример #5
0
    def __init__(self, descriptor, view_type=None):
        try:
            descriptor.__get__
        except AttributeError:
            raise TypeError(
                "NamedTemplateImplementation must be passed a descriptor."
                )
        self.descriptor = descriptor
        interface.implementer(INamedTemplate)(self)

        if view_type is not None:
            component.adapter(view_type)(self)
Пример #6
0
 def __call__(self, value, **kws):
     # Step 1: Check that the keyword argument names match the
     #         discriminators
     if set(kws).difference(set(self.discriminators)):
         raise ValueError('One or more keyword arguments did not match the '
                          'discriminators.')
     # Step 2: Create an attribute value factory
     factory = ValueFactory(value, self.value_class, self.discriminators)
     # Step 3: Build the adaptation signature
     signature = []
     for disc in self.discriminators:
         spec = get_specification(kws.get(disc))
         signature.append(spec)
     # Step 4: Assert the adaptation signature onto the factory
     adapter(*signature)(factory)
     implementer(IValue)(factory)
     return factory
Пример #7
0
def uid_to_url(path):
    if not path:
        return ""
    match = RESOLVEUID_RE.match(path)
    if match is None:
        return path

    uid, suffix = match.groups()
    href = uuidToURL(uid)
    if href is None:
        return path
    if suffix:
        href += "/" + suffix
    else:
        target_object = uuidToObject(uid)
        if target_object:
            adapter = queryMultiAdapter((target_object, target_object.REQUEST),
                                        IObjectPrimaryFieldTarget)
            if adapter and adapter():
                href = adapter()
    return href
Пример #8
0
def long_poll_event(source_spec):
    """Class decorator to declare an `ILongPollEvent`.

    :param source_spec: An interface or other specification understood by
        `zope.component` (a plain class can be passed too) that defines the
        source of an event. `IJob` or `storm.base.Storm` for example.
    """
    declare_adapter = adapter(source_spec)

    def declare_event(cls):
        classImplements(cls, ILongPollEvent)
        declare_adapter(cls)
        return cls

    return declare_event
Пример #9
0
def long_poll_event(source_spec):
    """Class decorator to declare an `ILongPollEvent`.

    :param source_spec: An interface or other specification understood by
        `zope.component` (a plain class can be passed too) that defines the
        source of an event. `IJob` or `storm.base.Storm` for example.
    """
    declare_adapter = adapter(source_spec)

    def declare_event(cls):
        classImplements(cls, ILongPollEvent)
        declare_adapter(cls)
        return cls

    return declare_event
Пример #10
0
    def get_item_from_uid(self, block):
        """
        Return serialized item from uid.
        We return the summary one because we want to avoid recursion and too much complex data returned here.
        For example if we serialize the whole context, we will have also all its blocks.
        This could lead to a huge amount of data returned.
        We need to wrap the item with IIndexableObject to be able to get all metadata like it was a brain.
        """
        items = api.content.find(UID=block["UID"], show_inactive=False)
        if len(items) == 0:
            return {}
        item = items[0]

        adapter = getMultiAdapter((item, getRequest()),
                                  ISerializeToJsonSummary)
        return adapter(force_all_metadata=True)
Пример #11
0
def json_compatible(value, context=None):
    """The json_compatible function converts any value to JSON compatible
    data when possible, raising a TypeError for unsupported values.
    This is done by using the IJsonCompatible converters.

    Be aware that adapting the value `None` will result in a component
    lookup error unless `None` is passed in as default value.
    Because of that the `json_compatible` helper method should always be
    used for converting values that may be None.
    """
    if context is not None:
        adapter = queryMultiAdapter((value, context),
                                    IContextawareJsonCompatible)
        if adapter:
            return adapter()
    else:
        return IJsonCompatible(value, None)
Пример #12
0
def extract_text(block, obj, request):
    """Extract text information from a block.

    This function tries the following methods, until it finds a result:
        1. searchableText attribute
        2. Server side adapter
        3. Subblocks

    The decision to use the server side adapter before the subblocks traversal
    allows addon developers to choose this implementation when they want a
    more granular control of the indexing.

    :param block: Dictionary with block information.
    :param obj: Context to be used to get a IBlockSearchableText.
    :param request: Current request.
    :returns: A string with text found in the block.
    """
    result = ""
    block_type = block.get("@type", "")
    # searchableText is the conventional way of storing
    # searchable info in a block
    searchableText = block.get("searchableText", "")
    if searchableText:
        # TODO: should we evaluate in some way this value? maybe passing
        # it into html/plain text transformer?
        return searchableText
    # Use server side adapters to extract the text data
    adapter = queryMultiAdapter((obj, request),
                                IBlockSearchableText,
                                name=block_type)
    result = adapter(block) if adapter is not None else ""
    if not result:
        subblocks = extract_subblocks(block)
        for subblock in subblocks:
            tmp_result = extract_text(subblock, obj, request)
            result = f"{result}\n{tmp_result}"
    return result
Пример #13
0
# -*- coding: utf-8 -*-
from zope.component import adapter
from z3c.form.interfaces import IFormLayer
from z3c.form.util import getSpecification
from plone.formwidget.querystring.widget import QueryStringFieldWidget
from plone.app.contenttypes.behaviors.collection import ICollection


QueryStringFieldWidget = adapter(
    getSpecification(ICollection['query']),
    IFormLayer
)(QueryStringFieldWidget)
Пример #14
0
    ...     interface.implements(zope.security.interfaces.IPrincipal)
    ...     def __init__(self, id, title=None, description=None):
    ...         self.id = id
    ...         self.title = title
    ...         self.description = description
    ...
    >>> dummy_annotation = {}
    >>> class DummyPAU(object):
    ...     interface.implements(interfaces.IPrincipalAnnotationUtility)
    ...     def getAnnotations(self, principal):
    ...         if principal.id == 'sue':
    ...             return dummy_annotation
    ...         raise NotImplementedError
    ...
    >>> pau = DummyPAU()
    >>> component.provideUtility(pau)
    >>> sue = DummyPrincipal('sue')
    >>> annotation = IAnnotations(sue)
    >>> annotation is dummy_annotation
    True
    """ # TODO move this out to a doctest file when we have one...
    utility = component.getUtility(IPrincipalAnnotationUtility)
    return utility.getAnnotations(principal)
component.adapter(zope.security.interfaces.IPrincipal)(annotations)
interface.implementer(IAnnotations)(annotations)

#############################################################################
# BBB: 12/20/2004
PrincipalAnnotationService = PrincipalAnnotationUtility
#############################################################################
Пример #15
0
def hook(provided, object):
	adapter = REGISTRY.lookup1(zope.interface.providedBy(object), provided, '')
	return adapter(object)
 def _wrapper(cls):
     adapter_ = factory(adapter(*ifaces)(cls), key=TIMELINE_ANNOTATIONS_KEY)
     provideAdapter(adapter_)
     return cls
Пример #17
0
    def decorator(class_):
        # Can't reassign the argument defined in the decorator factory
        # function due to how binding in closures apparently (don't)
        # work in Python, hence that argument is _key and is assigned to
        # a different identifier which will be used from here and inside
        # the class.
        key = _key

        classname = to_key(class_.__module__, class_.__name__)
        if key is None:
            key = classname

        names = _iface_fields(iface)

        classImplements(class_, iface)
        class_ = adapter(IAnnotatable)(class_)

        old_init = getattr(class_, '__init__')
        old_setattr = getattr(class_, '__setattr__')

        def __init__(self, context, *a, **kw):
            annotations = IAnnotations(context)
            d = annotations.get(key, _marker)

            if d is _marker:
                d = {}  # to let the loading routine work.
            elif not isinstance(d, PersistentMapping):
                raise TypeError(
                    'Could not instantiate a `%s` from %s with Annotation '
                    '`%s` as it is not of type PersistentMapping' % (
                        classname, context, key))

            for name in names:
                value = d.get(name, _marker)
                if value is _marker:
                    # if instance already has this attribute defined,
                    # do nothing.
                    if hasattr(self, name):
                        continue

                # set the loaded value to the instance only.
                try:
                    old_setattr(self, name, d.get(name))
                except WrongType as e:
                    raise TypeError(
                        'Could not assign attribute `%s` to class `%s` '
                        'with value from Annotation `%s` due to `%s`.' %
                        (name, classname, key, e.__repr__()))

            # finally associate context to the instance.
            old_setattr(self, 'context', context)
            old_init(self, *a, **kw)

        def __setattr__(self, name, value):
            """
            Whenever an attribute is set, persist it into the
            underyling PersistentMapping.
            """

            old_setattr(self, name, value)

            if name not in names:
                return

            d = _setup_dict_annotation(self.context, key)
            d[name] = value

        # TODO make the removal feature not an instance method
        # within every class?

        def uninstall(self):
            """
            Completely removes the annotation from context.
            """

            return _teardown_dict_annotation(self.context, key)

        # bind the modified methods to the input class

        class_.__init__ = MethodType(__init__, None, class_)
        class_.__setattr__ = MethodType(__setattr__, None, class_)
        class_.uninstall = MethodType(uninstall, None, class_)

        return class_
Пример #18
0
    def decorator(class_):
        # Can't reassign the argument defined in the decorator factory
        # function due to how binding in closures apparently (don't)
        # work in Python, hence that argument is _key and is assigned to
        # a different identifier which will be used from here and inside
        # the class.
        key = _key

        classname = to_key(class_.__module__, class_.__name__)
        if key is None:
            key = classname

        names = _iface_fields(iface)

        classImplements(class_, iface)
        class_ = adapter(IAnnotatable)(class_)

        old_init = getattr(class_, '__init__')
        old_setattr = getattr(class_, '__setattr__')

        def __init__(self, context, *a, **kw):
            annotations = IAnnotations(context)
            d = annotations.get(key, _marker)

            if d is _marker:
                d = {}  # to let the loading routine work.
            elif not isinstance(d, PersistentMapping):
                raise TypeError(
                    'Could not instantiate a `%s` from %s with Annotation '
                    '`%s` as it is not of type PersistentMapping' %
                    (classname, context, key))

            for name in names:
                value = d.get(name, _marker)
                if value is _marker:
                    # if instance already has this attribute defined,
                    # do nothing.
                    if hasattr(self, name):
                        continue

                # set the loaded value to the instance only.
                try:
                    old_setattr(self, name, d.get(name))
                except WrongType as e:
                    raise TypeError(
                        'Could not assign attribute `%s` to class `%s` '
                        'with value from Annotation `%s` due to `%s`.' %
                        (name, classname, key, e.__repr__()))

            # finally associate context to the instance.
            old_setattr(self, 'context', context)
            old_init(self, *a, **kw)

        def __setattr__(self, name, value):
            """
            Whenever an attribute is set, persist it into the
            underyling PersistentMapping.
            """

            old_setattr(self, name, value)

            if name not in names:
                return

            d = _setup_dict_annotation(self.context, key)
            d[name] = value

        # TODO make the removal feature not an instance method
        # within every class?

        def uninstall(self):
            """
            Completely removes the annotation from context.
            """

            return _teardown_dict_annotation(self.context, key)

        # bind the modified methods to the input class

        class_.__init__ = MethodType(__init__, None, class_)
        class_.__setattr__ = MethodType(__setattr__, None, class_)
        class_.uninstall = MethodType(uninstall, None, class_)

        return class_
 def _wrapper(cls):
     adapter_ = factory(adapter(*ifaces)(cls), key=TIMELINE_ANNOTATIONS_KEY)
     provideAdapter(adapter_)
     return cls
Пример #20
0
    widget = FieldWidget(field, AjaxSelectWidget(request))
    widget.vocabulary = 'plone.app.vocabularies.Users'
    return widget


@adapter(getSpecification(IOwnership['creators']), IWidgetsLayer)
@implementer(IFieldWidget)
def CreatorsFieldWidget(field, request):
    widget = FieldWidget(field, AjaxSelectWidget(request))
    widget.vocabulary = 'plone.app.vocabularies.Users'
    return widget


if HAS_RF:
    RelatedItemsFieldWidget = adapter(
        getSpecification(IRelatedItems['relatedItems']),
        IWidgetsLayer)(RelatedItemsFieldWidget)

if HAS_PAC:

    @adapter(getSpecification(ICollection['query']), IWidgetsLayer)
    @implementer(IFieldWidget)
    def QueryStringFieldWidget(field, request):
        return FieldWidget(field, QueryStringWidget(request))

    @adapter(getSpecification(IRichText['text']), IWidgetsLayer)
    @implementer(IFieldWidget)
    def RichTextFieldWidget(field, request):
        return FieldWidget(field, RichTextWidget(request))

Пример #21
0
# -*- coding: utf-8 -*-
from plone.app.contenttypes.behaviors.collection import ICollection
from plone.formwidget.querystring.widget import QueryStringFieldWidget
from z3c.form.interfaces import IFormLayer
from z3c.form.util import getSpecification
from zope.component import adapter

QueryStringFieldWidget = adapter(getSpecification(ICollection['query']),
                                 IFormLayer)(QueryStringFieldWidget)
Пример #22
0
class Queue(object):
    """
    The Queue adapter stores a list of Jobs to process.
    """

    interface.implements(IQueue)
    component.adapter(IPloneSiteRoot)
    security = ClassSecurityInformation()

    def __init__(self, context):
        """
        Constructor: load the annotations, which are stored on the
        plone site.

        @param context:     any context
        @type context:      Plone object
        """
        self.context = aq_inner(context.portal_url.getPortalObject())
        self.annotations = IAnnotations(self.context)
        self.logger = getLogger()

    security.declarePrivate('_get_jobs_queue')

    def _get_jobs_queue(self):
        if 'publisher-queue' not in self.annotations:
            self.annotations['publisher-queue'] = zc.queue.Queue()
        return self.annotations['publisher-queue']

    security.declarePrivate('_get_worker_queue')

    def _get_worker_queue(self):
        if 'worker-queue' not in self.annotations:
            self.annotations['worker-queue'] = zc.queue.Queue()
        return self.annotations['worker-queue']

    security.declarePrivate('get_worker_queue')

    def get_worker_queue(self):
        return self._get_worker_queue()

    security.declarePrivate('move_to_worker_queue')

    def move_to_worker_queue(self):
        job_queue = self._get_jobs_queue()
        worker_queue = self._get_worker_queue()

        # The worker queue threshold should be small
        # because we want the move-loop to terminate fast
        # but it should be enough big to use one cronjob
        # iteration, which is usually 1-2 minutes.
        worker_queue_threshold = 1000

        if len(worker_queue) > worker_queue_threshold:
            return

        if not len(job_queue):
            return

        while len(job_queue) > 0 and len(
                worker_queue) < worker_queue_threshold:
            try:
                if os.path.getsize(self.nextJob().dataFile) == 0:
                    # ABORT
                    self.logger.warning(
                        'Aborting move job to worker queue because data file'
                        ' is empty and async extractor may not be finished.')
                    self.logger.info('Worker queue: {} / {}'.format(
                        len(worker_queue), len(job_queue)))
                    transaction.commit()
                    return
            except OSError:
                pass

            worker_queue.put(job_queue.pull())

        self.logger.info('Worker queue: {} / {}'.format(
            len(worker_queue), len(job_queue)))
        transaction.commit()

    security.declarePrivate('getJobs')

    def getJobs(self):
        """
        Returns a list of Job objects
        @return:        job-objects
        @rtype:         list
        """

        # zc.queue.Queue does not support + operator
        return tuple(self._get_worker_queue()) + tuple(self._get_jobs_queue())

    security.declarePrivate('clearJobs')

    def clearJobs(self):
        """
        Remove all jobs from the queue.
        """
        jobs_queue = self._get_jobs_queue()
        worker_queue = self._get_worker_queue()
        map(worker_queue.pull, len(worker_queue) * [0])
        map(jobs_queue.pull, len(jobs_queue) * [0])

    security.declarePrivate('appendJob')

    def appendJob(self, job):
        """
        Appends a Job to the queue
        @param job:     Job object
        @type:          Job
        @return:        None
        """
        if publisher_jobs_are_disabled():
            return None

        if not isinstance(job, Job):
            raise TypeError('Excpected Job object')

        self._get_jobs_queue().put(job)

    security.declarePrivate('createJob')

    def createJob(self, *args, **kwargs):
        """
        Creates a new Job object, adds it to the queue
        and returns it.
        Arguments are redirected to the Job-Constructor.
        @return:    Job object
        @rtype:     Job
        """
        if publisher_jobs_are_disabled():
            return None

        job = Job(*args, **kwargs)
        self.appendJob(job)
        return job

    security.declarePrivate('removeJob')

    def removeJob(self, job):
        """
        Removes a Job from the queue
        @param job:     Job object
        @type job:      Job
        @return:        None
        """
        if not isinstance(job, Job):
            raise TypeError('Excpected Job object')

        job_queue = self._get_jobs_queue()
        worker_queue = self._get_worker_queue()

        if job in job_queue:
            job_queue.pull(tuple(job_queue).index(job))
        else:
            worker_queue.pull(tuple(worker_queue).index(job))

    security.declarePrivate('countJobs')

    def countJobs(self):
        """
        Returns the amount of jobs in the queue.
        Used in combination with popJob()
        @return:        Amount of jobs in the queue
        @rtype:         int
        """
        return len(self.getJobs())

    security.declarePrivate('nextJob')

    def nextJob(self):
        """
        Returns the next job to be executed but does not pop it.
        This should only be used when the job is NOT executed but
        when we need to make checks first in order to decide whether
        to execute it or not.
        @return:        Oldest Job object
        @rtype:         Job
        """
        return self._get_jobs_queue()[0]

    security.declarePrivate('popJob')

    def popJob(self):
        """
        Returns the oldest Job from the queue. The Job will be
        removed from the queue immediately!
        @return:        Oldest Job object
        @rtype:         Job
        """
        return self.get_worker_queue().pull()

    security.declarePrivate('_get_executed_jobs_storage')

    def _get_executed_jobs_storage(self):
        """Returns the IOBTree storage object for executed jobs.
        """
        if self.annotations.get('publisher-executed', _marker) == _marker:
            self.annotations['publisher-executed'] = IOBTree()
        return self.annotations['publisher-executed']

    security.declarePrivate('_generate_next_executed_jobs_storage_key')

    def _generate_next_executed_jobs_storage_key(self):
        """Returns a transaction-safe auto-increment value
        http://pyyou.wordpress.com/2009/12/09/how-to-add-a-counter-without-conflict-error-in-zope

        """
        ann_key = 'publisher-executed-key-auto-increment'
        # get the increaser stored in the annotations
        if ann_key not in self.annotations.keys():
            self.annotations[ann_key] = Increaser(0)
        inc = self.annotations[ann_key]
        # increase by one
        inc.set(inc() + 1)
        # check the current max key
        try:
            current_max_key = self._get_executed_jobs_storage().maxKey()
        except ValueError:
            # the storage is empty, start with 0
            current_max_key = 0
        while current_max_key >= inc():
            inc.set(inc() + 1)
        # set and return the new value
        self.annotations[ann_key] = inc
        return inc()

    security.declarePrivate('get_executed_jobs')

    def get_executed_jobs(self, start=0, end=None):
        """Returns a iterator of executed jobs. You can make a batch by
        providing a range with `start` and `end` parameters. The start and
        end parameters represent the index number in the storage, so we can
        expect that the length of the iterate is `end - start`, if there
        are enough objects in the storage.
        A key / value pair is returned.

        """
        data = self._get_executed_jobs_storage()
        if start == 0 and end == None:
            # return all
            return data.iteritems()

        elif start > end or start == end:
            return ()

        else:
            # make a batch without touching unused values
            # the iteritems() method wants the min-key and the
            # max-key which is used as filter then. So we need to map
            # our `start` and `end` (which is the index) to the max-
            # and min-*keys*
            keys = data.keys()[start:end]
            return data.iteritems(min(keys), max(keys))

    security.declarePrivate('get_executed_jobs_length')

    def get_executed_jobs_length(self):
        """Returns the amount of currently stored executed jobs.
        """
        return len(self._get_executed_jobs_storage().keys())

    security.declarePrivate('append_executed_job')

    def append_executed_job(self, job):
        """Add another
        """
        data = self._get_executed_jobs_storage()
        key = self._generate_next_executed_jobs_storage_key()
        data.insert(key, job)
        return key

    security.declarePrivate('remove_executed_job')

    def remove_executed_job(self, key, default=None):
        """Removes the job with the `key` from the executed jobs storage.
        """
        return self._get_executed_jobs_storage().pop(key, default)

    security.declarePrivate('get_executed_job_by_key')

    def get_executed_job_by_key(self, key):
        """Returns a executed job according to its internal storage key
        """
        return self._get_executed_jobs_storage()[key]

    security.declarePrivate('clear_executed_jobs')

    def clear_executed_jobs(self):
        """Removes all jobs from the executed jobs storage.
        """
        for key, job in self.get_executed_jobs():
            job.removeJob()
        self._get_executed_jobs_storage().clear()

    security.declarePrivate('remove_executed_jobs_older_than')

    def remove_executed_jobs_older_than(self, time):
        """Removes all executed jobs which are older
        than `time` (datetime instance).

        """
        def _get_date_of_job(job):
            """Returns the date of the newest execution of this job or None.
            """
            if getattr(job, 'executed_list', None) == None:
                return None
            elif len(job.executed_list) == 0:
                return None
            else:
                return job.executed_list[-1]['date']

        # start end
        data = self._get_executed_jobs_storage()
        for key in tuple(data.keys()):
            job = data.get(key)
            date = _get_date_of_job(job)
            if not date:
                continue
            elif date < time:
                self.remove_executed_job(key)
                job.removeJob()
            else:
                break

    security.declarePrivate('remove_jobs_by_filter')

    def remove_jobs_by_filter(self, filter_method):
        """Removs jobs by a filter method.
        The `filter_method` gets the `key` and the `job` as parameters.
        If it returns `True` the job os deleted.

        """
        for key, job in tuple(self.get_executed_jobs()):
            if filter_method(key, job):
                self.remove_executed_job(key)
                job.removeJob()
Пример #23
0
    widget = FieldWidget(field, AjaxSelectWidget(request))
    widget.vocabulary = "plone.app.vocabularies.Users"
    return widget


@adapter(getSpecification(IOwnership["creators"]), IWidgetsLayer)
@implementer(IFieldWidget)
def CreatorsFieldWidget(field, request):
    widget = FieldWidget(field, AjaxSelectWidget(request))
    widget.vocabulary = "plone.app.vocabularies.Users"
    return widget


if HAS_RF:
    RelatedItemsFieldWidget = adapter(getSpecification(IRelatedItems["relatedItems"]), IWidgetsLayer)(
        RelatedItemsFieldWidget
    )

if HAS_PAC:

    @adapter(getSpecification(ICollection["query"]), IWidgetsLayer)
    @implementer(IFieldWidget)
    def QueryStringFieldWidget(field, request):
        return FieldWidget(field, QueryStringWidget(request))

    @adapter(getSpecification(IRichText["text"]), IWidgetsLayer)
    @implementer(IFieldWidget)
    def RichTextFieldWidget(field, request):
        return FieldWidget(field, RichTextWidget(request))