Skip to content

Commit f5c42ca

Browse files
author
John Craig
committed
Add support for user-defined macros
Signed-off-by: John Craig <[email protected]>
1 parent 6d18854 commit f5c42ca

File tree

2 files changed

+68
-48
lines changed

2 files changed

+68
-48
lines changed

examples/macros/logon.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from tnz.ati import ati
22

3-
def logon(zti, arg):
3+
def do_logon(zti, arg):
44
logon_setup()
5-
65
ati.wait(lambda: ati.scrhas("Enter: "))
76
ati.send("app1 userid[enter]")
87
ati.wait(lambda: ati.scrhas("Password ===> "))
@@ -21,11 +20,11 @@ def logon(zti, arg):
2120
# disconnect from host
2221
ati.drop("SESSION")
2322

24-
def logon_helper():
23+
def logon_setup():
2524
ati.set("TRACE", "ALL")
2625
ati.set("LOGDEST", "example.log")
2726

2827
ati.set("ONERROR", "1")
2928
ati.set("DISPLAY", "HOST")
3029
ati.set("SESSION_HOST", "mvs1")
31-
ati.set("SESSION", "A")
30+
ati.set("SESSION", "A")

tnz/zti.py

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ def __init__(self, stdin=None, stdout=None):
120120
if os.getenv("ZTI_AUTOSIZE"):
121121
self.autosize = True
122122

123-
self.macros_dir = os.getenv("ZTI_MACROS_DIR")
124-
if self.macros_dir is None or not os.path.isdir(
125-
self.macros_dir):
126-
self.macros_dir = os.path.expanduser("~/.zti-macros.d")
127-
128123
self.pend_intro = None
129124
self.__dirty_ranges = []
130125
self.single_session = False
@@ -1861,45 +1856,6 @@ def help_plugin(entry=entry):
18611856

18621857
self.plugins = " ".join(plugins)
18631858

1864-
def __register_macros(self):
1865-
import importlib.util
1866-
import sys
1867-
import types
1868-
1869-
if not os.path.isdir(self.macros_dir):
1870-
return
1871-
1872-
for macro_file in os.listdir(self.macros_dir):
1873-
if not macro_file.endswith(".py"):
1874-
continue
1875-
1876-
if len(macro_file.split('.')) > 1:
1877-
continue
1878-
1879-
macro_name = macro_file.split('.')[0]
1880-
1881-
# Ignore macros which already exist
1882-
if f"do_{macro_name}" in self.get_names():
1883-
continue
1884-
1885-
macro_path = os.path.join(self.macros_dir, macro_file)
1886-
1887-
# Import the user macro as a module
1888-
macro_spec = importlib.util.spec_from_file_location(
1889-
f"module.{macro_name}", macro_path)
1890-
macro_module = importlib.util.module_from_spec(macro_spec)
1891-
sys.modules[f"module.{macro_name}"] = macro_module
1892-
macro_spec.loader.exec_module(macro_module)
1893-
1894-
# Find the function
1895-
if hasattr(macro_module, macro_name):
1896-
# Create a new bound method for the `Zti` class for this
1897-
# function
1898-
setattr(Zti, f"do_{macro_name}",
1899-
types.MethodType(
1900-
getattr(macro_module, macro_name),
1901-
self))
1902-
19031859
def __key_data(self, tns, data):
19041860
try:
19051861
tns.key_data(data, zti=self)
@@ -2751,6 +2707,71 @@ def __refresh(self):
27512707

27522708
self.stdscr.refresh(_win_callback=self.__set_event_fn())
27532709

2710+
def __register_macros(self):
2711+
import importlib.util
2712+
import sys
2713+
import types
2714+
2715+
macros_dir = os.getenv("ZTI_MACROS_DIR")
2716+
if macros_dir is None:
2717+
macros_dir = os.path.expanduser("~/.zti-mac")
2718+
2719+
if not os.path.isdir(macros_dir):
2720+
_logger.exception(f"{macros_dir} is not a directory")
2721+
return
2722+
2723+
for macro_file in os.listdir(macros_dir):
2724+
if not macro_file.endswith(".py"):
2725+
continue
2726+
2727+
if len(macro_file.split('.')) > 1:
2728+
continue
2729+
2730+
macro_name = macro_file.split('.')[0]
2731+
2732+
# Ignore macros with uppercase letters or
2733+
# spaces
2734+
uppercase = [l for l in macro_name if l.isupper()]
2735+
if ' ' in macro_name or len(uppercase) != 0:
2736+
continue
2737+
2738+
# Ignore macros which already exist
2739+
if f"do_{macro_name}" in self.get_names():
2740+
_logger.warning(f"Function with name do_{macro_name} already exists, macro registration failed")
2741+
continue
2742+
2743+
macro_path = os.path.join(macros_dir, macro_file)
2744+
2745+
# Import the user macro as a module
2746+
macro_spec = importlib.util.spec_from_file_location(
2747+
f"module.{macro_name}", macro_path)
2748+
macro_module = importlib.util.module_from_spec(macro_spec)
2749+
sys.modules[f"module.{macro_name}"] = macro_module
2750+
macro_spec.loader.exec_module(macro_module)
2751+
2752+
# Find the function
2753+
if hasattr(macro_module, f"do_{macro_name}"):
2754+
def do_macro(zti, arg):
2755+
self.__bg_wait_end()
2756+
macro_func = getattr(macro_module, f"do_{macro_name}")
2757+
macro_func(zti, arg)
2758+
2759+
# Create a new bound method for the `Zti` class for this
2760+
# function
2761+
setattr(Zti, f"do_{macro_name}",
2762+
types.MethodType(
2763+
do_macro,
2764+
self))
2765+
2766+
# Check if a corresponding help function exists
2767+
if hasattr(macro_module, f"help_{macro_name}"):
2768+
# Create a new bound method for the `Zti` class for this
2769+
# function
2770+
setattr(Zti, f"help_{macro_name}",
2771+
types.MethodType(
2772+
getattr(macro_module, f"help_{macro_name}"),
2773+
self))
2774+
27542775
def __scale_size(self, maxrow, maxcol):
27552776
arows, acols = self.autosize
27562777
aspect1 = arows / acols

0 commit comments

Comments
 (0)