def classlink(self, object, modname): """Make a link for a class.""" name, module = object.__name__, sys.modules.get(object.__module__) from java.lang import Object if (object is Object or Object in object.__bases__ or object is __builtin__.object): return pydoc.classname(object, modname) elif hasattr(module, name) and getattr(module, name) is object: return '<a href="%s.html#%s">%s</a>' % ( module.__name__, name, pydoc.classname(object, modname)) return pydoc.classname(object, modname)
def formattree(self, tree, modname, parent=None, prefix='+-- '): ''' Render in text a class tree as returned by inspect.getclasstree(). ''' result = [] for entry in tree: if type(entry) is type(()): c, bases = entry entry_details = '%s%s' % (prefix, pydoc.classname(c, modname)) if bases and bases != (parent,): parents = map(lambda c, m=modname: pydoc.classname(c, m), bases) entry_details += '(%s)' % ', '.join(parents) result.append(entry_details) elif type(entry) is type([]): result.append(self.formattree(entry, modname, c, '| +-- ')) return '\n'.join(result)
def classlink(self, object, modname): """Make a link for a class.""" name, module = object.__name__, sys.modules.get(object.__module__) if hasattr(module, name) and getattr(module, name) is object: return '<a href="%s.html#%s">%s</a>' % ( module.__name__, name, name ) return pydoc.classname(object, modname)
def classlink(self, obj, modname): """Make a link for a class.""" name, module = obj.__name__, sys.modules.get(obj.__module__) if hasattr(module, name) and getattr(module, name) is obj: return '<a href="{0}.html#{1}">{2}</a>'.format( module.__name__, name, name ) return pydoc.classname(obj, modname)
def docroutine( self, object, name=None, mod=None, cl=None ): """Produce text documentation for a function or method object.""" realname = object.__name__ name = name or realname note = '' skipdocs = 0 if inspect.ismethod( object ): imclass = object.im_class if cl: if imclass is not cl: note = ' from ' + classname( imclass, mod ) else: if object.im_self: note = ' method of %s instance' % classname( object.im_self.__class__, mod ) else: note = ' unbound %s method' % classname( imclass, mod ) object = object.im_func if name == realname: title = realname else: if ( cl and realname in cl.__dict__ and cl.__dict__[realname] is object ): skipdocs = 1 title = name + ' = ' + realname if inspect.isfunction( object ): args, varargs, varkw, defaults = inspect.getargspec( object ) argspec = inspect.formatargspec( args, varargs, varkw, defaults, formatvalue=self.formatvalue ) if realname == '<lambda>': title = 'lambda' argspec = argspec[1:-1] # remove parentheses else: argspec = '(...)' #decl = self.texttt(title + argspec + note + ":") if skipdocs: text = '\n' else: doc = getdoc( object ) or '' text= ( doc and rstrip( self.indent( doc ) ) + '\n' ) return self.funcdesc( title, argspec, text )
def _create_class_name_heading(self, clazz, name: str) -> str: name = self.__escape(name) real = self.__escape(clazz.__name__) bases = clazz.__bases__ return '%s%s' % ( ('class ' + self.__bold(real)) if name == real else (self.__bold(name) + ' = class ' + real), '(%s)' % ', '.join( map(lambda c, m=clazz.__module__: pydoc.classname(c, m), bases)) if bases else '')
def process_class_name(self, name, bases, module): """Format the class's name and bases.""" title = "## class " + self.bold(name) if bases: # get the names of each of the bases base_titles = [pydoc.classname(base, module) for base in bases] # if its not just object if len(base_titles) > 1: # append the list to the title title += "(%s)" % ", ".join(base_titles) return title
def classlink(self, object, modname): """Make a link for a class.""" if not object: return "<Interface>" import pdb pdb.set_trace() name, module = object.__name__, sys.modules.get(object.__module__) if hasattr(module, name) and getattr(module, name) is object: return '<a href="%s.html#%s">%s</a>' % (module.__name__, name, name) return pydoc.classname(object, modname)
def classlink(self, object, modname): """Make a link for a class.""" if not object: return "<Interface>" import pdb pdb.set_trace() name, module = object.__name__, sys.modules.get(object.__module__) if hasattr(module, name) and getattr(module, name) is object: return '<a href="%s.html#%s">%s</a>' % ( module.__name__, name, name ) return pydoc.classname(object, modname)
def docroutine(self, object, name=None, mod=None, cl=None): ''' Produce text documentation for a function or method object. ''' realname = object.__name__ name = name or realname note = '' skipdocs = 0 if inspect.ismethod(object): imclass = object.im_class if cl: if imclass is not cl: note = ' from ' + pydoc.classname(imclass, mod) skipdocs = 1 else: if object.im_self: note = ' method of %s instance' % classname( object.im_self.__class__, mod) else: note = ' unbound %s method' % classname(imclass,mod) object = object.im_func if name == realname: # title = self.bold(realname) title = '#### ' + realname else: if (cl and cl.__dict__.has_key(realname) and cl.__dict__[realname] is object): skipdocs = 1 # title = self.bold(name) + ' = ' + realname title = '####' + name + ' = ' + realname if inspect.isbuiltin(object): argspec = '(...)' else: args, varargs, varkw, defaults = inspect.getargspec(object) argspec = inspect.formatargspec( args, varargs, varkw, defaults, formatvalue=self.formatvalue) if realname == '<lambda>': title = 'lambda' argspec = argspec[1:-1] # remove parentheses decl = title + argspec + note if skipdocs: return decl + '\n' else: doc = pydoc.getdoc(object) or '' return decl + '\n' + '*%s*\n\n' % (doc and self.indent(doc).rstrip())
def docclass(self, object, name=None, mod=None, *ignored): """Produce Markdown documentation for a given class object.""" realname = object.__name__ name = name or realname bases = object.__bases__ def makename(c, m=object.__module__): return pydoc.classname(c, m) if name == realname: title = 'class ' + realname else: title = name + ' = class ' + realname if bases: parents = map(makename, bases) title = title + '(%s)' % ', '.join(parents) title = self.header(title, self.hlevel, name, 'class') self.hlevel += 1 doc = pydoc.getdoc(object) contents = doc and [doc + '\n'] or [] push = contents.append # List the mro, if non-trivial. mro = pydoc.deque(inspect.getmro(object)) if len(mro) > 2: push("Method resolution order:\n") for base in mro: push('* ' + makename(base)) push('') # Cute little class to pump out a horizontal rule between sections. class HorizontalRule: def __init__(self): self.needone = 0 def maybe(self): if self.needone: push('-' * 70 + '\n') self.needone = 1 hr = HorizontalRule() def spill(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: hr.maybe() push(msg) for name, kind, homecls, value in ok: try: value = getattr(object, name) except Exception: # Some descriptors may meet a failure in their __get__. # (bug #1785) push(self._docdescriptor(name, value, mod)) else: push(self.document(value, name, mod, object)) return attrs def spilldescriptors(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: hr.maybe() push(msg) for name, kind, homecls, value in ok: push(self._docdescriptor(name, value, mod)) return attrs def spilldata(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: hr.maybe() push(msg) for name, kind, homecls, value in ok: if (hasattr(value, '__call__') or inspect.isdatadescriptor(value)): doc = pydoc.getdoc(value) else: doc = None push( self.docother(getattr(object, name), name, mod, maxlen=70, doc=doc) + '\n') return attrs attrs = filter(lambda data: pydoc.visiblename(data[0], obj=object), pydoc.classify_class_attrs(object)) while attrs: if mro: thisclass = mro.popleft() else: thisclass = attrs[0][2] attrs, inherited = pydoc._split_list(attrs, lambda t: t[2] is thisclass) if thisclass is pydoc.__builtin__.object: attrs = inherited continue elif thisclass is object: tag = "defined here" else: tag = "inherited from %s" % pydoc.classname( thisclass, object.__module__) # Sort attrs by name. attrs.sort() # Pump out the attrs, segregated by kind. attrs = spill("Methods %s:\n" % tag, attrs, lambda t: t[1] == 'method') attrs = spill("Class methods %s:\n" % tag, attrs, lambda t: t[1] == 'class method') attrs = spill("Static methods %s:\n" % tag, attrs, lambda t: t[1] == 'static method') attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs, lambda t: t[1] == 'data descriptor') attrs = spilldata("Data and other attributes %s:\n" % tag, attrs, lambda t: t[1] == 'data') assert attrs == [] attrs = inherited self.hlevel -= 1 contents = '\n'.join(contents) if not contents: return title return title + contents.rstrip() + '\n'
def docroutine(self, object, name=None, mod=None, cl=None): """Produce text documentation for a function or method object.""" realname = object.__name__ name = name or realname note = '' skipdocs = 0 if _is_bound_method(object): imclass = object.__self__.__class__ if cl: if imclass is not cl: note = ' from ' + classname(imclass, mod) else: if object.__self__ is not None: note = ' method of %s instance' % classname( object.__self__.__class__, mod) else: note = ' unbound %s method' % classname(imclass, mod) if name == realname: title = self.bold(realname) else: if (cl and realname in cl.__dict__ and cl.__dict__[realname] is object): skipdocs = 1 title = self.bold(name) + ' = ' + realname argspec = None if inspect.isroutine(object): try: signature = inspect.signature(object) except (ValueError, TypeError): signature = None if signature: argspec = str(signature) argspec = re.sub(', /\)', ')', argspec) argspec = re.sub(', /', '', argspec) if realname == '<lambda>': title = self.bold(name) + ' lambda ' # XXX lambda's won't usually have func_annotations['return'] # since the syntax doesn't support but it is possible. # So removing parentheses isn't truly safe. argspec = argspec[1:-1] # remove parentheses if argspec: dummyargs = False else: dummyargs = True argspec = '(*args, **kwargs)' decl = 'def ' + title + argspec + ':' if dummyargs: decl += ' # unknown args #' if note: decl += f' # {note}' decl = decl.replace('# #', '#') impl = 'raise NotImplementedError()' if skipdocs: return decl + '\n' + self.indent(impl) + '\n' else: doc = getdoc(object) or '' if doc: doc = f'"""\n{doc}\n"""' return decl + '\n' + self.indent(( (doc + '\n') if doc else '') + impl).rstrip() + '\n'
def docclass(self, object, name=None, mod=None, *ignored): """Produce text documentation for a given class object.""" realname = object.__name__ name = name or realname bases = object.__bases__ def makename(c, m=object.__module__): return classname(c, m) if name == realname: title = 'class ' + self.bold(realname) else: title = self.bold(name) + ' = class ' + realname if bases: parents = map(makename, bases) title = title + '(%s)' % ', '.join(parents) title += ':' contents = [] push = contents.append doc = getdoc(object) if doc: push(f'"""\n{doc}\n"""') # List the mro, if non-trivial. mro = deque(inspect.getmro(object)) if len(mro) > 2: push("\n## Method resolution order:") for i, base in enumerate(mro, 1): push(f'# {i}) ' + makename(base)) push('') # Cute little class to pump out a horizontal rule between sections. class HorizontalRule: def __init__(self): self.needone = 0 def maybe(self): if self.needone: push('# ' + '-' * 68) self.needone = 1 hr = HorizontalRule() def spill(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: hr.maybe() push('# ' + msg) for name, kind, homecls, value in ok: try: value = getattr(object, name) except Exception: # Some descriptors may meet a failure in their __get__. # (bug #1785) push(self._docdescriptor(name, value, mod)) else: push(self.document(value, name, mod, object)) return attrs def spilldescriptors(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: hr.maybe() push('# ' + msg) for name, kind, homecls, value in ok: push(self._docdescriptor(name, value, mod)) return attrs def spilldata(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: hr.maybe() push('# ' + msg) for name, kind, homecls, value in ok: if callable(value) or inspect.isdatadescriptor(value): doc = getdoc(value) else: doc = None try: obj = getattr(object, name) except AttributeError: obj = homecls.__dict__[name] push( self.docother(obj, name, mod, maxlen=70, doc=doc) + '\n') return attrs attrs = [(name, kind, cls, value) for name, kind, cls, value in classify_class_attrs(object) if visiblename(name, obj=object)] while attrs: if mro: thisclass = mro.popleft() else: thisclass = attrs[0][2] attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) if thisclass is builtins.object: attrs = inherited continue elif thisclass is object: tag = "defined here" else: tag = "inherited from %s" % classname(thisclass, object.__module__) sort_attributes(attrs, object) # Pump out the attrs, segregated by kind. attrs = spill("Methods %s:\n" % tag, attrs, lambda t: t[1] == 'method') attrs = spill("Class methods %s:\n" % tag, attrs, lambda t: t[1] == 'class method') attrs = spill("Static methods %s:\n" % tag, attrs, lambda t: t[1] == 'static method') attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs, lambda t: t[1] == 'data descriptor') attrs = spilldata("Data and other attributes %s:\n" % tag, attrs, lambda t: t[1] == 'data') assert attrs == [] attrs = inherited contents = '\n'.join(contents) or 'pass' return '\n' + title + '\n' + self.indent(contents.rstrip(), ' ') + '\n'
def song_synchronizable_class(cls): # type: (T) -> T SYNCHRONIZABLE_CLASSE_NAMES.add(classname(cls, "")) return cls
def docroutine(self, object, name=None, mod=None, cl=None): """Produce text documentation for a function or method object.""" realname = object.__name__ name = name or realname note = '' skipdocs = 0 if inspect.ismethod(object): imclass = object.im_class if cl: if imclass is not cl: note = ' from ' + pydoc.classname(imclass, mod) else: if object.im_self is not None: note = ' method of %s instance' % pydoc.classname( object.im_self.__class__, mod) else: note = ' unbound %s method' % pydoc.classname(imclass, mod) object = object.im_func if name == realname: title = self.bold(realname) else: if (cl and realname in cl.__dict__ and cl.__dict__[realname] is object): skipdocs = 1 title = self.bold(name) + ' = ' + realname if inspect.isfunction(object): args, varargs, varkw, defaults = inspect.getargspec(object) argspec = inspect.formatargspec(args, varargs, varkw, defaults, formatvalue=self.formatvalue) if realname == '<lambda>': title = self.bold(name) + ' lambda ' argspec = argspec[1:-1] # remove parentheses else: argspec = '(...)' decl = title + argspec + note # Argh this pydoc thing is designed like shit... # no where to catch class methods vs base methods?!?! FAIL!!! FAIL MR GUIDO!!! FAIL!! if "(self" in decl: st = "==== %s ====\n" % decl else: st = "== %s ==\n" % decl if skipdocs: return st else: doc = pydoc.getdoc(object) or '' content = [] if doc: itrs = [] for k in doc.split('\n'): if "@param " in k: n = k.strip('@param ').split(':') itrs.append(" * '''%s''': %s" % tuple(n)) else: itrs.append(k) content.append('\n'.join(itrs) + '\n') return "%s%s\n" % (st, '\n'.join(content))
def docclass(self, object, name=None, mod=None): """Produce text documentation for a given class object.""" realname = object.__name__ name = name or realname bases = object.__bases__ def makename(c, m=object.__module__): return pydoc.classname(c, m) if name == realname: title = '== class ' + self.bold(realname) else: title = '== ' + self.bold(name) + ' = class ' + realname if bases: parents = map(makename, bases) title += '(%s)' % join(parents, ', ') title += " ==" classDoc = pydoc.getdoc(object) contents = [] if classDoc: itrs = [] for k in classDoc.split('\n'): if "@ivar " in k: n = k.strip('@ivar ').split(':') itrs.append(" * '''%s''': %s" % tuple(n)) else: itrs.append(k) contents.append('\n'.join(itrs) + '\n') push = contents.append # List the mro, if non-trivial. mro = deque(inspect.getmro(object)) if len(mro) > 2: push("Method resolution order:") for base in mro: push(' ' + makename(base)) push('') def spill(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: push(msg) for name, kind, homecls, value in ok: push( self.document(getattr(object, name), name, mod, object)) return attrs def spilldescriptors(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: push(msg) for name, kind, homecls, value in ok: push(self._docdescriptor(name, value, mod)) return attrs def spilldata(msg, attrs, predicate): ok, attrs = pydoc._split_list(attrs, predicate) if ok: push(msg) for name, kind, homecls, value in ok: if callable(value) or inspect.isdatadescriptor(value): doc = pydoc.getdoc(value) else: doc = None push( self.docother(getattr(object, name), name, mod, maxlen=70, doc=doc) + '\n') return attrs attrs = filter( lambda (name, kind, cls, value): pydoc.visiblename(name), inspect.classify_class_attrs(object)) while attrs: if mro: thisclass = mro.popleft() else: thisclass = attrs[0][2] attrs, inherited = pydoc._split_list(attrs, lambda t: t[2] is thisclass) if thisclass is __builtin__.object: attrs = inherited continue elif thisclass is object: tag = "defined here" else: tag = "inherited from %s" % pydoc.classname( thisclass, object.__module__) filter(lambda t: not t[0].startswith('_'), attrs) # Sort attrs by name. attrs.sort() # Pump out the attrs, segregated by kind. attrs = spill("=== Methods %s: ===\n" % tag, attrs, lambda t: t[1] == 'method') attrs = spill("=== Class methods %s: ===\n" % tag, attrs, lambda t: t[1] == 'class method') attrs = spill("=== Static methods %s: ===\n" % tag, attrs, lambda t: t[1] == 'static method') attrs = spilldescriptors("=== Data descriptors %s: ===\n" % tag, attrs, lambda t: t[1] == 'data descriptor') attrs = spilldata("=== Data and other attributes %s: ===\n" % tag, attrs, lambda t: t[1] == 'data') assert attrs == [] attrs = inherited contents = '\n'.join(contents) if not contents: return title + '\n' return title + '\n' + self.indent(rstrip(contents), '') + '\n'
def update_event(self, inp=-1): self.set_output_val(0, pydoc.classname(self.input(0), self.input(1)))
def docclass(self, cls, name=None, mod=None): """Produce text documentation for the class object cls.""" # the overall document, as a line-delimited list document = [] # get the object's actual name, defaulting to the passed in name name = name or cls.__name__ # get the object's bases bases = cls.__bases__ # get the object's module mod = cls.__module__ # get the object's MRO mro = [pydoc.classname(base, mod) for base in inspect.getmro(cls)] # get the object's classname, which should be printed classtitle = self.process_class_name(name, bases, mod) document.append(classtitle) document.append(self.underline) # get the object's docstring, which should be printed docstring = self.process_docstring(cls) document.append(docstring) # get all the attributes of the class attrs = [] for name, kind, classname, value in pydoc.classify_class_attrs(cls): if pydoc.visiblename(name): #if kind is not builtins.object: if classname == cls: obj = (name, kind, classname, value) attrs.append(obj) # sort them into categories data = [attr for attr in attrs if attr[1] == "data"] descriptors = [attr for attr in attrs if attr[1] == "data descriptor"] methods = [attr for attr in attrs if "method" in attr[1]] # start the data section document.append(self.process_subsection("data")) document.append(self.underline) # process your attributes for name, kind, classname, value in data: document.append(self.document(getattr(cls, name), name, mod, cls)) # start the descriptors section document.append(self.process_subsection("descriptors")) document.append(self.underline) # process your descriptors for desc in descriptors: document.append(self._docdescriptor(name, value, mod)) # start the methods section document.append(self.process_subsection("methods")) document.append(self.underline) # process your methods for f in methods: if not f[0].startswith("__"): document.append(self.docroutine(f[-1])) return "\n".join(document)
def test_pydoc_classname(self): """ pydoc should name the classes appropriately """ import pydoc from tralchemy.rdfs import Class self.failUnlessEqual(pydoc.classname(Class, "tralchemy.rdfs"), "Class") self.failUnlessEqual(pydoc.classname(Class, "tralchemy.rdf"), "tralchemy.rdfs.Class")
def makename(c, m=object.__module__): return pydoc.classname(c, m)
def docclass( self, object, name=None, mod=None ): """Produce tex documentation for a given class object.""" realname = object.__name__ name = name or realname bases = object.__bases__ def makename( c, m=object.__module__ ): return classname( c, m ) if name == realname: title = self.texttt( 'class ' + realname ) else: title = self.texttt( name + ' = class ' + realname ) if bases: parents = map( makename, bases ) title = title + '(%s)' % join( parents, ', ' ) doc = getdoc( object ) contents = doc and [doc + '\n'] or [] push = contents.append # List the mro, if non-trivial. mro = deque( inspect.getmro( object ) ) # if len(mro) > 2: # push("Method resolution order:") # for base in mro: # push(' ' + makename(base)) # push('') # class to pump out a horizontal rule between sections. class HorizontalRule: def __init__( self ): self.needone = 0 def maybe( self ): if self.needone: push( '%' * 40 ) self.needone = 1 hr = HorizontalRule() def spill( msg, attrs, predicate ): ok, attrs = _split_list( attrs, predicate ) if ok: hr.maybe() #push(msg) docstr = [] for name, kind, homecls, value in ok: if name.startswith( '__' ) and name.endswith( '__' ): pass else: docstr.append( self.document( getattr( object, name ), name, mod, object ) ) push( "\n".join( docstr ) ) return attrs def spillproperties( msg, attrs, predicate ): ok, attrs = _split_list( attrs, predicate ) if ok: hr.maybe() push( msg ) for name, kind, homecls, value in ok: push( self._docproperty( name, value, mod ) ) return attrs def spilldata( msg, attrs, predicate ): ok, attrs = _split_list( attrs, predicate ) if ok: hr.maybe() push( msg ) for name, kind, homecls, value in ok: if callable( value ) or inspect.isdatadescriptor( value ): doc = getdoc( value ) else: doc = None push( self.docother( getattr( object, name ), name, mod, 70, doc ) + '\n' ) return attrs attrs = filter( lambda ( name, kind, cls, value ): visiblename( name ), inspect.classify_class_attrs( object ) ) while attrs: if mro: thisclass = mro.popleft() else: thisclass = attrs[0][2] attrs, inherited = _split_list( attrs, lambda t: t[2] is thisclass ) if thisclass is __builtin__.object: attrs = inherited continue elif thisclass is object: tag = "defined here" else: tag = "inherited from %s" % classname( thisclass, object.__module__ ) filter( lambda t: not t[0].startswith( '_' ), attrs ) # Sort attrs by name. attrs.sort() # Pump out the attrs, segregated by kind. attrs = spill( "Methods %s:\n" % tag, attrs, lambda t: t[1] == 'method' ) attrs = spill( "Class methods %s:\n" % tag, attrs, lambda t: t[1] == 'class method' ) attrs = spill( "Static methods %s:\n" % tag, attrs, lambda t: t[1] == 'static method' ) # attrs = spillproperties("Properties %s:\n" % tag, attrs, # lambda t: t[1] == 'property') # attrs = spilldata("Data and other attributes %s:\n" % tag, attrs, # lambda t: t[1] == 'data') # assert attrs == [] # attrs = inherited contents = '\n'.join( contents ) if not contents: return title + '\n' return self.classdesc( realname, '\n' + self.indent( rstrip( contents ), ' ' ) )
def docclass(self, cls, name=None, mod=None): """Produce text documentation for the class object cls.""" # the overall document, as a line-delimited list document = [] # get the object's actual name, defaulting to the passed in name name = name or cls.__name__ # get the object's bases bases = cls.__bases__ # get the object's module mod = cls.__module__ # get the object's MRO mro = [pydoc.classname(base, mod) for base in inspect.getmro(cls)] # get the object's classname, which should be printed classtitle = self.process_class_name(name, bases, mod) document.append(classtitle) document.append(self.underline) # get the object's docstring, which should be printed docstring = self.process_docstring(cls) document.append(docstring) # get all the attributes of the class attrs = [] for name, kind, classname, value in pydoc.classify_class_attrs(cls): if pydoc.visiblename(name): attrs.append((name, kind, classname, value)) # sort them into categories data, descriptors, methods = [], [], [] for attr in attrs: if attr[1] == "data" and not attr[0].startswith("_"): data.append(attr) elif attr[1] == "data descriptor" and not attr[0].startswith("_"): descriptors.append(attr) elif "method" in attr[1] and not attr[2] is builtins.object: methods.append(attr) if data: # start the data section document.append(self.process_subsection(self.bold("data"))) document.append(self.underline) # process your attributes for name, kind, classname, value in data: if hasattr(value, '__call__') or inspect.isdatadescriptor(value): doc = getdoc(value) else: doc = None document.append( self.docother( getattr(cls, name), name, mod, maxlen=70, doc=doc) + '\n') if descriptors: # start the descriptors section document.append(self.process_subsection(self.bold("descriptors"))) document.append(self.underline) # process your descriptors for name, kind, classname, value in descriptors: document.append(self._docdescriptor(name, value, mod)) if methods: # start the methods section document.append(self.process_subsection(self.bold("methods"))) document.append(self.underline) # process your methods for name, kind, classname, value in methods: document.append( self.document(getattr(cls, name), name, mod, cls)) return "\n".join(document)
def docclass(self, object, name=None, mod=None, *ignored): """Produce text documentation for a given class object.""" realname = object.__name__ name = name or realname bases = object.__bases__ def makename(c, m=object.__module__): return classname(c, m) if name == realname: title = 'class ' + self.bold(realname) else: title = self.bold(name) + ' = class ' + realname if bases: parents = map(makename, bases) title = title + '(%s)' % ', '.join(parents) contents = [] push = contents.append try: signature = inspect.signature(object) except (ValueError, TypeError): signature = None if signature: argspec = str(signature) if argspec and argspec != '()': push(name + argspec) doc = getdoc(object) if doc: push(self.indent(doc.splitlines()[0])) # List the mro, if non-trivial. mro = deque(inspect.getmro(object)) if len(mro) > 2: push("Method resolution order:") for base in mro: push(' ' + makename(base)) # List the built-in subclasses, if any: subclasses = sorted((str(cls.__name__) for cls in type.__subclasses__(object) if not cls.__name__.startswith("_") and cls.__module__ == "builtins"), key=str.lower) no_of_subclasses = len(subclasses) MAX_SUBCLASSES_TO_DISPLAY = 4 if subclasses: push("Built-in subclasses:") for subclassname in subclasses[:MAX_SUBCLASSES_TO_DISPLAY]: push(' ' + subclassname) if no_of_subclasses > MAX_SUBCLASSES_TO_DISPLAY: push(' ... and ' + str(no_of_subclasses - MAX_SUBCLASSES_TO_DISPLAY) + ' other subclasses') push('') def header(msg): push(f"\n{msg}\n" + ("-" * len(msg))) def spill(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: header(msg) for name, kind, homecls, value in ok: try: value = getattr(object, name) except Exception: # Some descriptors may meet a failure in their __get__. # (bug #1785) push(self.docdata(value, name, mod)) else: push(self.document(value, name, mod, object)) return attrs def spilldescriptors(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: header(msg) for name, kind, homecls, value in ok: push(self.docdata(value, name, mod)) return attrs def spilldata(msg, attrs, predicate): ok, attrs = _split_list(attrs, predicate) if ok: header(msg) for name, kind, homecls, value in ok: if callable(value) or inspect.isdatadescriptor(value): doc = getdoc(value) else: doc = None try: obj = getattr(object, name) except AttributeError: obj = homecls.__dict__[name] push(self.docother(obj, name, mod, maxlen=70, doc=doc)) return attrs attrs = [(name, kind, cls, value) for name, kind, cls, value in classify_class_attrs(object) if visiblename(name, obj=object)] while attrs: if mro: thisclass = mro.popleft() else: thisclass = attrs[0][2] attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) if object is not builtins.object and thisclass is builtins.object: attrs = inherited continue elif thisclass is object: tag = "defined here" else: tag = "inherited from %s" % classname(thisclass, object.__module__) sort_attributes(attrs, object) # Pump out the attrs, segregated by kind. attrs = spill("Methods %s" % tag, attrs, lambda t: t[1] == 'method') attrs = spill("Class methods %s" % tag, attrs, lambda t: t[1] == 'class method') attrs = spill("Static methods %s" % tag, attrs, lambda t: t[1] == 'static method') attrs = spilldescriptors("Readonly properties %s" % tag, attrs, lambda t: t[1] == 'readonly property') attrs = spilldescriptors("Data descriptors %s" % tag, attrs, lambda t: t[1] == 'data descriptor') attrs = spilldata("Data and other attributes %s" % tag, attrs, lambda t: t[1] == 'data') assert attrs == [] attrs = inherited contents = '\n'.join(contents) if not contents: return title + '\n' return title + '\n' + self.indent(contents.rstrip(), ' | ') + '\n'
def docclass(self, cls, name=None, mod=None): """Produce text documentation for the class object cls.""" # the overall document, as a line-delimited list document = [] # get the object's actual name, defaulting to the passed in name name = name or cls.__name__ # get the object's bases bases = cls.__bases__ # get the object's module mod = cls.__module__ # get the object's MRO mro = [pydoc.classname(base, mod) for base in inspect.getmro(cls)] # get the object's classname, which should be printed classtitle = self.process_class_name(name, bases, mod) document.append(classtitle) document.append(self.underline) # get the object's docstring, which should be printed docstring = self.process_docstring(cls) document.append(docstring) # get all the attributes of the class attrs = [] for name, kind, classname, value in pydoc.classify_class_attrs(cls): if pydoc.visiblename(name): attrs.append((name, kind, classname, value)) # sort them into categories data, descriptors, methods = [], [], [] for attr in attrs: if attr[1] == "data" and not attr[0].startswith("_"): data.append(attr) elif attr[1] == "data descriptor" and not attr[0].startswith("_"): descriptors.append(attr) elif "method" in attr[1] and not attr[2] is builtins.object: methods.append(attr) if data: # start the data section document.append(self.process_subsection(self.bold("data"))) document.append(self.underline) # process your attributes for name, kind, classname, value in data: if hasattr(value, '__call__') or inspect.isdatadescriptor(value): doc = getdoc(value) else: doc = None document.append(self.docother(getattr(cls, name), name, mod, maxlen=70, doc=doc) + '\n') if descriptors: # start the descriptors section document.append(self.process_subsection(self.bold("descriptors"))) document.append(self.underline) # process your descriptors for name, kind, classname, value in descriptors: document.append(self._docdescriptor(name, value, mod)) if methods: # start the methods section document.append(self.process_subsection(self.bold("methods"))) document.append(self.underline) # process your methods for name, kind, classname, value in methods: document.append(self.document(getattr(cls, name), name, mod, cls)) return "\n".join(document)
def makename(c, m=object.__module__): return pydoc.classname(c, m) parents = map(makename, bases)
def store_class_data(self, cls): # type: (Any) -> None attributes = class_attributes(cls) self.song.set_data(classname(cls, ""), attributes)
def __doc_class_mro(cls, clazz, mro: deque) -> str: mro = [base for base in mro ][1:] # First remove own class name, as we see that. return cls.__heading( '\> ' + ' > '.join( [pydoc.classname(base, clazz.__module__) for base in mro]), 3)