|
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
|
|
|
import enarksh |
|
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): |
|
25
|
|
|
self._zmq_context = None |
|
26
|
|
|
""" |
|
27
|
|
|
The ZMQ context. |
|
28
|
|
|
|
|
29
|
|
|
:type: Context |
|
30
|
|
|
""" |
|
31
|
|
|
|
|
32
|
|
|
self._zmq_controller = None |
|
33
|
|
|
""" |
|
34
|
|
|
The socket for communicating with the controller. |
|
35
|
|
|
|
|
36
|
|
|
:type: zmq.sugar.socket.Socket |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
40
|
|
|
def main(self, filenames): |
|
41
|
|
|
""" |
|
42
|
|
|
The main function of load_schedule. |
|
43
|
|
|
|
|
44
|
|
|
:param list[str] filenames: The filenames of the schedules to be loaded. |
|
45
|
|
|
""" |
|
46
|
|
|
# Initialize ZMQ. |
|
47
|
|
|
self._zmq_init() |
|
48
|
|
|
|
|
49
|
|
|
# Send XML files to the controller. |
|
50
|
|
|
status = 0 |
|
51
|
|
|
for filename in filenames: |
|
52
|
|
|
try: |
|
53
|
|
|
err = self._load_schedule(filename) |
|
54
|
|
|
if err: |
|
55
|
|
|
status = -1 |
|
56
|
|
|
except Exception as exception: |
|
57
|
|
|
print(exception, file=sys.stderr) |
|
58
|
|
|
traceback.print_exc(file=sys.stderr) |
|
59
|
|
|
status = -1 |
|
60
|
|
|
|
|
61
|
|
|
return status |
|
62
|
|
|
|
|
63
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
64
|
|
|
def _zmq_init(self): |
|
|
|
|
|
|
65
|
|
|
self._zmq_context = zmq.Context() |
|
66
|
|
|
|
|
67
|
|
|
# Create socket for communicating with the controller. |
|
68
|
|
|
self._zmq_controller = self._zmq_context.socket(zmq.REQ) |
|
69
|
|
|
self._zmq_controller.connect(enarksh.CONTROLLER_LOCKSTEP_END_POINT) |
|
70
|
|
|
|
|
71
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
72
|
|
|
def _load_schedule(self, filename): |
|
73
|
|
|
""" |
|
74
|
|
|
Sends a message to the controller to load a new schedule definition. |
|
75
|
|
|
|
|
76
|
|
|
:param str filename: The name of XML file with the schedule definition. |
|
77
|
|
|
|
|
78
|
|
|
:rtype bool: True on success. Otherwise False. |
|
79
|
|
|
""" |
|
80
|
|
|
with open(filename, 'rt', encoding='utf-8') as f: |
|
|
|
|
|
|
81
|
|
|
xml = f.read() |
|
82
|
|
|
|
|
83
|
|
|
# Compose the message for the controller. |
|
84
|
|
|
message = ScheduleDefinitionMessage(xml, os.path.realpath(filename)) |
|
85
|
|
|
|
|
86
|
|
|
# Send the message to the controller. |
|
87
|
|
|
self._zmq_controller.send_pyobj(message) |
|
88
|
|
|
|
|
89
|
|
|
# Await the response from the controller. |
|
90
|
|
|
response = self._zmq_controller.recv_json() |
|
91
|
|
|
|
|
92
|
|
|
print(response['message'], end='') |
|
93
|
|
|
if response['message'][-1:] != '\n': |
|
94
|
|
|
print() |
|
95
|
|
|
|
|
96
|
|
|
return response['ret'] == 0 |
|
97
|
|
|
|
|
98
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
99
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.