File: dispatcher.py

package info (click to toggle)
gajim 0.16.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 23,884 kB
  • sloc: python: 61,879; sh: 10,928; makefile: 132; xml: 74
file content (117 lines) | stat: -rw-r--r-- 3,651 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
74
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright (c) 2010, Alexander Cherniuk (ts33kr@gmail.com)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
Backbone of the command system. Provides smart and controllable
dispatching mechanism with an auto-discovery functionality. In addition
to automatic discovery and dispatching, also features manual control
over the process.
"""

from types import NoneType
from tools import remove

COMMANDS = {}
CONTAINERS = {}

def add_host(host):
    CONTAINERS[host] = []

def remove_host(host):
    remove(CONTAINERS, host)

def add_container(container):
    for host in container.HOSTS:
        CONTAINERS[host].append(container)

def remove_container(container):
    for host in container.HOSTS:
        remove(CONTAINERS[host], container)

def add_commands(container):
    commands = COMMANDS.setdefault(container, {})
    for command in traverse_commands(container):
        for name in command.names:
            commands[name] = command

def remove_commands(container):
    remove(COMMANDS, container)

def traverse_commands(container):
    for name in dir(container):
        attribute = getattr(container, name)
        if is_command(attribute):
            yield attribute

def is_command(attribute):
    from framework import Command
    return isinstance(attribute, Command)

def is_root(namespace):
    metaclass = namespace.get("__metaclass__", NoneType)
    return issubclass(metaclass, Dispatchable)

def get_command(host, name):
    for container in CONTAINERS[host]:
        command = COMMANDS[container].get(name)
        if command:
            return command

def list_commands(host):
    for container in CONTAINERS[host]:
        commands = COMMANDS[container]
        for name, command in commands.iteritems():
            yield name, command

class Dispatchable(type):

    def __init__(self, name, bases, namespace):
        parents = super(Dispatchable, self)
        parents.__init__(name, bases, namespace)
        if not is_root(namespace):
            self.dispatch()

    def dispatch(self):
        if self.AUTOMATIC:
            self.enable()

class Host(Dispatchable):

    def enable(self):
        add_host(self)

    def disable(self):
        remove_host(self)

class Container(Dispatchable):

    def enable(self):
        add_container(self)
        add_commands(self)

    def disable(self):
        remove_commands(self)
        remove_container(self)