event.py 3.81 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-
""" event.py

"""
5
from gluon.storage import Storage
6
from gluon.tools import PluginManager
7 8 9 10 11 12 13 14 15
from dataframes import (get_items,
                        get_items_per_year,
                        get_items_small,
                        get_items_small_per_year,
                        get_objectlike_items,
                        get_objectlike_items_per_year,
                        get_peoplelike_items,
                        get_peoplelike_items_per_year,
                        get_people_per_year)
16

LE GAC Renaud's avatar
LE GAC Renaud committed
17

18 19 20 21
class EventException(BaseException):
    pass


22
class Event(object):
23
    """Initialise the plugin event.
24

25 26 27
    Note:
        It has to be performed after the initialisation
        of the plugin dbui.
28

29
    """
30
    @staticmethod
31 32
    def define_paths(lg="en"):
        """Define paths for the plugin_event.
33

34 35 36
        Args:
            lg (str): the language. Possible values are ``en`` or ``fr``.

37 38 39 40 41 42 43 44
        Note:
            The corresponding files will be loaded
            via the controller ``plugin_dbui/index``

        """
        dbui = PluginManager("dbui").dbui

        dbui.plugins_paths["event"] = {
45
            "css": "static/plugin_event/resources/css/",
46
            "debug": None,
47
            "lg": "static/plugin_event/locale/event-lang-%s.js" % lg,
48
            "libmin": "static/plugin_event/event-min.js"}
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    @staticmethod
    def get_source(name):
        """Get the source identified by its name.

        Raised:
            EventException

        Args:
            name (str): name of the source to be used in the UI.

        Returns:
            reference:
                the function to generate the DataFrame.

        """
        event = PluginManager("event").event
        if event is None:
            raise EventException("Plugin event is not configured.")

        sources = event.sources
        if sources is None:
            raise EventException("Plugin event sources are not configured.")

        if name not in sources:
74
            raise EventException("The source %s is not registered" % name)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

        return sources[name].func

    @staticmethod
    def get_sources():
        """Return the list of registered sources.

        Raises:
            EventException

        Returns:
            iterator:
                names of the registered sources.

        """
        event = PluginManager("event").event
        if event is None:
            raise EventException("Plugin event is not configured.")

        sources = event.sources
        if sources is None:
            raise EventException("Sources are not configured.")

        return sources.iterkeys()

100
    @staticmethod
101
    def register_source(name, func):
102 103 104 105 106 107 108 109 110
        """Register sources which are used in the reporting section.

        Args:
            name (str): name of the source to be used in the UI.
            func (reference): the function generating the DataFrame.

        """
        event = PluginManager("event").event

111 112
        if event.sources is None:
            event.sources = Storage()
113

114
        event.sources[name] = Storage(func=func)
115 116 117 118 119 120

    @staticmethod
    def register_sources():
        """Register all sources defined in plugin_event.

        """
LE GAC Renaud's avatar
LE GAC Renaud committed
121
        Event.register_source("items", get_items)
122
        Event.register_source("items / year", get_items_per_year)
LE GAC Renaud's avatar
LE GAC Renaud committed
123
        Event.register_source("items small", get_items_small)
124
        Event.register_source("items small / year", get_items_small_per_year)
LE GAC Renaud's avatar
LE GAC Renaud committed
125 126
        Event.register_source("object like", get_objectlike_items)

127
        Event.register_source("object like / year",
LE GAC Renaud's avatar
LE GAC Renaud committed
128 129 130 131
                              get_objectlike_items_per_year)

        Event.register_source("people like", get_peoplelike_items)

132
        Event.register_source("people like / year",
LE GAC Renaud's avatar
LE GAC Renaud committed
133 134
                              get_peoplelike_items_per_year)

135
        Event.register_source("people / year", get_people_per_year)