Python Client  0.0.0.0
Python wrapper for Corelink DLL
corelink_const.py
Go to the documentation of this file.
1 """
2  @file corelink_const.py
3  Copies the constants from the DLL to wrapper
4 """
5 import ctypes
6 from sys import platform
7 import config
8 
9 # pylint: disable=global-statement
10 # global declaration is essential
11 DLL_PATH = config.DLL_PATH
12 LIB = None
13 STATE_CODES = dict()
14 ERROR_CODES = dict()
15 DATA_CODES = dict()
16 CALL_BACK = dict()
17 
19  """
20  initialize State Codes
21  """
22  global LIB,STATE_CODES
23  arr = ["STREAM_STATE_NONE",
24  "STREAM_STATE_SEND_UDP",
25  "STREAM_STATE_SEND_TCP",
26  "STREAM_STATE_RECV_UDP",
27  "STREAM_STATE_RECV_TCP",
28  "STREAM_STATE_SEND",
29  "STREAM_STATE_RECV",
30  "STREAM_STATE_UDP",
31  "STREAM_STATE_TCP",
32  "STREAM_STATE_ALL"]
33  for string in arr:
34  STATE_CODES[string] = ctypes.c_int.in_dll(LIB, string).value
35 
37  """
38  Initialize Error Codes
39  """
40  global LIB
41  global ERROR_CODES
42  arr = ["ERROR_CODE_NONE",
43  "ERROR_CODE_STATE",
44  "ERROR_CODE_VALUE",
45  "ERROR_CODE_SOCKET",
46  "ERROR_CODE_COMM",
47  "ERROR_CODE_NO_TOKEN",
48  ]
49  for string in arr:
50  ERROR_CODES[string] = ctypes.c_int.in_dll(LIB,string).value
51 
53  """
54  Initialize CallBack Codes
55  """
56  global LIB,CALL_BACK
57  arr = ["CALLBACK_DROPPED",
58  "CALLBACK_STALE",
59  "CALLBACK_SUBSCRIBE",
60  "CALLBACK_UPDATE",
61  ]
62  for string in arr:
63  CALL_BACK[string] = ctypes.c_int.in_dll(LIB,string).value
64 
66  """
67  Initialize Data Codes
68  """
69  global LIB,DATA_CODES
70  arr = ["DATA_FUNCTION_LIST",
71  "DATA_FUNCTION_INFO",
72  "DATA_WORK_SPACES",
73  "DATA_STREAM_INFO",
74  "DATA_STREAM_LIST",
75  "DATA_MAX",
76  ]
77  for string in arr:
78  DATA_CODES[string] = ctypes.c_int.in_dll(LIB,string).value
79 
81  """
82  get stream_state_to_bit_index
83  """
84  return LIB.stream_state_to_bit_index(state)
85 
86 def stream_state_name(state):
87  """
88  get stream_state_name
89  """
90  global LIB
91  length = LIB.stream_state_nameLen(state)
92  if length == 0:
93  return ""
94  buffer = ctypes.create_string_buffer(length)
95  LIB.stream_state_nameStr(state, buffer)
96  name = buffer.value
97  name = name.decode('UTF-8')
98  # LIB.stream_state_name.argtypes = [ctypes.c_int]
99  # LIB.stream_state_name.restype = ctypes.c_char_p
100  # ans = (LIB.stream_state_name(ctypes.c_int(state))).decode('UTF-8')
101  return name
102 
103 def error_code_name(error_code):
104  """
105  Get error_code_name
106  """
107  global LIB
108  length = LIB.errorCodeNameLen(error_code)
109  if length == 0:
110  return ""
111  buffer = ctypes.create_string_buffer(length)
112  LIB.errorCodeNameStr(error_code, buffer)
113  name = buffer.value
114  name = name.decode('UTF-8')
115  # LIB.error_code_name.argtypes = [ctypes.c_int]
116  # LIB.error_code_name.restype = ctypes.c_char_p
117  # ans = (LIB.error_code_name(ctypes.c_int(error_code))).decode('UTF-8')
118  return name
119 
120 def init_lib():
121  """
122  Initialize LIB depending on Operating Sysytem
123  """
124  global LIB
125  global DLL_PATH
126  if LIB is not None:
127  return
128  if platform.startswith('win'):
129  LIB = ctypes.CDLL(DLL_PATH+"Corelink.dll")
130  if platform.startswith('linux'):
131  LIB = ctypes.CDLL(DLL_PATH+"libCorelink.so")
132  if platform.startswith('darwin'):
133  LIB = ctypes.CDLL(DLL_PATH+"libCorelink.dylib")
134 
135 init_lib()
138 
139 ERROR_CODE_STRING= [error_code_name(0),
140  error_code_name(1),
141  error_code_name(2),
142  error_code_name(3),
143  error_code_name(4),
144  error_code_name(5)]