python_module_tools
Tools for working with python modules and classes.
classes_from_module(superclass, module)
Return plug-ins from module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
superclass | superclass | Superclass of subclasses to look for | required |
module | ModuleType | Imported module where to look for 'superclass' subclasses. | required |
Returns:
Type | Description |
---|---|
List of plug-ins, or empty list if none is found. |
Source code in client/ayon_core/lib/python_module_tools.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
import_filepath(filepath, module_name=None, sys_module_name=None)
Import python file as python module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath | str | Path to python file. | required |
module_name | str | Name of loaded module. Only for Python 3. By default is filled with filename of filepath. | None |
sys_module_name | str | Name of module in | None |
Todo (antirotor): We should add the module to the sys.modules always but we need to be careful about it and test it properly.
Source code in client/ayon_core/lib/python_module_tools.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
import_module_from_dirpath(dirpath, folder_name, dst_module_name=None)
Import passed directory as a python module.
Imported module can be assigned as a child attribute of already loaded module from sys.modules
if has support of setattr
. That is not default behavior of python modules so parent module must be a custom module with that ability.
It is not possible to reimport already cached module. If you need to reimport module you have to remove it from caches manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dirpath | str | Parent directory path of loaded folder. | required |
folder_name | str | Folder name which should be imported inside passed directory. | required |
dst_module_name | str | Parent module name under which can be loaded module added. | None |
Source code in client/ayon_core/lib/python_module_tools.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
is_func_signature_supported(func, *args, **kwargs)
Check if a function signature supports passed args and kwargs.
This check does not actually call the function, just look if function can be called with the arguments.
Notes
This does NOT check if the function would work with passed arguments only if they can be passed in. If function have args, *kwargs in parameters, this will always return 'True'.
Example
def my_function(my_number): ... return my_number + 1 ... is_func_signature_supported(my_function, 1) True is_func_signature_supported(my_function, 1, 2) False is_func_signature_supported(my_function, my_number=1) True is_func_signature_supported(my_function, number=1) False is_func_signature_supported(my_function, "string") True def my_other_function(args, kwargs): ... my_function(args, **kwargs) ... is_func_signature_supported( ... my_other_function, ... "string", ... 1, ... other=None ... ) True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func | Callable | A function where the signature should be tested. | required |
*args | Any | Positional arguments for function signature. | () |
**kwargs | Any | Keyword arguments for function signature. | {} |
Returns:
Name | Type | Description |
---|---|---|
bool | Function can pass in arguments. |
Source code in client/ayon_core/lib/python_module_tools.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
modules_from_path(folder_path)
Get python scripts as modules from a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | Path to folder containing python scripts. | required |
Returns:
Type | Description |
---|---|
tuple |
Source code in client/ayon_core/lib/python_module_tools.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
recursive_bases_from_class(klass)
Extract all bases from entered class.
Source code in client/ayon_core/lib/python_module_tools.py
107 108 109 110 111 112 113 114 |
|