-
Notifications
You must be signed in to change notification settings - Fork 20
Feature: User Macros #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
john-craig
wants to merge
6
commits into
IBM:main
Choose a base branch
from
john-craig:feature/user-macros
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature: User Macros #147
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ab4e78
Add support for user-defined macros
bb31a5b
Fix style
3f381d7
Merge branch 'main' into feature/user-macros
john-craig 859819b
Fix requested changes
358e4d4
Merge branch 'main' into feature/user-macros
john-craig 0fdca75
Merge branch 'main' into feature/user-macros
john-craig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| title: User Macros | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| User macro file names must take the form `my_macro.py`, and must contain a function with the signature, | ||
|
|
||
| ```python | ||
| def do_my_macro(zti, arg): | ||
| ... | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| The macro file may optionally also contain a function with the signature, | ||
|
|
||
| ```python | ||
| def help_my_macro(zti): | ||
| ... | ||
| ``` | ||
|
|
||
| This function will be registered as the help command for the main macro command. | ||
|
|
||
| See `examples/macros/logon.py` for an example of a user macro. | ||
|
|
||
| A macro file name may not contain more than one period character. User macros which conflict with existing ZTI commands are ignored. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from tnz.ati import ati | ||
|
|
||
| def do_logon(zti, arg): | ||
| logon_setup() | ||
| ati.wait(lambda: ati.scrhas("Enter: ")) | ||
| ati.send("app1 userid[enter]") | ||
| ati.wait(lambda: ati.scrhas("Password ===> ")) | ||
| ati.send("password[enter]") | ||
|
|
||
| # maybe look for a status message | ||
| ati.wait(lambda: ati.scrhas("ICH70001I")) | ||
|
|
||
| # do something useful | ||
| ati.wait(lambda: ati.scrhas("***")) | ||
| ati.send("[enter]") | ||
| ati.wait(lambda: ati.scrhas("READY")) | ||
|
najohnsn marked this conversation as resolved.
|
||
| ati.send("logon[enter]") | ||
| ati.wait(lambda: ati.scrhas("LOGGED OFF")) | ||
|
|
||
| # disconnect from host | ||
| ati.drop("SESSION") | ||
|
|
||
| def logon_setup(): | ||
| ati.set("TRACE", "ALL") | ||
| ati.set("LOGDEST", "example.log") | ||
|
|
||
| ati.set("ONERROR", "1") | ||
| ati.set("DISPLAY", "HOST") | ||
| ati.set("SESSION_HOST", "mvs1") | ||
| ati.set("SESSION", "A") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,8 @@ def __init__(self, stdin=None, stdout=None): | |
|
|
||
| self.__install_plugins() | ||
|
|
||
| self.__register_macros() | ||
|
|
||
| # Methods | ||
|
|
||
| def atexit(self): | ||
|
|
@@ -2704,6 +2706,76 @@ def __refresh(self): | |
|
|
||
| self.stdscr.refresh(_win_callback=self.__set_event_fn()) | ||
|
|
||
| def __register_macros(self): | ||
| import importlib.util | ||
| import sys | ||
| import types | ||
|
|
||
| macros_dir = os.getenv("ZTI_MACROS_DIR") | ||
| if macros_dir is None: | ||
| macros_dir = os.path.expanduser("~/.zti-mac") | ||
|
|
||
| if not os.path.isdir(macros_dir): | ||
| _logger.exception(f"{macros_dir} is not a directory") | ||
|
najohnsn marked this conversation as resolved.
Outdated
|
||
| return | ||
|
|
||
| for macro_file in os.listdir(macros_dir): | ||
| if not macro_file.endswith(".py"): | ||
| continue | ||
|
|
||
| if len(macro_file.split('.')) > 1: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest the use of |
||
| continue | ||
|
|
||
| macro_name = macro_file.split('.')[0] | ||
|
najohnsn marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Ignore macros with uppercase letters or | ||
| # spaces | ||
| uppercase = [letter for letter in | ||
| macro_name if letter.isupper()] | ||
| if ' ' in macro_name or len(uppercase) != 0: | ||
|
najohnsn marked this conversation as resolved.
Outdated
|
||
| continue | ||
|
|
||
| # Ignore macros which already exist | ||
| if f"do_{macro_name}" in self.get_names(): | ||
|
najohnsn marked this conversation as resolved.
|
||
| _logger.warning(f"Function with name do_{macro_name}" | ||
| " already exists, macro registration" | ||
| " failed") | ||
| continue | ||
|
|
||
| macro_path = os.path.join(macros_dir, macro_file) | ||
|
|
||
| # Import the user macro as a module | ||
| macro_spec = importlib.util.spec_from_file_location( | ||
| f"module.{macro_name}", macro_path) | ||
| macro_module = importlib.util.module_from_spec(macro_spec) | ||
| sys.modules[f"module.{macro_name}"] = macro_module | ||
| macro_spec.loader.exec_module(macro_module) | ||
|
|
||
| # Find the function | ||
| if hasattr(macro_module, f"do_{macro_name}"): | ||
| def do_macro(zti, arg): | ||
| self.__bg_wait_end() | ||
| macro_func = getattr(macro_module, | ||
| f"do_{macro_name}") | ||
| macro_func(zti, arg) | ||
|
|
||
| # Create a new bound method for the `Zti` class for this | ||
| # function | ||
| setattr(Zti, f"do_{macro_name}", | ||
| types.MethodType( | ||
| do_macro, | ||
| self)) | ||
|
|
||
| # Check if a corresponding help function exists | ||
| if hasattr(macro_module, f"help_{macro_name}"): | ||
| # Create a new bound method for the `Zti` class | ||
| # for this function | ||
| setattr(Zti, f"help_{macro_name}", | ||
| types.MethodType( | ||
| getattr(macro_module, | ||
| f"help_{macro_name}"), | ||
| self)) | ||
|
|
||
| def __scale_size(self, maxrow, maxcol): | ||
| arows, acols = self.autosize | ||
| aspect1 = arows / acols | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it would actually be better to do
from tnz import ati. that way the sample uses the current global ati object - which is what zti works with. whenfrom tnz.ati import atiis used, the code here is bound the the global ati object at the time of import - there are situations in which that object can change.