forked from doloopwhile/PyExecJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_abstract_runtime_context.py
More file actions
53 lines (42 loc) · 1.38 KB
/
_abstract_runtime_context.py
File metadata and controls
53 lines (42 loc) · 1.38 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import execjs
from abc import ABCMeta, abstractmethod
import six
@six.add_metaclass(ABCMeta)
class AbstractRuntimeContext(object):
'''
Abstract base class for runtime context class.
'''
def exec_(self, source):
'''Execute source by JavaScript runtime and return all output to stdout as a string.
source -- JavaScript code to execute.
'''
if not self.is_available():
raise execjs.RuntimeUnavailableError
return self._exec_(source)
def eval(self, source):
'''Evaluate source in JavaScript runtime.
source -- JavaScript code to evaluate.
'''
if not self.is_available():
raise execjs.RuntimeUnavailableError
return self._eval(source)
def call(self, name, *args):
'''Call a JavaScript function in context.
name -- Name of funtion object to call
args -- Arguments for the funtion object
'''
if not self.is_available():
raise execjs.RuntimeUnavailableError
return self._call(name, *args)
@abstractmethod
def is_available(self):
raise NotImplementedError
@abstractmethod
def _exec_(self, source):
raise NotImplementedError
@abstractmethod
def _eval(self, source):
raise NotImplementedError
@abstractmethod
def _call(self, name, *args):
raise NotImplementedError