fixbot/module.py
author Tero Marttila <terom@fixme.fi>
Sat, 20 Feb 2010 23:06:43 +0200
changeset 64 8574aeff9b36
parent 62 e4db89a5f6bc
permissions -rw-r--r--
blind error handling tweaks
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
from twisted.internet import protocol
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
from twisted.application import service, internet
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
from twisted.python import log
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
import datetime
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
class ModuleInfo (object) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
        Nexus-side handle on a Module
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    # module's name
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    name = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    def __str__ (self) :
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    16
        return "%s" % (self.name)
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    def __repr__ (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        return "<module %s>" % (self.name, )
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
class Event (object) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        An Event, sent by a Module to the Nexus, to be distributed further
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    # the ModuleInfo object
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    module = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    # the event type as a string
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    type = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    # event message as a string (up to 64k, although that won't fit onto IRC..)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    msg = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    # timestamp as a datetime.datetime
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    when = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    def __init__ (self, module, type, msg) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        self.module = module
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        self.type = type
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        self.msg = msg
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        self.when = datetime.datetime.now()
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    def __str__ (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        return "[%s] %s" % (self.type, self.msg)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    def __repr__ (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        return "%s @ %s" % (self.type, self.when)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
class Module (ModuleInfo, protocol.ClientFactory) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        Module core, handles the API connection state and processes all messages
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
    # our API connection to the Nexus
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    connection = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    def __init__ (self, config, protocol) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            config      - configuration for connecting to nexus
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
            protocol    - API client protocol to use for this factory
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        self.protocol = protocol
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        self.connection = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        # XXX: legacy: self.secret = config['api-secret']
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    73
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    def _onRegistered (self, connection) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
            Connected to nexus and registered
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        log.msg("Connected and registered")
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        self.connection = connection
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    82
        
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    83
        # XXX: abort on errors?
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        self.handleConnect()
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    86
    
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    87
    # XXX: unused, bad interface
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    def disconnect (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
            Disconnect from Nexus
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        self.connection.transport.loseConnection()
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    94
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
    95
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    def sendEvent (self, type, msg) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
            Send event to nexus
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
        self.connection.sendEvent(Event(self, type, msg))
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   103
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    def handleConnect (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            Do something once we are connected to nexus and registered
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        pass
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
64
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   111
    def abort (self, err) :
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   112
        """
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   113
            Abort this module, disconnecting with the given error
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   114
        """
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   115
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   116
        self.connection.abort(str(err))
8574aeff9b36 blind error handling tweaks
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   117
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
def makeService (module_class, config, protocol) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
    s = service.MultiService()
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
    # build factory
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    factory = module_class(config, protocol)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
    # the API client
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    log.msg("Connecting to API server on [%s:%d]" % (config['api-server'], config['api-port']))
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    api_client = internet.TCPClient(config['api-server'], config['api-port'], factory)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    api_client.setServiceParent(s)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    return s
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131