Welcome, guest | Sign In | My Account | Store | Cart
"""docgiver module"""

import types
import modulehacker

class DocGiver(modulehacker.Hacker):
    def fix_classdoc(self, cls):
        if cls.__doc__:
            return
        for base in cls.__mro__[1:]:
            if base.__doc__:
                cls.__doc__ = base.__doc__
                break

    def fix_methoddoc(self, cls):
        for attr in dir(cls):
            method = getattr(cls, attr)
            if not isinstance(method, types.FunctionType):
                continue
            if method.__doc__:
                continue
            for base in cls.__mro__[1:]:
                if attr not in dir(base):
                    continue
                m = getattr(base, attr)
                if isinstance(m, types.FunctionType) and m.__doc__:
                    method.__doc__ = m.__doc__
                break

    def hack(self, module):
        for attr in dir(module):
            cls = getattr(module, attr)
            if isinstance(cls, type):
                #self.fix_classdoc(cls)
                self.fix_methoddoc(cls)
        return module

modulehacker.register(DocGiver())

History