Skip to content

Commit 92b7ec8

Browse files
author
John Craig
committed
Code review changes
1 parent 05eef0d commit 92b7ec8

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

docs/macros.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22
title: User Macros
33
---
44

5-
Users may define macros for `zti`. When defined, user macros appear in the list of available commands for `zti`. User macros are created by placing Python files in the ZTI macros directory, which defaults to the path `~/.zti-macros.d`. This path may be overridden with the `ZTI_MACROS_DIR` environment variables.
5+
Users may define macros for `zti`. When defined, user macros appear in the list of available commands for `zti`. User macros are created by placing Python files in the ZTI macros directory, which defaults to the path `~/.zti-mac`. This path may be overridden with the `ZTI_MACROS_DIR` environment variables.
66

77
User macro file names must take the form `my_macro.py`, and must contain a function with the signature,
88

99
```python
10-
def my_macro(zti, arg):
10+
def do_my_macro(zti, arg):
1111
...
1212
```
1313

1414
This function is invoked when the macro command is run from `zti`. It is passed the current `zti` instance as the first argument and any arguments to the command as the second argument.
1515

16+
The macro file may optionally also contain a function with the signature,
17+
18+
```python
19+
def help_my_macro(zti):
20+
...
21+
```
22+
23+
This function will be registered as the help command for the main macro command.
24+
1625
See `examples/macros/logon.py` for an example of a user macro.
1726

1827
A macro file name may not contain more than one period character. User macros which conflict with existing ZTI commands are ignored.

tnz/zti.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
ZTI_AUTOSIZE
3030
ZTI_SECLEVEL (see tnz.py)
3131
ZTI_TITLE
32-
ZTI_MACROS_DIR (~/.zti-macros.d is default)
32+
ZTI_MACROS_DIR (~/.zti-mac is default)
3333
_BPX_TERMPATH (see _termlib.py)
3434
3535
Copyright 2021, 2024 IBM Inc. All Rights Reserved.
@@ -119,7 +119,7 @@ def __init__(self, stdin=None, stdout=None):
119119
self.macros_dir = os.getenv("ZTI_MACROS_DIR")
120120
if self.macros_dir is None or not os.path.isdir(
121121
self.macros_dir):
122-
self.macros_dir = os.path.expanduser("~/.zti-macros.d")
122+
self.macros_dir = os.path.expanduser("~/.zti-mac")
123123

124124
self.pend_intro = None
125125
self.__dirty_ranges = []
@@ -1846,45 +1846,6 @@ def help_plugin(entry=entry):
18461846

18471847
self.plugins = " ".join(plugins)
18481848

1849-
def __register_macros(self):
1850-
import importlib.util
1851-
import sys
1852-
import types
1853-
1854-
if not os.path.isdir(self.macros_dir):
1855-
return
1856-
1857-
for macro_file in os.listdir(self.macros_dir):
1858-
if not macro_file.endswith(".py"):
1859-
continue
1860-
1861-
if len(macro_file.split('.')) > 1:
1862-
continue
1863-
1864-
macro_name = macro_file.split('.')[0]
1865-
1866-
# Ignore macros which already exist
1867-
if f"do_{macro_name}" in self.get_names():
1868-
continue
1869-
1870-
macro_path = os.path.join(self.macros_dir, macro_file)
1871-
1872-
# Import the user macro as a module
1873-
macro_spec = importlib.util.spec_from_file_location(
1874-
f"module.{macro_name}", macro_path)
1875-
macro_module = importlib.util.module_from_spec(macro_spec)
1876-
sys.modules[f"module.{macro_name}"] = macro_module
1877-
macro_spec.loader.exec_module(macro_module)
1878-
1879-
# Find the function
1880-
if hasattr(macro_module, macro_name):
1881-
# Create a new bound method for the `Zti` class for this
1882-
# function
1883-
setattr(Zti, f"do_{macro_name}",
1884-
types.MethodType(
1885-
getattr(macro_module, macro_name),
1886-
self))
1887-
18881849
def __key_data(self, tns, data):
18891850
try:
18901851
tns.key_data(data, zti=self)
@@ -2741,6 +2702,55 @@ def __refresh(self):
27412702

27422703
self.stdscr.refresh(_win_callback=self.__set_event_fn())
27432704

2705+
def __register_macros(self):
2706+
import importlib.util
2707+
import sys
2708+
import types
2709+
2710+
if not os.path.isdir(self.macros_dir):
2711+
_logger.exception(f"{self.macros_dir} is not a directory")
2712+
return
2713+
2714+
for macro_file in os.listdir(self.macros_dir):
2715+
if not macro_file.endswith(".py"):
2716+
continue
2717+
2718+
if len(macro_file.split('.')) > 1:
2719+
continue
2720+
2721+
macro_name = macro_file.split('.')[0]
2722+
2723+
# Ignore macros which already exist
2724+
if f"do_{macro_name}" in self.get_names():
2725+
continue
2726+
2727+
macro_path = os.path.join(self.macros_dir, macro_file)
2728+
2729+
# Import the user macro as a module
2730+
macro_spec = importlib.util.spec_from_file_location(
2731+
f"module.{macro_name}", macro_path)
2732+
macro_module = importlib.util.module_from_spec(macro_spec)
2733+
sys.modules[f"module.{macro_name}"] = macro_module
2734+
macro_spec.loader.exec_module(macro_module)
2735+
2736+
# Find the function
2737+
if hasattr(macro_module, f"do_{macro_name}"):
2738+
# Create a new bound method for the `Zti` class for this
2739+
# function
2740+
setattr(Zti, f"do_{macro_name}",
2741+
types.MethodType(
2742+
getattr(macro_module, f"do_{macro_name}"),
2743+
self))
2744+
2745+
# Check if a corresponding help function exists
2746+
if hasattr(macro_module, f"help_{macro_name}"):
2747+
# Create a new bound method for the `Zti` class for this
2748+
# function
2749+
setattr(Zti, f"help_{macro_name}",
2750+
types.MethodType(
2751+
getattr(macro_module, f"help_{macro_name}"),
2752+
self))
2753+
27442754
def __scale_size(self, maxrow, maxcol):
27452755
arows, acols = self.autosize
27462756
aspect1 = arows / acols

0 commit comments

Comments
 (0)