Passed
Push — master ( 3b532d...9b942b )
by P.R.
02:53
created

LoadScheduleClient.__load_schedule()   B

Complexity

Conditions 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 26
ccs 0
cts 10
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
"""
2
Enarksh
3
4
Copyright 2013-2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import os
9
import sys
10
import traceback
11
12
import zmq
13
14
from enarksh.Config import Config
15
from enarksh.controller.message.ScheduleDefinitionMessage import ScheduleDefinitionMessage
16
17
18
class LoadScheduleClient:
19
    """
20
    A client for requesting the controller to load new schedules.
21
    """
22
23
    # ------------------------------------------------------------------------------------------------------------------
24
    def __init__(self, io):
25
        """
26
        Object constructor.
27
28
        :param enarksh.style.EnarkshStyle.EnarkshStyle io: The output decorator.
29
        """
30
        self.__zmq_context = None
31
        """
32
        The ZMQ context.
33
34
        :type: Context
35
        """
36
37
        self.__zmq_controller = None
38
        """
39
        The socket for communicating with the controller.
40
41
        :type: zmq.sugar.socket.Socket
42
        """
43
44
        self._io = io
45
        """
46
        The output decorator.
47
48
        :type: enarksh.style.EnarkshStyle.EnarkshStyle
49
        """
50
51
    # ------------------------------------------------------------------------------------------------------------------
52
    def main(self, filenames):
53
        """
54
        The main function of load_schedule.
55
56
        :param list[str] filenames: The filenames of the schedules to be loaded.
57
        """
58
        # Initialize ZMQ.
59
        self.__zmq_init()
60
61
        # Send XML files to the controller.
62
        status = 0
63
        for filename in filenames:
64
            try:
65
                err = self.__load_schedule(filename)
66
                if err:
67
                    status = -1
68
            except Exception as exception:
69
                print(exception, file=sys.stderr)
70
                traceback.print_exc(file=sys.stderr)
71
                status = -1
72
73
        return status
74
75
    # ------------------------------------------------------------------------------------------------------------------
76
    def __zmq_init(self):
77
        """
78
        Initializes ZMQ.
79
        """
80
        config = Config.get()
81
82
        self.__zmq_context = zmq.Context()
83
84
        # Create socket for communicating with the controller.
85
        self.__zmq_controller = self.__zmq_context.socket(zmq.REQ)
86
        self.__zmq_controller.connect(config.get_controller_lockstep_end_point())
87
88
    # ------------------------------------------------------------------------------------------------------------------
89
    def __load_schedule(self, filename):
90
        """
91
        Sends a message to the controller to load a new schedule definition.
92
93
        :param str filename: The name of XML file with the schedule definition.
94
95
        :rtype bool: True on success. Otherwise False.
96
        """
97
        with open(filename, 'rt', encoding='utf-8') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ([a-z_][a-z0-9_]{1,60}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
98
            xml = f.read()
99
100
        # Compose the message for the controller.
101
        message = ScheduleDefinitionMessage(xml, os.path.realpath(filename))
102
103
        # Send the message to the controller.
104
        self.__zmq_controller.send_pyobj(message)
105
106
        # Await the response from the controller.
107
        response = self.__zmq_controller.recv_json()
108
109
        if response['ret'] == 0:
110
            self._io.log_verbose(response['message'])
111
        else:
112
            self._io.error(response['message'])
113
114
        return response['ret'] == 0
115
116
# ----------------------------------------------------------------------------------------------------------------------
117