web-framework-api 3.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- web_framework_api/WebFramework.py +143 -0
- web_framework_api/__init__.py +0 -0
- web_framework_api/exceptions/WebFrameworkException.py +18 -0
- web_framework_api/utility/Config.py +319 -0
- web_framework_api/utility/DLLHandler.py +88 -0
- web_framework_api/utility/Utils.py +88 -0
- web_framework_api-3.1.0.dist-info/METADATA +189 -0
- web_framework_api-3.1.0.dist-info/RECORD +10 -0
- web_framework_api-3.1.0.dist-info/WHEEL +5 -0
- web_framework_api-3.1.0.dist-info/licenses/license/LICENSE +21 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import os
|
|
3
|
+
from typing import Callable
|
|
4
|
+
|
|
5
|
+
from .utility import Config
|
|
6
|
+
from web_framework_api.utility.DLLHandler import DLLHandler
|
|
7
|
+
from web_framework_api.exceptions.WebFrameworkException import WebFrameworkException
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class WebFramework:
|
|
11
|
+
"""
|
|
12
|
+
Web server
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
@staticmethod
|
|
16
|
+
def get_web_framework_version() -> str:
|
|
17
|
+
"""
|
|
18
|
+
Get WebFramework version
|
|
19
|
+
:return: String representation of version in format {major}.{minor}.{patch}
|
|
20
|
+
"""
|
|
21
|
+
version = DLLHandler.get_instance().call_function("getWebFrameworkVersion", ctypes.c_char_p)
|
|
22
|
+
|
|
23
|
+
return str(version.decode())
|
|
24
|
+
|
|
25
|
+
def __init__(self, implementation: ctypes.c_void_p):
|
|
26
|
+
self.__implementation = implementation
|
|
27
|
+
self.__function_signature = ctypes.CFUNCTYPE(None)
|
|
28
|
+
self.__on_start_server = None
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def from_path(cls, config_path: str) -> "WebFramework":
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
:param config_path: Path to *.json config
|
|
35
|
+
:return:
|
|
36
|
+
"""
|
|
37
|
+
config_path = os.path.abspath(config_path)
|
|
38
|
+
|
|
39
|
+
if not os.path.exists(config_path):
|
|
40
|
+
raise FileNotFoundError(f"Path {config_path} doesn't exist")
|
|
41
|
+
|
|
42
|
+
exception = ctypes.c_void_p(0)
|
|
43
|
+
implementation = DLLHandler.get_instance().call_function("createWebFrameworkFromPath", ctypes.c_void_p,
|
|
44
|
+
ctypes.c_char_p(config_path.encode()),
|
|
45
|
+
ctypes.byref(exception))
|
|
46
|
+
|
|
47
|
+
if exception:
|
|
48
|
+
raise WebFrameworkException(exception.value)
|
|
49
|
+
|
|
50
|
+
return cls(implementation)
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def from_string(cls, server_configuration: str, application_directory: str) -> "WebFramework":
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
:param server_configuration: *.json config file content
|
|
57
|
+
:param application_directory: Working directory
|
|
58
|
+
:return:
|
|
59
|
+
"""
|
|
60
|
+
exception = ctypes.c_void_p(0)
|
|
61
|
+
implementation = DLLHandler.get_instance().call_function("createWebFrameworkFromString", ctypes.c_void_p,
|
|
62
|
+
ctypes.c_char_p(server_configuration.encode()),
|
|
63
|
+
ctypes.c_char_p(application_directory.encode()),
|
|
64
|
+
ctypes.byref(exception))
|
|
65
|
+
|
|
66
|
+
if exception:
|
|
67
|
+
raise WebFrameworkException(exception.value)
|
|
68
|
+
|
|
69
|
+
return cls(implementation)
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def from_config(cls, config: Config) -> "WebFramework":
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
:param config: Config instance
|
|
76
|
+
:return:
|
|
77
|
+
"""
|
|
78
|
+
exception = ctypes.c_void_p(0)
|
|
79
|
+
implementation = DLLHandler.get_instance().call_function("createWebFrameworkFromConfig", ctypes.c_void_p,
|
|
80
|
+
ctypes.c_uint64(config.implementation),
|
|
81
|
+
ctypes.byref(exception))
|
|
82
|
+
|
|
83
|
+
if exception:
|
|
84
|
+
raise WebFrameworkException(exception.value)
|
|
85
|
+
|
|
86
|
+
return cls(implementation)
|
|
87
|
+
|
|
88
|
+
def start(self, wait: bool = False, on_start_server: Callable[[], None] = None):
|
|
89
|
+
"""
|
|
90
|
+
Start server
|
|
91
|
+
:param wait: Wait until server stop(False doesn't work)
|
|
92
|
+
:param on_start_server: On start server callback
|
|
93
|
+
:return:
|
|
94
|
+
"""
|
|
95
|
+
if on_start_server is None:
|
|
96
|
+
def default_function():
|
|
97
|
+
pass
|
|
98
|
+
|
|
99
|
+
on_start_server = default_function
|
|
100
|
+
|
|
101
|
+
self.__on_start_server = on_start_server
|
|
102
|
+
exception = ctypes.c_void_p(0)
|
|
103
|
+
|
|
104
|
+
DLLHandler.get_instance().call_class_member_function("startWebFrameworkServer", None, self.__implementation,
|
|
105
|
+
wait,
|
|
106
|
+
self.__function_signature(self.__on_start_server),
|
|
107
|
+
ctypes.byref(exception))
|
|
108
|
+
|
|
109
|
+
if exception:
|
|
110
|
+
raise WebFrameworkException(exception.value)
|
|
111
|
+
|
|
112
|
+
def stop(self, wait: bool = True):
|
|
113
|
+
"""
|
|
114
|
+
Stop server
|
|
115
|
+
:param wait: Wait until server stop
|
|
116
|
+
:return:
|
|
117
|
+
"""
|
|
118
|
+
exception = ctypes.c_void_p(0)
|
|
119
|
+
DLLHandler.get_instance().call_class_member_function("stopWebFrameworkServer", None, self.__implementation,
|
|
120
|
+
wait,
|
|
121
|
+
ctypes.byref(exception))
|
|
122
|
+
|
|
123
|
+
if exception:
|
|
124
|
+
raise WebFrameworkException(exception.value)
|
|
125
|
+
|
|
126
|
+
def is_server_running(self) -> bool:
|
|
127
|
+
"""
|
|
128
|
+
Is server running
|
|
129
|
+
:return:
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
exception = ctypes.c_void_p(0)
|
|
133
|
+
result = DLLHandler.get_instance().call_class_member_function("isServerRunning", ctypes.c_bool,
|
|
134
|
+
self.__implementation,
|
|
135
|
+
ctypes.byref(exception))
|
|
136
|
+
|
|
137
|
+
if exception:
|
|
138
|
+
raise WebFrameworkException(exception.value)
|
|
139
|
+
|
|
140
|
+
return result
|
|
141
|
+
|
|
142
|
+
def __del__(self):
|
|
143
|
+
DLLHandler.get_instance().delete_web_framework(self.__implementation)
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
|
|
3
|
+
from web_framework_api.utility.DLLHandler import DLLHandler
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class WebFrameworkException(Exception):
|
|
7
|
+
"""
|
|
8
|
+
Exception class for WebFramework exceptions
|
|
9
|
+
"""
|
|
10
|
+
def __init__(self, implementation: ctypes.c_void_p):
|
|
11
|
+
self.__implementation = implementation
|
|
12
|
+
|
|
13
|
+
def __str__(self) -> str:
|
|
14
|
+
return DLLHandler.get_instance().call_function("getErrorMessage", ctypes.c_char_p,
|
|
15
|
+
ctypes.c_uint64(self.__implementation)).decode()
|
|
16
|
+
|
|
17
|
+
def __del__(self):
|
|
18
|
+
DLLHandler.get_instance().delete_web_framework_exception(self.__implementation)
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
from multipledispatch import dispatch
|
|
5
|
+
|
|
6
|
+
from web_framework_api.utility.DLLHandler import DLLHandler
|
|
7
|
+
from web_framework_api.exceptions.WebFrameworkException import WebFrameworkException
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Config:
|
|
11
|
+
"""
|
|
12
|
+
Config file representation
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, implementation: ctypes.c_void_p):
|
|
16
|
+
self.implementation = implementation
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
def from_path(cls, config_path: str) -> "Config":
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
:param config_path: Path to *.json config file
|
|
23
|
+
:return:
|
|
24
|
+
"""
|
|
25
|
+
exception = ctypes.c_void_p(0)
|
|
26
|
+
implementation = DLLHandler.get_instance().call_function("createConfigFromPath", ctypes.c_void_p,
|
|
27
|
+
ctypes.c_char_p(config_path.encode()),
|
|
28
|
+
ctypes.byref(exception))
|
|
29
|
+
|
|
30
|
+
if exception:
|
|
31
|
+
raise WebFrameworkException(exception.value)
|
|
32
|
+
|
|
33
|
+
return cls(implementation)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_string(cls, server_configuration: str, application_directory: str) -> "Config":
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
:param server_configuration: *.json config file content
|
|
40
|
+
:param application_directory: Working directory
|
|
41
|
+
:return:
|
|
42
|
+
"""
|
|
43
|
+
exception = ctypes.c_void_p(0)
|
|
44
|
+
implementation = DLLHandler.get_instance().call_function("createConfigFromString", ctypes.c_void_p,
|
|
45
|
+
ctypes.c_char_p(server_configuration.encode()),
|
|
46
|
+
ctypes.c_char_p(application_directory.encode()),
|
|
47
|
+
ctypes.byref(exception))
|
|
48
|
+
|
|
49
|
+
if exception:
|
|
50
|
+
raise WebFrameworkException(exception.value)
|
|
51
|
+
|
|
52
|
+
return cls(implementation)
|
|
53
|
+
|
|
54
|
+
@dispatch(str, str, bool)
|
|
55
|
+
def override_configuration(self, key: str, value: str, recursive: bool = True) -> "Config":
|
|
56
|
+
"""
|
|
57
|
+
Override string
|
|
58
|
+
:param key: JSON key
|
|
59
|
+
:param value: New string value
|
|
60
|
+
:param recursive: Recursive search for key
|
|
61
|
+
:return: self
|
|
62
|
+
"""
|
|
63
|
+
exception = ctypes.c_void_p(0)
|
|
64
|
+
|
|
65
|
+
DLLHandler.get_instance().call_class_member_function("overrideConfigurationString", None, self.implementation,
|
|
66
|
+
ctypes.c_char_p(key.encode()),
|
|
67
|
+
ctypes.c_char_p(value.encode()),
|
|
68
|
+
recursive, ctypes.byref(exception))
|
|
69
|
+
|
|
70
|
+
if exception:
|
|
71
|
+
raise WebFrameworkException(exception.value)
|
|
72
|
+
|
|
73
|
+
return self
|
|
74
|
+
|
|
75
|
+
@dispatch(str, int, bool)
|
|
76
|
+
def override_configuration(self, key: str, value: int, recursive: bool = True) -> "Config":
|
|
77
|
+
"""
|
|
78
|
+
Override integer
|
|
79
|
+
:param key: JSON key
|
|
80
|
+
:param value: New integer value
|
|
81
|
+
:param recursive: Recursive search for key
|
|
82
|
+
:return: self
|
|
83
|
+
"""
|
|
84
|
+
exception = ctypes.c_void_p(0)
|
|
85
|
+
|
|
86
|
+
DLLHandler.get_instance().call_class_member_function("overrideConfigurationInteger", None, self.implementation,
|
|
87
|
+
ctypes.c_char_p(key.encode()), ctypes.c_int64(value),
|
|
88
|
+
recursive, ctypes.byref(exception))
|
|
89
|
+
|
|
90
|
+
if exception:
|
|
91
|
+
raise WebFrameworkException(exception.value)
|
|
92
|
+
|
|
93
|
+
return self
|
|
94
|
+
|
|
95
|
+
@dispatch(str, bool, bool)
|
|
96
|
+
def override_configuration(self, key: str, value: bool, recursive: bool = True) -> "Config":
|
|
97
|
+
"""
|
|
98
|
+
Override bool
|
|
99
|
+
:param key: JSON key
|
|
100
|
+
:param value: New bool value
|
|
101
|
+
:param recursive: Recursive search for key
|
|
102
|
+
:return: self
|
|
103
|
+
"""
|
|
104
|
+
exception = ctypes.c_void_p(0)
|
|
105
|
+
|
|
106
|
+
DLLHandler.get_instance().call_class_member_function("overrideConfigurationBoolean", None, self.implementation,
|
|
107
|
+
ctypes.c_char_p(key.encode()), value,
|
|
108
|
+
recursive, ctypes.byref(exception))
|
|
109
|
+
|
|
110
|
+
if exception:
|
|
111
|
+
raise WebFrameworkException(exception.value)
|
|
112
|
+
|
|
113
|
+
return self
|
|
114
|
+
|
|
115
|
+
def override_configuration_string_array(self, key: str, value: List[str], recursive: bool = True) -> "Config":
|
|
116
|
+
"""
|
|
117
|
+
Override string array
|
|
118
|
+
:param key: JSON key
|
|
119
|
+
:param value: New string array value
|
|
120
|
+
:param recursive: Recursive search for key
|
|
121
|
+
:return: self
|
|
122
|
+
"""
|
|
123
|
+
exception = ctypes.c_void_p(0)
|
|
124
|
+
|
|
125
|
+
data = (ctypes.c_char_p * len(value))()
|
|
126
|
+
|
|
127
|
+
for i in range(len(value)):
|
|
128
|
+
data[i] = value[i].encode()
|
|
129
|
+
|
|
130
|
+
DLLHandler.get_instance().call_class_member_function("overrideConfigurationStringArray", None,
|
|
131
|
+
self.implementation,
|
|
132
|
+
ctypes.c_char_p(key.encode()), ctypes.pointer(data),
|
|
133
|
+
recursive, ctypes.c_int64(len(value)),
|
|
134
|
+
ctypes.byref(exception))
|
|
135
|
+
|
|
136
|
+
if exception:
|
|
137
|
+
raise WebFrameworkException(exception.value)
|
|
138
|
+
|
|
139
|
+
return self
|
|
140
|
+
|
|
141
|
+
def override_configuration_integer_array(self, key: str, value: List[int], recursive: bool = True) -> "Config":
|
|
142
|
+
"""
|
|
143
|
+
Override integer array
|
|
144
|
+
:param key: JSON key
|
|
145
|
+
:param value: New integer array value
|
|
146
|
+
:param recursive: Recursive search for key
|
|
147
|
+
:return: self
|
|
148
|
+
"""
|
|
149
|
+
exception = ctypes.c_void_p(0)
|
|
150
|
+
|
|
151
|
+
data = (ctypes.c_int64 * len(value))()
|
|
152
|
+
|
|
153
|
+
for i in range(len(value)):
|
|
154
|
+
data[i] = value[i]
|
|
155
|
+
|
|
156
|
+
DLLHandler.get_instance().call_class_member_function("overrideConfigurationIntegerArray", None,
|
|
157
|
+
self.implementation,
|
|
158
|
+
ctypes.c_char_p(key.encode()), ctypes.pointer(data),
|
|
159
|
+
recursive, ctypes.c_int64(len(value)),
|
|
160
|
+
ctypes.byref(exception))
|
|
161
|
+
|
|
162
|
+
if exception:
|
|
163
|
+
raise WebFrameworkException(exception.value)
|
|
164
|
+
|
|
165
|
+
return self
|
|
166
|
+
|
|
167
|
+
def override_base_path(self, base_path: str) -> "Config":
|
|
168
|
+
"""
|
|
169
|
+
Override config file directory
|
|
170
|
+
:param base_path: New base path
|
|
171
|
+
:return: self
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
exception = ctypes.c_void_p(0)
|
|
175
|
+
|
|
176
|
+
DLLHandler.get_instance().call_class_member_function("overrideBasePath", None, self.implementation,
|
|
177
|
+
ctypes.c_char_p(base_path.encode()),
|
|
178
|
+
ctypes.byref(exception))
|
|
179
|
+
|
|
180
|
+
if exception:
|
|
181
|
+
raise WebFrameworkException(exception.value)
|
|
182
|
+
|
|
183
|
+
return self
|
|
184
|
+
|
|
185
|
+
def get_configuration_string(self, key: str, recursive: bool = True) -> str:
|
|
186
|
+
"""
|
|
187
|
+
Get string from config
|
|
188
|
+
:param key: Config key
|
|
189
|
+
:param recursive: Search recursively
|
|
190
|
+
:return: Config string value
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
exception = ctypes.c_void_p(0)
|
|
194
|
+
|
|
195
|
+
handler = DLLHandler.get_instance()
|
|
196
|
+
string_implementation = handler.call_class_member_function("getConfigurationString",
|
|
197
|
+
ctypes.c_void_p,
|
|
198
|
+
self.implementation,
|
|
199
|
+
ctypes.c_char_p(key.encode()),
|
|
200
|
+
recursive, ctypes.byref(exception))
|
|
201
|
+
|
|
202
|
+
if exception:
|
|
203
|
+
raise WebFrameworkException(exception.value)
|
|
204
|
+
|
|
205
|
+
result_ptr = handler.call_function("getDataFromString", ctypes.c_char_p,
|
|
206
|
+
ctypes.c_uint64(string_implementation))
|
|
207
|
+
|
|
208
|
+
result = str(result_ptr.decode())
|
|
209
|
+
|
|
210
|
+
handler.delete_web_framework_string(string_implementation)
|
|
211
|
+
|
|
212
|
+
return result
|
|
213
|
+
|
|
214
|
+
def get_configuration_integer(self, key: str, recursive: bool = True) -> int:
|
|
215
|
+
"""
|
|
216
|
+
Get integer from config
|
|
217
|
+
:param key: Config key
|
|
218
|
+
:param recursive: Search recursively
|
|
219
|
+
:return: Config integer value
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
exception = ctypes.c_void_p(0)
|
|
223
|
+
|
|
224
|
+
result = DLLHandler.get_instance().call_class_member_function("getConfigurationInteger",
|
|
225
|
+
ctypes.c_int64,
|
|
226
|
+
self.implementation,
|
|
227
|
+
ctypes.c_char_p(key.encode()),
|
|
228
|
+
recursive, ctypes.byref(exception))
|
|
229
|
+
if exception:
|
|
230
|
+
raise WebFrameworkException(exception.value)
|
|
231
|
+
|
|
232
|
+
return result
|
|
233
|
+
|
|
234
|
+
def get_configuration_boolean(self, key: str, recursive: bool = True) -> bool:
|
|
235
|
+
"""
|
|
236
|
+
Get boolean from config
|
|
237
|
+
:param key: Config key
|
|
238
|
+
:param recursive: Search recursively
|
|
239
|
+
:return: Config boolean value
|
|
240
|
+
"""
|
|
241
|
+
|
|
242
|
+
exception = ctypes.c_void_p(0)
|
|
243
|
+
|
|
244
|
+
result = DLLHandler.get_instance().call_class_member_function("getConfigurationBoolean",
|
|
245
|
+
ctypes.c_bool,
|
|
246
|
+
self.implementation,
|
|
247
|
+
ctypes.c_char_p(key.encode()),
|
|
248
|
+
recursive, ctypes.byref(exception))
|
|
249
|
+
if exception:
|
|
250
|
+
raise WebFrameworkException(exception.value)
|
|
251
|
+
|
|
252
|
+
return result
|
|
253
|
+
|
|
254
|
+
def get_configuration(self) -> str:
|
|
255
|
+
"""
|
|
256
|
+
Get current config JSON string data
|
|
257
|
+
:return:
|
|
258
|
+
"""
|
|
259
|
+
exception = ctypes.c_void_p(0)
|
|
260
|
+
|
|
261
|
+
handler = DLLHandler.get_instance()
|
|
262
|
+
string_implementation = handler.call_class_member_function("getConfiguration", ctypes.c_void_p,
|
|
263
|
+
self.implementation,
|
|
264
|
+
ctypes.byref(exception))
|
|
265
|
+
|
|
266
|
+
if exception:
|
|
267
|
+
raise WebFrameworkException(exception.value)
|
|
268
|
+
|
|
269
|
+
result_ptr = handler.call_function("getDataFromString", ctypes.c_char_p,
|
|
270
|
+
ctypes.c_uint64(string_implementation))
|
|
271
|
+
|
|
272
|
+
result = str(result_ptr.decode())
|
|
273
|
+
|
|
274
|
+
handler.delete_web_framework_string(string_implementation)
|
|
275
|
+
|
|
276
|
+
return result
|
|
277
|
+
|
|
278
|
+
def get_base_path(self) -> str:
|
|
279
|
+
"""
|
|
280
|
+
Get config file directory
|
|
281
|
+
:return:
|
|
282
|
+
"""
|
|
283
|
+
exception = ctypes.c_void_p(0)
|
|
284
|
+
|
|
285
|
+
handler = DLLHandler.get_instance()
|
|
286
|
+
string_implementation = handler.call_class_member_function("getBasePath", ctypes.c_void_p,
|
|
287
|
+
self.implementation,
|
|
288
|
+
ctypes.byref(exception))
|
|
289
|
+
|
|
290
|
+
if exception:
|
|
291
|
+
raise WebFrameworkException(exception.value)
|
|
292
|
+
|
|
293
|
+
result_ptr = handler.call_function("getDataFromString", ctypes.c_char_p,
|
|
294
|
+
ctypes.c_uint64(string_implementation))
|
|
295
|
+
|
|
296
|
+
result = str(result_ptr.decode())
|
|
297
|
+
|
|
298
|
+
handler.delete_web_framework_string(string_implementation)
|
|
299
|
+
|
|
300
|
+
return result
|
|
301
|
+
|
|
302
|
+
def get_raw_configuration(self) -> str:
|
|
303
|
+
"""
|
|
304
|
+
Get raw config JSON string data
|
|
305
|
+
:return:
|
|
306
|
+
"""
|
|
307
|
+
exception = ctypes.c_void_p(0)
|
|
308
|
+
|
|
309
|
+
result = DLLHandler.get_instance().call_class_member_function("getRawConfiguration", ctypes.c_char_p,
|
|
310
|
+
self.implementation,
|
|
311
|
+
ctypes.byref(exception))
|
|
312
|
+
|
|
313
|
+
if exception:
|
|
314
|
+
raise WebFrameworkException(exception.value)
|
|
315
|
+
|
|
316
|
+
return result.decode()
|
|
317
|
+
|
|
318
|
+
def __del__(self):
|
|
319
|
+
DLLHandler.get_instance().delete_web_framework_config(self.implementation)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from web_framework_api.utility.Utils import get_libraries
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def initialize_web_framework(path_to_dll: str = ""):
|
|
11
|
+
"""
|
|
12
|
+
Load WebFramework shared library
|
|
13
|
+
:param path_to_dll: Path to shared library without prefixes(lib for Linux) and file extensions(.dll, .so). By default, take path to WebFramework library from pip package
|
|
14
|
+
:return:
|
|
15
|
+
"""
|
|
16
|
+
if DLLHandler.instance is not None:
|
|
17
|
+
return
|
|
18
|
+
|
|
19
|
+
shared_libraries_dir = os.path.abspath(
|
|
20
|
+
os.path.join(Path(__file__).parent.parent, "dll" if sys.platform == "win32" else "lib"))
|
|
21
|
+
download = False
|
|
22
|
+
empty_path_to_dll = len(path_to_dll) == 0
|
|
23
|
+
|
|
24
|
+
if len(path_to_dll) == 0:
|
|
25
|
+
path_to_dll = os.path.join(shared_libraries_dir, "WebFramework")
|
|
26
|
+
download = True
|
|
27
|
+
|
|
28
|
+
path_to_dll = os.path.abspath(path_to_dll)
|
|
29
|
+
|
|
30
|
+
if sys.platform == "win32":
|
|
31
|
+
path_to_dll = f"{path_to_dll}.dll"
|
|
32
|
+
|
|
33
|
+
os.environ["PATH"] += os.pathsep + f"{shared_libraries_dir}"
|
|
34
|
+
else:
|
|
35
|
+
path = Path(path_to_dll)
|
|
36
|
+
path_to_dll = f"{path.parent}/lib{path.name}.so"
|
|
37
|
+
|
|
38
|
+
if empty_path_to_dll and ("LD_LIBRARY_PATH" in os.environ and shared_libraries_dir not in os.environ[
|
|
39
|
+
"LD_LIBRARY_PATH"]) or "LD_LIBRARY_PATH" not in os.environ:
|
|
40
|
+
raise Exception(f"{shared_libraries_dir} not in LD_LIBRARY_PATH")
|
|
41
|
+
|
|
42
|
+
if not os.path.exists(path_to_dll):
|
|
43
|
+
if not download:
|
|
44
|
+
raise FileNotFoundError(f"Path {path_to_dll} doesn't exist")
|
|
45
|
+
else:
|
|
46
|
+
get_libraries(shared_libraries_dir)
|
|
47
|
+
|
|
48
|
+
DLLHandler.instance = DLLHandler(path_to_dll)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class DLLHandler:
|
|
52
|
+
instance = None
|
|
53
|
+
|
|
54
|
+
def __init__(self, path_to_dll: str):
|
|
55
|
+
self.__handle = ctypes.CDLL(path_to_dll)
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
def get_instance(cls) -> "DLLHandler":
|
|
59
|
+
if cls.instance is None:
|
|
60
|
+
raise Exception("WebFramework must be initialized with initialize_web_framework function")
|
|
61
|
+
|
|
62
|
+
return cls.instance
|
|
63
|
+
|
|
64
|
+
def call_function(self, function_name: str, return_type, *args):
|
|
65
|
+
function = self.__handle[function_name]
|
|
66
|
+
|
|
67
|
+
function.restype = return_type
|
|
68
|
+
|
|
69
|
+
return function(*args)
|
|
70
|
+
|
|
71
|
+
def call_class_member_function(self, function_name: str, return_type, implementation: ctypes.c_void_p, *args):
|
|
72
|
+
function = self.__handle[function_name]
|
|
73
|
+
|
|
74
|
+
function.restype = return_type
|
|
75
|
+
|
|
76
|
+
return function(ctypes.c_uint64(implementation), *args)
|
|
77
|
+
|
|
78
|
+
def delete_web_framework_string(self, implementation: ctypes.c_void_p):
|
|
79
|
+
self.call_function("deleteWebFrameworkString", None, ctypes.c_uint64(implementation))
|
|
80
|
+
|
|
81
|
+
def delete_web_framework_config(self, implementation: ctypes.c_void_p):
|
|
82
|
+
self.call_function("deleteWebFrameworkConfig", None, ctypes.c_uint64(implementation))
|
|
83
|
+
|
|
84
|
+
def delete_web_framework(self, implementation: ctypes.c_void_p):
|
|
85
|
+
self.call_function("deleteWebFramework", None, ctypes.c_uint64(implementation))
|
|
86
|
+
|
|
87
|
+
def delete_web_framework_exception(self, implementation: ctypes.c_void_p):
|
|
88
|
+
self.call_function("deleteWebFrameworkException", None, ctypes.c_uint64(implementation))
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import os.path
|
|
2
|
+
import shutil
|
|
3
|
+
import sys
|
|
4
|
+
import zipfile
|
|
5
|
+
|
|
6
|
+
import requests
|
|
7
|
+
|
|
8
|
+
from tqdm import tqdm
|
|
9
|
+
|
|
10
|
+
WEB_FRAMEWORK_MAJOR_VERSION = "3"
|
|
11
|
+
WEB_FRAMEWORK_MINOR_VERSION = "1"
|
|
12
|
+
WEB_FRAMEWORK_PATCH_VERSION = "0"
|
|
13
|
+
WEB_FRAMEWORK_LIBRARIES_FILE_NAME = "libraries.zip"
|
|
14
|
+
WEB_FRAMEWORK_DOWNLOAD_BLOCK_SIZE = 1024
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def download_libraries(download_path: str):
|
|
18
|
+
url = get_url()
|
|
19
|
+
response = requests.get(url, stream=True)
|
|
20
|
+
|
|
21
|
+
total_size = int(response.headers.get("content-length", 0))
|
|
22
|
+
|
|
23
|
+
with tqdm(total=total_size, unit="B", unit_scale=True) as progress_bar:
|
|
24
|
+
progress_bar.set_description(f"Downloading {url}")
|
|
25
|
+
|
|
26
|
+
with open(f"{download_path}/{WEB_FRAMEWORK_LIBRARIES_FILE_NAME}", "wb") as file:
|
|
27
|
+
for data in response.iter_content(WEB_FRAMEWORK_DOWNLOAD_BLOCK_SIZE):
|
|
28
|
+
progress_bar.update(len(data))
|
|
29
|
+
file.write(data)
|
|
30
|
+
|
|
31
|
+
if total_size != 0 and progress_bar.n != total_size:
|
|
32
|
+
raise RuntimeError("Could not download file")
|
|
33
|
+
|
|
34
|
+
return f"{download_path}/{WEB_FRAMEWORK_LIBRARIES_FILE_NAME}"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def unzip_libraries(path_to_zip: str, output_path: str):
|
|
38
|
+
with zipfile.ZipFile(path_to_zip, "r") as zip_file:
|
|
39
|
+
zip_file.extractall(output_path)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def copy_libraries(libraries_directory: str):
|
|
43
|
+
if sys.platform == "win32":
|
|
44
|
+
directory_path = os.path.join(libraries_directory, "Release_Windows", "dll")
|
|
45
|
+
|
|
46
|
+
for file in os.listdir(directory_path):
|
|
47
|
+
shutil.copy(os.path.join(directory_path, file), libraries_directory)
|
|
48
|
+
|
|
49
|
+
else:
|
|
50
|
+
directory_path = os.path.join(libraries_directory, "Release_Linux", "lib")
|
|
51
|
+
|
|
52
|
+
for file in os.listdir(directory_path):
|
|
53
|
+
if file.endswith(".so"):
|
|
54
|
+
shutil.copy(os.path.join(directory_path, file), libraries_directory)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def remove_files(libraries_directory: str):
|
|
58
|
+
for file in os.listdir(libraries_directory):
|
|
59
|
+
if os.path.isdir(os.path.join(libraries_directory, file)):
|
|
60
|
+
shutil.rmtree(os.path.join(libraries_directory, file))
|
|
61
|
+
|
|
62
|
+
os.remove(os.path.join(libraries_directory, WEB_FRAMEWORK_LIBRARIES_FILE_NAME))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def create_directories(libraries_directory: str):
|
|
66
|
+
if sys.platform == "win32":
|
|
67
|
+
os.makedirs(os.path.join(libraries_directory, "dll"))
|
|
68
|
+
else:
|
|
69
|
+
os.makedirs(os.path.join(libraries_directory, "lib"))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_url():
|
|
73
|
+
if sys.platform == "win32":
|
|
74
|
+
return f"https://github.com/LazyPanda07/WebFramework/releases/download/v{WEB_FRAMEWORK_MAJOR_VERSION}.{WEB_FRAMEWORK_MINOR_VERSION}.{WEB_FRAMEWORK_PATCH_VERSION}/windows.zip"
|
|
75
|
+
else:
|
|
76
|
+
return f"https://github.com/LazyPanda07/WebFramework/releases/download/v{WEB_FRAMEWORK_MAJOR_VERSION}.{WEB_FRAMEWORK_MINOR_VERSION}.{WEB_FRAMEWORK_PATCH_VERSION}/linux.zip"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def get_libraries(libraries_directory: str):
|
|
80
|
+
create_directories(libraries_directory)
|
|
81
|
+
|
|
82
|
+
zip_path = download_libraries(libraries_directory)
|
|
83
|
+
|
|
84
|
+
unzip_libraries(zip_path, libraries_directory)
|
|
85
|
+
|
|
86
|
+
copy_libraries(libraries_directory)
|
|
87
|
+
|
|
88
|
+
remove_files(libraries_directory)
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: web_framework_api
|
|
3
|
+
Version: 3.1.0
|
|
4
|
+
Summary: Python API for WebFramework
|
|
5
|
+
Keywords: Web
|
|
6
|
+
Author-Email: LazyPanda07 <semengricenko@gmail.com>
|
|
7
|
+
Maintainer-Email: LazyPanda07 <semengricenko@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2020 Semyon Gritsenko
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Project-URL: Repository, http://github.com/LazyPanda07/WebFramework
|
|
34
|
+
Project-URL: Wiki, http://github.com/LazyPanda07/WebFramework/wiki
|
|
35
|
+
Requires-Dist: multipledispatch
|
|
36
|
+
Requires-Dist: requests
|
|
37
|
+
Requires-Dist: tqdm
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
C++ HTTP/HTTPS server with Python API
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
* [Quick start](#quick-start)
|
|
44
|
+
* [main.py](#mainpy)
|
|
45
|
+
* [Settings](#settings)
|
|
46
|
+
* [Config](#config)
|
|
47
|
+
* [Run sample](#run-sample)
|
|
48
|
+
* [Executors](#executors)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## Quick start
|
|
52
|
+
Server needs few files to run:
|
|
53
|
+
* [web.json](#settings) with routes
|
|
54
|
+
* [Executors](#executors)
|
|
55
|
+
* [Windows](https://github.com/LazyPanda07/WebFramework/releases/latest/download/hello_executor_windows.zip)
|
|
56
|
+
* [Linux](https://github.com/LazyPanda07/WebFramework/releases/latest/download/hello_executor_linux.zip)
|
|
57
|
+
* [config.json](#config) with server settings
|
|
58
|
+
All these files must be in the same directory as ```main.py```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
### main.py
|
|
62
|
+
```python
|
|
63
|
+
from web_framework_api.WebFramework import WebFramework # Server
|
|
64
|
+
from web_framework_api.utility.DLLHandler import initialize_web_framework # WebFramework initialization
|
|
65
|
+
from web_framework_api.exceptions.WebFrameworkException import WebFrameworkException # Exception
|
|
66
|
+
|
|
67
|
+
def on_start():
|
|
68
|
+
print("Server is running")
|
|
69
|
+
|
|
70
|
+
if __name__ == '__main__':
|
|
71
|
+
try:
|
|
72
|
+
initialize_web_framework() # Load WebFramework shared library
|
|
73
|
+
|
|
74
|
+
server = WebFramework.from_path("config.json") # Create server
|
|
75
|
+
|
|
76
|
+
server.start(True, on_start) # Start server and wait
|
|
77
|
+
except WebFrameworkException as exception:
|
|
78
|
+
print(exception)
|
|
79
|
+
|
|
80
|
+
exit(-1)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Settings
|
|
85
|
+
```web.json```
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"HelloExecutor": {
|
|
89
|
+
"route": "",
|
|
90
|
+
"loadType": "initialization"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
### Config
|
|
97
|
+
```config.json```
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"WebServer": {
|
|
101
|
+
"ip": "0.0.0.0",
|
|
102
|
+
"port": 8080,
|
|
103
|
+
"timeout": 0
|
|
104
|
+
},
|
|
105
|
+
"WebFramework": {
|
|
106
|
+
"settingsPaths": [
|
|
107
|
+
"web.json"
|
|
108
|
+
],
|
|
109
|
+
"loadSources": [
|
|
110
|
+
"hello_executor"
|
|
111
|
+
],
|
|
112
|
+
"assetsPath": "assets",
|
|
113
|
+
"templatesPath": "templates",
|
|
114
|
+
"cachingSize": 536870912,
|
|
115
|
+
"webServerType": "multiThreaded",
|
|
116
|
+
"HTTPS": {
|
|
117
|
+
"useHTTPS": false,
|
|
118
|
+
"pathToCertificate": "certificates/cert.pem",
|
|
119
|
+
"pathToKey": "certificates/key.pem"
|
|
120
|
+
},
|
|
121
|
+
"defaultAssetsPath": "WebFrameworkAssets"
|
|
122
|
+
},
|
|
123
|
+
"Logging": {
|
|
124
|
+
"usingLogging": false,
|
|
125
|
+
"dateFormat": "DMY",
|
|
126
|
+
"logFileSize": 134217728
|
|
127
|
+
},
|
|
128
|
+
"ThreadPoolServer": {
|
|
129
|
+
"threadCount": 0
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
### Run sample
|
|
136
|
+
After running server open url [127.0.0.1:8080](http://127.0.0.1:8080).
|
|
137
|
+
You will see response from server
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"message": "Hello, World!"
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
## Executors
|
|
146
|
+
Executors are C++ classes that responsible for giving responses for their route(url).
|
|
147
|
+
Source code of HelloExecutor from example
|
|
148
|
+
```HelloExecutor.h```
|
|
149
|
+
```cpp
|
|
150
|
+
#pragma once
|
|
151
|
+
|
|
152
|
+
#include "Executors/BaseStatelessExecutor.h"
|
|
153
|
+
|
|
154
|
+
namespace executors
|
|
155
|
+
{
|
|
156
|
+
class HelloExecutor : public framework::BaseStatelessExecutor
|
|
157
|
+
{
|
|
158
|
+
public:
|
|
159
|
+
HelloExecutor() = default;
|
|
160
|
+
|
|
161
|
+
void doGet(framework::HTTPRequest& request, framework::HTTPResponse& response) override;
|
|
162
|
+
|
|
163
|
+
~HelloExecutor() = default;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
```HelloExecutor.cpp```
|
|
168
|
+
```cpp
|
|
169
|
+
#include "HelloExecutor.h"
|
|
170
|
+
|
|
171
|
+
#include "JSONBuilder.h"
|
|
172
|
+
|
|
173
|
+
namespace executors
|
|
174
|
+
{
|
|
175
|
+
void HelloExecutor::doGet(framework::HTTPRequest& request, framework::HTTPResponse& response)
|
|
176
|
+
{
|
|
177
|
+
response.addBody(json::JSONBuilder(CP_UTF8).appendString("message", "Hello, World!"));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
DECLARE_EXECUTOR(HelloExecutor);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
More information you can find in [wiki](https://github.com/LazyPanda07/WebFramework/wiki/Executors).
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
### Hello executor
|
|
187
|
+
* Links
|
|
188
|
+
* [Windows](https://github.com/LazyPanda07/WebFramework/releases/latest/download/hello_executor_windows.zip)
|
|
189
|
+
* [Linux](https://github.com/LazyPanda07/WebFramework/releases/latest/download/hello_executor_linux.zip)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
web_framework_api/WebFramework.py,sha256=Psw-khF5AL2vK8WXiCtu1eYBoYhysBYksE9GryllDWU,5397
|
|
2
|
+
web_framework_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
web_framework_api/exceptions/WebFrameworkException.py,sha256=8U6_0kRz9FBLivaH88yP6q7kYSkRbkL3CKg5OCKvwp4,641
|
|
4
|
+
web_framework_api/utility/Config.py,sha256=1_KPFzfCvS7UE5-xCIpKyYzKrj_tIMXnp0atTzIskSM,12702
|
|
5
|
+
web_framework_api/utility/DLLHandler.py,sha256=VJJg3cjXKSVEur1j_bGta3BCvo3N-niX_wDy40WXmiQ,3196
|
|
6
|
+
web_framework_api/utility/Utils.py,sha256=UYe8KqUnldwe6w-REzqBnlS6dDUEwsLbjzoS1VBTbMQ,3073
|
|
7
|
+
web_framework_api-3.1.0.dist-info/METADATA,sha256=nEjpRFc527agv8hTLLA-32q6pDx78KlwYh3TPqlgQ6U,5365
|
|
8
|
+
web_framework_api-3.1.0.dist-info/WHEEL,sha256=gxW59xGPYrrbXjuxOwDtUfAuLQzXH6BJZostV-5Lvqc,106
|
|
9
|
+
web_framework_api-3.1.0.dist-info/licenses/license/LICENSE,sha256=2qWVL8xV3683inNKbRMFVlYfedj3s4dGzhpZuPH51kA,1073
|
|
10
|
+
web_framework_api-3.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Semyon Gritsenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|