Package rivescript :: Module python
[hide private]
[frames] | no frames]

Source Code for Module rivescript.python

 1  #!/usr/bin/env python 
 2   
 3  # The MIT License (MIT) 
 4  # 
 5  # Copyright (c) 2014 Noah Petherbridge 
 6  # 
 7  # Permission is hereby granted, free of charge, to any person obtaining a copy 
 8  # of this software and associated documentation files (the "Software"), to deal 
 9  # in the Software without restriction, including without limitation the rights 
10  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
11  # copies of the Software, and to permit persons to whom the Software is 
12  # furnished to do so, subject to the following conditions: 
13  # 
14  # The above copyright notice and this permission notice shall be included in all 
15  # copies or substantial portions of the Software. 
16  # 
17  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
18  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
19  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
20  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
21  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
22  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
23  # SOFTWARE. 
24   
25  # Python3 compat 
26  from __future__ import print_function 
27   
28  __docformat__ = 'plaintext' 
29   
30   
31 -class PyRiveObjects:
32 """A RiveScript object handler for Python code. 33 34 This class provides built-in support for your RiveScript documents to include 35 and execute object macros written in Python. For example: 36 37 > object base64 python 38 import base64 as b64 39 return b64.b64encode(" ".join(args)) 40 < object 41 42 + encode * in base64 43 - OK: <call>base64 <star></call> 44 45 Python object macros receive these two parameters: 46 47 rs: The reference to the parent RiveScript instance 48 args: A list of argument words passed to your object macro 49 50 Python support is on by default. To turn it off, just unset the Python language 51 handler on your RiveScript object: 52 53 rs.set_handler("python", None)""" 54 _objects = {} # The cache of objects loaded 55
56 - def __init__(self):
57 pass
58
59 - def load(self, name, code):
60 """Prepare a Python code object given by the RiveScript interpreter.""" 61 # We need to make a dynamic Python method. 62 source = "def RSOBJ(rs, args):\n" 63 for line in code: 64 source = source + "\t" + line + "\n" 65 66 source += "self._objects[name] = RSOBJ\n" 67 68 try: 69 exec(source) 70 #self._objects[name] = RSOBJ 71 except Exception as e: 72 print("Failed to load code from object", name) 73 print("The error given was: ", e)
74
75 - def call(self, rs, name, user, fields):
76 """Invoke a previously loaded object.""" 77 # Call the dynamic method. 78 if not name in self._objects: 79 return '[ERR: Object Not Found]' 80 func = self._objects[name] 81 reply = '' 82 try: 83 reply = func(rs, fields) 84 if reply is None: 85 reply = '' 86 except Exception as e: 87 print("Error executing Python object:", e) 88 reply = '[ERR: Error when executing Python object]' 89 return str(reply)
90