File size: 1,908 Bytes
2419253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""install the Converse hook into the pack's workbench/server.py (upper layer of the overlay; the lower file is untouched).
Workbench.chat -> chat_core (the head + your readers, unchanged); the new chat() calls chat_core first and, when the head did
not answer with an answer, lets the turn loop compose. ORACLE7_CONVERSE=0 turns the hook off (the measurement harness uses it).
usage: patch_server.py <lower server.py> <upper server.py>"""
import ast
import sys

src, dst = sys.argv[1:3]
s = open(src, encoding="utf-8").read()
old = "    def chat(self, text, sid):\n        h = help_reply(text)\n"
assert s.count(old) == 1, "Workbench.chat not found once"
new = '''    def chat(self, text, sid):
        """11.5: the head first (chat_core); where it did not answer with an answer, the turn loop composes (converse)"""
        r = self.chat_core(text, sid)
        if os.environ.get("ORACLE7_CONVERSE", "1") == "0":
            return r
        cv = self._converse()
        if cv is None or not cv.ready() or cv.stands(r, text):
            return r
        return cv.reply(text, sid, r)

    def _converse(self):
        if getattr(self, "_cv", None) is None:
            try:
                import os as _os, sys as _sys
                _sys.path.insert(0, _os.path.join(_os.path.dirname(_os.path.abspath(__file__)), "converse"))
                from converse.session import Converse
                self._cv = Converse(self.eng, self)
            except Exception as e:  # noqa: BLE001
                self._cv = False
                self._cv_error = repr(e)[:300]
        return self._cv or None

    def chat_core(self, text, sid):
        h = help_reply(text)
'''
s = s.replace(old, new)
if "\nimport os\n" not in s:
    s = s.replace("\nimport json\n", "\nimport json\nimport os\n", 1)
ast.parse(s)
open(dst, "w", encoding="utf-8").write(s)
print("server.py patched ->", dst)