autodoc.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:drf_tweaks 作者: ArabellaTech 项目源码 文件源码
def autodoc(base_doc="", classess=DEFAULT_CLASSESS, add_classess=None, skip_classess=None):
    def copy_method(cls, method_name, method):
        """ create facade for a method with preservation of original docstring """
        def shadow_method(self, *args, **kwargs):
            return method(self, *args, **kwargs)
        shadow_method.__doc__ = method.__doc__
        setattr(cls, method_name, shadow_method)

    def wrapped(cls):
        applies_to = set([])
        classess_to_apply = [c for c in classess if not skip_classess or c not in skip_classess]
        if add_classess:
            classess_to_apply += [c for c in add_classess if not skip_classess or c not in skip_classess]

        for autodoc_class in classess_to_apply:
            applies_to |= set(autodoc_class.applies_to)

        # Create facades for original methods - docstring of methods are immutable, so we need to change docstring of
        # functions. But without shadowing the method.__func__ will point to the same function for classes that
        # inherits them from the same parents.
        for method_name in applies_to:
            method = getattr(cls, method_name, None)
            if method:
                copy_method(cls, method_name, method)

        # update docstrings
        for autodoc_class in classess_to_apply:
            for method_name in autodoc_class.applies_to:
                method = getattr(cls, method_name, None)
                if method:
                    six.get_unbound_function(method).__doc__ = \
                        autodoc_class.update_docstring(cls, base_doc, six.get_unbound_function(method).__doc__,
                                                       method_name)
        return cls
    return wrapped
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号