-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_robot.py
More file actions
34 lines (27 loc) · 882 Bytes
/
flask_robot.py
File metadata and controls
34 lines (27 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from robot import Robot
from flask import current_app
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
class FlaskRobot(object):
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
def teardown(self, exception):
if exception is not None:
if hasattr(self, 'robot'):
self.robot.close()
def new_robot(self):
return Robot(current_app.config['ROBOT_PORT'])
@property
def instance(self):
if not hasattr(self, 'robot'):
self.robot = self.new_robot()
return self.robot