Хранилища Subversion pytwidcpp

Редакция

Редакция 4 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
6 scream 1
Я -*- coding: utf-8 -*-
2 scream 2
""" PyCODC NMDC/ADC Client """
3
from twisted.internet import protocol
4
from twisted.protocols import basic
5
from twisted.python import log
6
 
7
import socket
8
import re
9
 
4 scream 10
class NMDC(basic.LineReceiver):
2 scream 11
    """
12
    NMDC Protocol implementation
13
    """
14
 
4 scream 15
    delimiter = "|"
16
 
2 scream 17
    buffer = ""
18
    encoding = None
19
    hostname = None
20
 
4 scream 21
    def lineReceived(self, line):
22
        print "Line Received: %s" % line
6 scream 23
        self.parseLine(line)
2 scream 24
 
25
    def connectionLost(self, reason):
26
        print "Conlost: %s" %self
27
 
28
    def connectionMade(self):
29
        print "Made connect"
30
        if self.hostname is None:
31
            self.hostname = socket.getfqdn()
32
 
33
    def sendLine(self, line):
34
        _to_send = line.encode('utf-8')
35
        self.transport.write(_to_send)
36
 
37
    def parseLine(self, line):
38
        """
39
        Returns NMDC command and params
40
        """
41
        print "parse: %s" % line
42
        if line.startswith("$"):
43
            try:
44
                m = re.match("^\$(?P<command>[a-zA-Z]+)[ ](?P<params>.+$)", line)
45
 
46
                command = m.group("command")
47
                params = m.group("params")
6 scream 48
                self.handleCommand(command, params)
2 scream 49
            except:
50
                print "Exception: %s"% line
51
 
52
    def handleCommand(self, command, params):
53
        print "handleCommand: %s %s" % (command, params)
54
        method = getattr(self, "nmdc_%s" % command, None)
55
        try:
56
            if method is not None:
57
                method(params)
58
            else: self.nmdc_unknown(command, params)
59
        except:
60
            print "handleCommand except"
61
            log.deferr()
62
        pass
63
 
64
    def makeAnswer(self, command, params):
65
        if params is not "":
66
            answer = "$%s %s|" % (command, params)
67
        else:
68
            answer = "$%s|"% command
69
 
70
        self.sendLine(answer)
71
        print "makeAnswer: %s" % answer
72
 
6 scream 73
    def nmdc_rawline(self, line):
74
        print "rawline: %s"% line
2 scream 75
 
6 scream 76
    def nmdc_unknown(self, command):
77
        print "nmdc_%s doesn't exist." % command
78