HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
plugin_manager.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
6  {
7  py::class_<FacExtensionInterface> py_fac_extension_interface(m, "FacExtensionInterface", R"(
8  An extension interface that lets a plugin provide a parser or writer for a file type.
9  )");
10 
11  py::enum_<FacExtensionInterface::Feature>(py_fac_extension_interface, "Feature", R"(
12  The feature that is provided by the extension.
13  )")
14  .value("FacUnknown", FacExtensionInterface::Feature::FacUnknown, R"(No known feature.)")
15  .value("FacNetlistParser", FacExtensionInterface::Feature::FacNetlistParser, R"(A netlist parser.)")
16  .value("FacNetlistWriter", FacExtensionInterface::Feature::FacNetlistWriter, R"(A netlist writer.)")
17  .value("FacGatelibParser", FacExtensionInterface::Feature::FacGatelibParser, R"(A gate library parser.)")
18  .value("FacGatelibWriter", FacExtensionInterface::Feature::FacGatelibWriter, R"(A gate library writer.)")
19  .export_values();
20 
21  py_fac_extension_interface.def("get_feature", &FacExtensionInterface::get_feature, R"(
22  Get the feature provided by the extension.
23 
24  :returns: The feature.
25  :rtype: hal_py.FacExtensionInterface.Feature
26  )");
27 
28  py_fac_extension_interface.def("get_description", &FacExtensionInterface::get_description, R"(
29  Get the description of the extension.
30 
31  :returns: The description.
32  :rtype: str
33  )");
34 
35  py_fac_extension_interface.def("get_supported_file_extensions", &FacExtensionInterface::get_supported_file_extensions, R"(
36  Get the file extensions supported by the extension.
37 
38  :returns: A list of file extensions.
39  :rtype: list[str]
40  )");
41 
42  auto py_plugin_manager = m.def_submodule("plugin_manager");
43 
44  py::class_<plugin_manager::PluginFeature> py_plugin_feature(py_plugin_manager, "PluginFeature", R"(
45  A feature provided by a plugin, such as a parser or a writer.
46  )");
47 
48  py_plugin_feature.def_readwrite("feature", &plugin_manager::PluginFeature::feature, R"(
49  The kind of feature that is provided.
50 
51  :type: hal_py.FacExtensionInterface.Feature
52  )");
53 
54  py_plugin_feature.def_readwrite("args", &plugin_manager::PluginFeature::args, R"(
55  Additional arguments of the feature, holding the supported file extensions if a parser or writer is registered.
56 
57  :type: list[str]
58  )");
59 
60  py_plugin_feature.def_readwrite("description", &plugin_manager::PluginFeature::description, R"(
61  The description of the feature.
62 
63  :type: str
64  )");
65 
66  py_plugin_manager.def("get_plugin_names", &plugin_manager::get_plugin_names, R"(
67  Get the names of all loaded plugins.
68 
69  :returns: The set of plugin names.
70  :rtype: set(str)
71  )");
72 
73  py_plugin_manager.def("load_all_plugins", &plugin_manager::load_all_plugins, py::arg("directory_names") = std::vector<std::filesystem::path>(), R"(
74  Load all plugins in the specified directories.
75  If ``directory_names`` is empty, the default directories will be searched.
76 
77  :param list[pathlib.Path] directory_names: A list of directory paths.
78  :returns: ``True`` on success, ``False`` otherwise.
79  :rtype: bool
80  )");
81 
82  py_plugin_manager.def("load", &plugin_manager::load, py::arg("plugin_name"), py::arg("file_path"), R"(
83  Load a single plugin by specifying its name and file path.
84 
85  :param str plugin_name: The desired name that is unique in the framework.
86  :param pathlib.Path file_path: The path to the plugin file.
87  :returns: ``True`` on success, ``False`` otherwise.
88  :rtype: bool
89  )");
90 
91  py_plugin_manager.def("unload_all_plugins", &plugin_manager::unload_all_plugins, R"(
92  Releases all plugins and their associated resources.
93 
94  :returns: ``True`` on success, ``False`` otherwise.
95  :rtype: bool
96  )");
97 
98  py_plugin_manager.def("unload", &plugin_manager::unload, py::arg("plugin_name"), R"(
99  Releases a single plugin and its associated ressources.
100 
101  :param str plugin_name: The name of the plugin to unload.
102  :returns: ``True`` on success, ``False`` otherwise.
103  :rtype: bool
104  )");
105 
106  py_plugin_manager.def("get_cli_plugin_flags", &plugin_manager::get_cli_plugin_flags, R"(
107  Get a mapping of flags pointing to their corresponding CLI plugin.
108 
109  :returns: A dict from flag to plugin name.
110  :rtype: dict[str,str]
111  )");
112 
113  py_plugin_manager.def("get_ui_plugin_flags", &plugin_manager::get_ui_plugin_flags, R"(
114  Get a mapping of flags pointing to their corresponding UI plugin.
115 
116  :returns: A dict from flag to plugin name.
117  :rtype: dict[str,str]
118  )");
119 
120  py_plugin_manager.def(
121  "get_plugin_instance",
122  [](const std::string& plugin_name, bool initialize, bool silent) -> BasePluginInterface* { return plugin_manager::get_plugin_instance(plugin_name, initialize, silent); },
123  py::arg("plugin_name"),
124  py::arg("initialize") = true,
125  py::arg("silent") = false,
126  R"(
127  Gets the interface for a plugin specified by name.
128  By default calls the initialize() function of the plugin.
129  The returned object is of the plugin's own type as soon as the Python module of that plugin has been imported, and of type ``hal_py.BasePluginInterface`` otherwise.
130 
131  :param str plugin_name: The name of the plugin.
132  :param bool initialize: Set to ``False`` to not call the initialize function of the plugin. Defaults to ``True``.
133  :param bool silent: Set to ``True`` to omit the error message if the plugin is not found. Defaults to ``False``.
134  :returns: The plugin interface on success, ``None`` otherwise.
135  :rtype: hal_py.BasePluginInterface or None
136  )");
137 
138  py_plugin_manager.def("add_model_changed_callback", &plugin_manager::add_model_changed_callback, py::arg("callback"), R"(
139  Add a callback to notify the GUI about loaded or unloaded plugins.
140  The callback takes a bool that is ``True`` on load and ``False`` on unload, the plugin name, and the plugin path.
141 
142  :param callback: The callback function.
143  :type callback: lambda(bool, str, str) -> None
144  :returns: The ID of the registered callback.
145  :rtype: int
146  )");
147 
148  py_plugin_manager.def("remove_model_changed_callback", &plugin_manager::remove_model_changed_callback, py::arg("id"), R"(
149  Remove a registered callback.
150 
151  :param int id: The ID of the registered callback.
152  )");
153 
154  py_plugin_manager.def("add_existing_options_description", &plugin_manager::add_existing_options_description, py::arg("existing_options"), R"(
155  Register existing program options to avoid reuse by plugins.
156 
157  :param hal_py.ProgramOptions existing_options: The program options.
158  )");
159 
160  py_plugin_manager.def("get_cli_plugin_options", &plugin_manager::get_cli_plugin_options, R"(
161  Get the command line interface options of all plugins.
162 
163  :returns: The program options.
164  :rtype: hal_py.ProgramOptions
165  )");
166 
167  py_plugin_manager.def("get_plugin_path", &plugin_manager::get_plugin_path, py::arg("plugin_name"), R"(
168  Get the full path of a plugin.
169  On macOS, several possible file extensions are probed.
170 
171  :param str plugin_name: The name of the plugin.
172  :returns: The full path to the plugin within the HAL build directory.
173  :rtype: pathlib.Path
174  )");
175 
176  py_plugin_manager.def("has_valid_file_extension", &plugin_manager::has_valid_file_extension, py::arg("file_name"), R"(
177  Check whether a file has an extension that is legal for a plugin on the current operating system.
178 
179  :param pathlib.Path file_name: The path to the file.
180  :returns: ``False`` if the extension indicates that the file cannot be a plugin, ``True`` otherwise.
181  :rtype: bool
182  )");
183 
184  py_plugin_manager.def("get_plugin_features", &plugin_manager::get_plugin_features, py::arg("name"), R"(
185  Get the features of a plugin identified by the stem of its file name.
186 
187  :param str name: The stem of the file name of the plugin.
188  :returns: A list of features such as parsers or writers.
189  :rtype: list[hal_py.plugin_manager.PluginFeature]
190  )");
191  }
192 } // namespace hal
std::vector< std::string > get_supported_file_extensions() const
void plugin_manager_init(py::module &m)
const Module * module(const Gate *g, const NodeBoxes &boxes)
void add_existing_options_description(const ProgramOptions &existing_options)
u64 add_model_changed_callback(std::function< void(bool, std::string const &, std::string const &)> callback)
bool has_valid_file_extension(std::filesystem::path file_name)
bool unload(const std::string &plugin_name)
bool load_all_plugins(const std::vector< std::filesystem::path > &directory_names={})
bool load(const std::string &plugin_name, const std::filesystem::path &file_path=std::filesystem::path())
std::set< std::string > get_plugin_names()
std::unordered_map< std::string, std::string > get_cli_plugin_flags()
std::unordered_map< std::string, std::string > get_ui_plugin_flags()
std::filesystem::path get_plugin_path(std::string plugin_name)
ProgramOptions get_cli_plugin_options()
void remove_model_changed_callback(u64 id)
BasePluginInterface * get_plugin_instance(const std::string &plugin_name, bool initialize=true, bool silent=false)
std::vector< PluginFeature > get_plugin_features(std::string name)
Definition: defines.h:45
void initialize()
Definition: event_log.cpp:302
std::vector< std::string > args
FacExtensionInterface::Feature feature