|
29 | 29 | ZTI_AUTOSIZE |
30 | 30 | ZTI_SECLEVEL (see tnz.py) |
31 | 31 | ZTI_TITLE |
32 | | - ZTI_MACROS_DIR (~/.zti-macros.d is default) |
| 32 | + ZTI_MACROS_DIR (~/.zti-mac is default) |
33 | 33 | _BPX_TERMPATH (see _termlib.py) |
34 | 34 |
|
35 | 35 | Copyright 2021, 2024 IBM Inc. All Rights Reserved. |
@@ -119,7 +119,7 @@ def __init__(self, stdin=None, stdout=None): |
119 | 119 | self.macros_dir = os.getenv("ZTI_MACROS_DIR") |
120 | 120 | if self.macros_dir is None or not os.path.isdir( |
121 | 121 | self.macros_dir): |
122 | | - self.macros_dir = os.path.expanduser("~/.zti-macros.d") |
| 122 | + self.macros_dir = os.path.expanduser("~/.zti-mac") |
123 | 123 |
|
124 | 124 | self.pend_intro = None |
125 | 125 | self.__dirty_ranges = [] |
@@ -1846,45 +1846,6 @@ def help_plugin(entry=entry): |
1846 | 1846 |
|
1847 | 1847 | self.plugins = " ".join(plugins) |
1848 | 1848 |
|
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 | | - |
1888 | 1849 | def __key_data(self, tns, data): |
1889 | 1850 | try: |
1890 | 1851 | tns.key_data(data, zti=self) |
@@ -2741,6 +2702,55 @@ def __refresh(self): |
2741 | 2702 |
|
2742 | 2703 | self.stdscr.refresh(_win_callback=self.__set_event_fn()) |
2743 | 2704 |
|
| 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 | + |
2744 | 2754 | def __scale_size(self, maxrow, maxcol): |
2745 | 2755 | arows, acols = self.autosize |
2746 | 2756 | aspect1 = arows / acols |
|
0 commit comments