89 lines
4.6 KiB
Python
89 lines
4.6 KiB
Python
# uncompyle6 version 3.9.2
|
|
# Python bytecode version base 3.7.0 (3394)
|
|
# Decompiled from: Python 3.8.19 (default, Mar 20 2024, 15:27:52)
|
|
# [Clang 14.0.6 ]
|
|
# Embedded file name: /var/user/app/device_supervisorbak/device_supervisor/lib/azure/iot/device/patch.py
|
|
# Compiled at: 2024-04-18 03:12:58
|
|
# Size of source mod 2**32: 7347 bytes
|
|
"""This module provides patches used to dynamically modify items from the libraries"""
|
|
import sys, inspect, logging
|
|
logger = logging.getLogger(__name__)
|
|
shim_scope = {}
|
|
|
|
def add_shims_for_inherited_methods(target_class):
|
|
"""Dynamically add overriding, pass-through shim methods for all public inherited methods
|
|
on a child class, which simply call into the parent class implementation of the same method.
|
|
|
|
These shim methods will include the same docstrings as the method from the parent class.
|
|
|
|
This currently only works for Python 3.5+
|
|
|
|
Using DEBUG logging will allow you to see output of all dynamic operations that occur within
|
|
for debugging purposes.
|
|
|
|
:param target_class: The child class to add shim methods to
|
|
"""
|
|
class_functions = inspect.getmembers(target_class, predicate=(inspect.isfunction))
|
|
class_methods = inspect.getmembers(target_class, predicate=(inspect.ismethod))
|
|
all_methods = class_functions + class_methods
|
|
class_attributes = inspect.classify_class_attrs(target_class)
|
|
classname_alias = target_class.__name__
|
|
while classname_alias in shim_scope:
|
|
classname_alias += "_"
|
|
|
|
class_module = inspect.getmodule(target_class)
|
|
import_cmdstr = "from {module} import {target_class} as {alias}".format(module=(class_module.__name__),
|
|
target_class=(target_class.__name__),
|
|
alias=classname_alias)
|
|
logger.debug("exec: " + import_cmdstr)
|
|
for method in all_methods:
|
|
method_name = method[0]
|
|
method_obj = method[1]
|
|
method_attribute = [att for att in class_attributes if att.name == method_name][0]
|
|
originating_class_obj = method_attribute.defining_class
|
|
if method_name[0] != "_":
|
|
if originating_class_obj != target_class:
|
|
method_sig = inspect.signature(method_obj)
|
|
sig_params = method_sig.parameters
|
|
if inspect.ismethod(method_obj):
|
|
complete_params = []
|
|
complete_params.append(inspect.Parameter("cls", inspect.Parameter.POSITIONAL_OR_KEYWORD))
|
|
complete_params += list(sig_params.values())
|
|
method_sig = method_sig.replace(parameters=complete_params)
|
|
else:
|
|
invoke_params_list = []
|
|
for param in sig_params.values():
|
|
if param.name != "self" and param.name != "cls":
|
|
new_param = param.replace(default=(inspect.Parameter.empty))
|
|
invoke_params_list.append(new_param)
|
|
|
|
invoke_params = method_sig.replace(parameters=invoke_params_list)
|
|
if inspect.ismethod(method_obj):
|
|
obj_or_type = "cls"
|
|
else:
|
|
obj_or_type = "self"
|
|
if inspect.iscoroutine(method_obj) or inspect.iscoroutinefunction(method_obj):
|
|
def_syntax = "async def"
|
|
ret_syntax = "return await"
|
|
else:
|
|
def_syntax = "def"
|
|
ret_syntax = "return"
|
|
fn_def_cmdstr = "{def_syntax} {method_name}{signature}: {ret_syntax} super({leaf_class}, {object_or_type}).{method_name}{invocation}".format(def_syntax=def_syntax,
|
|
method_name=method_name,
|
|
signature=(str(method_sig)),
|
|
ret_syntax=ret_syntax,
|
|
leaf_class=classname_alias,
|
|
object_or_type=obj_or_type,
|
|
invocation=(str(invoke_params)))
|
|
logger.debug("exec: " + fn_def_cmdstr)
|
|
set_doc_cmdstr = "{method_name}.__doc__ = {leaf_class}.{method_name}.__doc__".format(method_name=method_name,
|
|
leaf_class=classname_alias)
|
|
logger.debug("exec: " + set_doc_cmdstr)
|
|
if inspect.ismethod(method_obj):
|
|
attach_shim_cmdstr = "setattr({leaf_class}, '{method_name}', classmethod({method_name}))".format(leaf_class=classname_alias,
|
|
method_name=method_name)
|
|
else:
|
|
attach_shim_cmdstr = "setattr({leaf_class}, '{method_name}', {method_name})".format(leaf_class=classname_alias,
|
|
method_name=method_name)
|
|
logger.debug("exec: " + attach_shim_cmdstr)
|