Хранилища Subversion pytwidcpp

Редакция

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

Редакция Автор № строки Строка
2 scream 1
# -*- coding: utf-8 -*-
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
 
10
class NMDC(protocol.Protocol):
11
    """
12
    NMDC Protocol implementation
13
    """
14
 
15
    buffer = ""
16
    encoding = None
17
    hostname = None
18
 
19
    def dataReceived(self, data):
20
        print "Data: %s" % data
21
        lines = (self.buffer + data).split("|")
22
        print self.buffer
23
        for line in lines:
24
            if len(line) <= 2:
25
                continue
26
            print "Line: %s" % line
27
            command, params = self.parseLine(line)
28
        self.handleCommand(command, params)
29
 
30
    def connectionLost(self, reason):
31
        print "Conlost: %s" %self
32
        pass
33
 
34
    def connectionMade(self):
35
        print "Made connect"
36
        if self.hostname is None:
37
            self.hostname = socket.getfqdn()
38
 
39
    def sendLine(self, line):
40
        _to_send = line.encode('utf-8')
41
        self.transport.write(_to_send)
42
 
43
    def parseLine(self, line):
44
        """
45
        Returns NMDC command and params
46
        """
47
        print "parse: %s" % line
48
        if line.startswith("$"):
49
            try:
50
                m = re.match("^\$(?P<command>[a-zA-Z]+)[ ](?P<params>.+$)", line)
51
 
52
                command = m.group("command")
53
                params = m.group("params")
54
                print "NMDC Command: %s; Params: %s" %(command, params)
55
            except:
56
                print "Exception: %s"% line
57
            return command, params
58
        else:
59
            return "", ""
60
 
61
    def handleCommand(self, command, params):
62
        print "handleCommand: %s %s" % (command, params)
63
        method = getattr(self, "nmdc_%s" % command, None)
64
        try:
65
            if method is not None:
66
                method(params)
67
            else: self.nmdc_unknown(command, params)
68
        except:
69
            print "handleCommand except"
70
            log.deferr()
71
        pass
72
 
73
    def makeAnswer(self, command, params):
74
        if params is not "":
75
            answer = "$%s %s|" % (command, params)
76
        else:
77
            answer = "$%s|"% command
78
 
79
        self.sendLine(answer)
80
        print "makeAnswer: %s" % answer
81
 
82
    def nmdc_unknown(self, command, params):
83
        print "nmdc_%s doesn't exist. Called with params: %s" % (command,params)
84