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 | type | Superclass of subclasses to look for | required |
module | ModuleType | Imported module where to look for 'superclass' subclasses. | required |
Returns:
| Type | Description |
|---|---|
list[type] | list[type]: List of plug-ins, or empty list if none is found. |
Source code in client/ayon_core/lib/python_module_tools.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
get_dynamic_import_module_name(dirpath, filename=None)
Get hash of directory path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dirpath | Path | str | Directory path to hash. | required |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | Hash of directory path. |
Source code in client/ayon_core/lib/python_module_tools.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
import_filepath(filepath, module_name=None, sys_module_name=None)
Import python file as python module.
It is recommended to pass in only 'filepath' and let function generate module names automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath | str | Path | 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 |
Source code in client/ayon_core/lib/python_module_tools.py
46 47 48 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 | |
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
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
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
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
modules_from_path(path)
Get python scripts as modules from a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | Path | Path to folder containing python scripts or path to a python script. | required |
Returns:
| Name | Type | Description |
|---|---|---|
ModulesResult | ModulesResult | Contains successfully imported modules and information about paths that failed to import. |
Source code in client/ayon_core/lib/python_module_tools.py
193 194 195 196 197 198 199 200 201 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 | |
recursive_bases_from_class(klass)
Extract all bases from entered class.
Source code in client/ayon_core/lib/python_module_tools.py
255 256 257 258 259 260 261 262 | |