Completed
Push — master ( c12343...f97e79 )
by Andrii
14:30
created

Daemon   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 72
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 7 3
A quit() 0 3 1
A connect_external() 0 8 2
A start() 0 3 1
A __init__() 0 10 1
B login() 0 23 5
A connect_internal() 0 2 1
A stop() 0 2 1
A request() 0 4 2
1
#!/usr/bin/env python
2
3
import os
4
import time
5
import socket
6
7
from pprint import pprint
8
9
from heppy.EPP import REPP
10
from heppy.Error import Error
11
from heppy.Login import Login
12
from heppy.Client import Client
13
from heppy.Request import Request
14
from heppy.Response import Response
15
16
class Daemon:
17
    def __init__(self, config):
18
        self.config = config
19
        self.is_external = False
20
        self.client = None
21
        self.handler = SignalHandler({
22
            'SIGINT':  self.quit,
23
            'SIGTERM': self.quit,
24
            'SIGHUP':  self.hello,
25
            'SIGUSR1': self.hello,
26
            'SIGUSR2': self.hello,
27
        })
28
29
    def quit(self):
30
        global quit
31
        quit()
32
33
    def start(self,args = {}):
34
        self.connect()
35
        self.login(args)
36
37
    def connect(self):
38
        if self.client is not None:
39
            return
40
        if self.is_external:
41
            self.connect_external()
42
        else:
43
            self.connect_internal()
44
45
    def login(self, args = {}):
46
        greeting = self.client.get_greeting()
47
        greetobj = Response.parsexml(greeting)
48
        pprint(greetobj.data)
49
        try:
50
            request = Login.build(self.config, greeting, args)
51
            query = str(request)
52
            print Request.prettifyxml(query)
53
            reply = self.client.request(query)
54
            print Request.prettifyxml(reply)
55
        except Error as e:
56
            Error.die(2, 'failed perform login request')
57
        error = None
58
        try:
59
            response = Response.parsexml(reply)
60
            data = response.data
61
            pprint(data)
62
        except Error as e:
63
            error = e.message
64
            data = e.data
65
        if error is not None and data['resultCode']!='2002':
66
            Error.die(2, 'bad login response', data)
67
        print 'LOGIN OK'
68
69
    def connect_internal(self):
70
        self.client = REPP(self.config['epp'])
71
72
    def connect_external(self):
73
        try:
74
            self.client = Client(self.config['local']['address'])
75
            self.client.connect()
76
        except socket.error as e:
77
            os.system(self.config['zdir'] + '/eppyd ' + self.config['path'] + ' &')
78
            time.sleep(2)
79
            self.client = Client(self.config['local']['address'])
80
81
    def stop(self, args = {}):
82
        Error.die(3, 'stop not implemented', config)
83
84
    def request(self, query):
85
        with self.handler.block_signals():
86
            reply = self.client.request(query)
87
        return reply
88
89