PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/python/helpers/epydoc/apidoc.py

http://github.com/JetBrains/intellij-community
Python | 2203 lines | 2178 code | 1 blank | 24 comment | 1 complexity | c7604853c1b645d3af1fad254edb3907 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. # epydoc -- API Documentation Classes
  2. #
  3. # Copyright (C) 2005 Edward Loper
  4. # Author: Edward Loper <edloper@loper.org>
  5. # URL: <http://epydoc.sf.net>
  6. #
  7. # $Id: apidoc.py 1675 2008-01-29 17:12:56Z edloper $
  8. """
  9. Classes for encoding API documentation about Python programs.
  10. These classes are used as a common representation for combining
  11. information derived from introspection and from parsing.
  12. The API documentation for a Python program is encoded using a graph of
  13. L{APIDoc} objects, each of which encodes information about a single
  14. Python variable or value. C{APIDoc} has two direct subclasses:
  15. L{VariableDoc}, for documenting variables; and L{ValueDoc}, for
  16. documenting values. The C{ValueDoc} class is subclassed further, to
  17. define the different pieces of information that should be recorded
  18. about each value type:
  19. G{classtree: APIDoc}
  20. The distinction between variables and values is intentionally made
  21. explicit. This allows us to distinguish information about a variable
  22. itself (such as whether it should be considered 'public' in its
  23. containing namespace) from information about the value it contains
  24. (such as what type the value has). This distinction is also important
  25. because several variables can contain the same value: each variable
  26. should be described by a separate C{VariableDoc}; but we only need one
  27. C{ValueDoc}, since they share a single value.
  28. @todo: Add a cache to canonical name lookup?
  29. """
  30. __docformat__ = 'epytext en'
  31. ######################################################################
  32. ## Imports
  33. ######################################################################
  34. import types, re, os.path, pickle
  35. from epydoc import log
  36. import epydoc
  37. import __builtin__
  38. from epydoc.compat import * # Backwards compatibility
  39. from epydoc.util import decode_with_backslashreplace, py_src_filename
  40. import epydoc.markup.pyval_repr
  41. ######################################################################
  42. # Dotted Names
  43. ######################################################################
  44. class DottedName:
  45. """
  46. A sequence of identifiers, separated by periods, used to name a
  47. Python variable, value, or argument. The identifiers that make up
  48. a dotted name can be accessed using the indexing operator:
  49. >>> name = DottedName('epydoc', 'api_doc', 'DottedName')
  50. >>> print name
  51. epydoc.apidoc.DottedName
  52. >>> name[1]
  53. 'api_doc'
  54. """
  55. UNREACHABLE = "??"
  56. _IDENTIFIER_RE = re.compile("""(?x)
  57. (%s | # UNREACHABLE marker, or..
  58. (script-)? # Prefix: script (not a module)
  59. \w+ # Identifier (yes, identifiers starting with a
  60. # digit are allowed. See SF bug #1649347)
  61. '?) # Suffix: submodule that is shadowed by a var
  62. (-\d+)? # Suffix: unreachable vals with the same name
  63. $"""
  64. % re.escape(UNREACHABLE))
  65. class InvalidDottedName(ValueError):
  66. """
  67. An exception raised by the DottedName constructor when one of
  68. its arguments is not a valid dotted name.
  69. """
  70. _ok_identifiers = set()
  71. """A cache of identifier strings that have been checked against
  72. _IDENTIFIER_RE and found to be acceptable."""
  73. def __init__(self, *pieces, **options):
  74. """
  75. Construct a new dotted name from the given sequence of pieces,
  76. each of which can be either a C{string} or a C{DottedName}.
  77. Each piece is divided into a sequence of identifiers, and
  78. these sequences are combined together (in order) to form the
  79. identifier sequence for the new C{DottedName}. If a piece
  80. contains a string, then it is divided into substrings by
  81. splitting on periods, and each substring is checked to see if
  82. it is a valid identifier.
  83. As an optimization, C{pieces} may also contain a single tuple
  84. of values. In that case, that tuple will be used as the
  85. C{DottedName}'s identifiers; it will I{not} be checked to
  86. see if it's valid.
  87. @kwparam strict: if true, then raise an L{InvalidDottedName}
  88. if the given name is invalid.
  89. """
  90. if len(pieces) == 1 and isinstance(pieces[0], tuple):
  91. self._identifiers = pieces[0] # Optimization
  92. return
  93. if len(pieces) == 0:
  94. raise DottedName.InvalidDottedName('Empty DottedName')
  95. self._identifiers = []
  96. for piece in pieces:
  97. if isinstance(piece, DottedName):
  98. self._identifiers += piece._identifiers
  99. elif isinstance(piece, basestring):
  100. for subpiece in piece.split('.'):
  101. if piece not in self._ok_identifiers:
  102. if not self._IDENTIFIER_RE.match(subpiece):
  103. if options.get('strict'):
  104. raise DottedName.InvalidDottedName(
  105. 'Bad identifier %r' % (piece,))
  106. else:
  107. log.warning("Identifier %r looks suspicious; "
  108. "using it anyway." % piece)
  109. self._ok_identifiers.add(piece)
  110. self._identifiers.append(subpiece)
  111. else:
  112. raise TypeError('Bad identifier %r: expected '
  113. 'DottedName or str' % (piece,))
  114. self._identifiers = tuple(self._identifiers)
  115. def __repr__(self):
  116. idents = [`ident` for ident in self._identifiers]
  117. return 'DottedName(' + ', '.join(idents) + ')'
  118. def __str__(self):
  119. """
  120. Return the dotted name as a string formed by joining its
  121. identifiers with periods:
  122. >>> print DottedName('epydoc', 'api_doc', DottedName')
  123. epydoc.apidoc.DottedName
  124. """
  125. return '.'.join(self._identifiers)
  126. def __add__(self, other):
  127. """
  128. Return a new C{DottedName} whose identifier sequence is formed
  129. by adding C{other}'s identifier sequence to C{self}'s.
  130. """
  131. if isinstance(other, (basestring, DottedName)):
  132. return DottedName(self, other)
  133. else:
  134. return DottedName(self, *other)
  135. def __radd__(self, other):
  136. """
  137. Return a new C{DottedName} whose identifier sequence is formed
  138. by adding C{self}'s identifier sequence to C{other}'s.
  139. """
  140. if isinstance(other, (basestring, DottedName)):
  141. return DottedName(other, self)
  142. else:
  143. return DottedName(*(list(other)+[self]))
  144. def __getitem__(self, i):
  145. """
  146. Return the C{i}th identifier in this C{DottedName}. If C{i} is
  147. a non-empty slice, then return a C{DottedName} built from the
  148. identifiers selected by the slice. If C{i} is an empty slice,
  149. return an empty list (since empty C{DottedName}s are not valid).
  150. """
  151. if isinstance(i, types.SliceType):
  152. pieces = self._identifiers[i.start:i.stop]
  153. if pieces: return DottedName(pieces)
  154. else: return []
  155. else:
  156. return self._identifiers[i]
  157. def __hash__(self):
  158. return hash(self._identifiers)
  159. def __cmp__(self, other):
  160. """
  161. Compare this dotted name to C{other}. Two dotted names are
  162. considered equal if their identifier subsequences are equal.
  163. Ordering between dotted names is lexicographic, in order of
  164. identifier from left to right.
  165. """
  166. if not isinstance(other, DottedName):
  167. return -1
  168. return cmp(self._identifiers, other._identifiers)
  169. def __len__(self):
  170. """
  171. Return the number of identifiers in this dotted name.
  172. """
  173. return len(self._identifiers)
  174. def container(self):
  175. """
  176. Return the DottedName formed by removing the last identifier
  177. from this dotted name's identifier sequence. If this dotted
  178. name only has one name in its identifier sequence, return
  179. C{None} instead.
  180. """
  181. if len(self._identifiers) == 1:
  182. return None
  183. else:
  184. return DottedName(*self._identifiers[:-1])
  185. def dominates(self, name, strict=False):
  186. """
  187. Return true if this dotted name is equal to a prefix of
  188. C{name}. If C{strict} is true, then also require that
  189. C{self!=name}.
  190. >>> DottedName('a.b').dominates(DottedName('a.b.c.d'))
  191. True
  192. """
  193. len_self = len(self._identifiers)
  194. len_name = len(name._identifiers)
  195. if (len_self > len_name) or (strict and len_self == len_name):
  196. return False
  197. # The following is redundant (the first clause is implied by
  198. # the second), but is done as an optimization.
  199. return ((self._identifiers[0] == name._identifiers[0]) and
  200. self._identifiers == name._identifiers[:len_self])
  201. def contextualize(self, context):
  202. """
  203. If C{self} and C{context} share a common ancestor, then return
  204. a name for C{self}, relative to that ancestor. If they do not
  205. share a common ancestor (or if C{context} is C{UNKNOWN}), then
  206. simply return C{self}.
  207. This is used to generate shorter versions of dotted names in
  208. cases where users can infer the intended target from the
  209. context.
  210. @type context: L{DottedName}
  211. @rtype: L{DottedName}
  212. """
  213. if context is UNKNOWN or not context or len(self) <= 1:
  214. return self
  215. if self[0] == context[0]:
  216. return self[1:].contextualize(context[1:])
  217. else:
  218. return self
  219. # Find the first index where self & context differ.
  220. for i in range(min(len(context), len(self))):
  221. if self._identifiers[i] != context._identifiers[i]:
  222. first_difference = i
  223. break
  224. else:
  225. first_difference = i+1
  226. # Strip off anything before that index.
  227. if first_difference == 0:
  228. return self
  229. elif first_difference == len(self):
  230. return self[-1:]
  231. else:
  232. return self[first_difference:]
  233. ######################################################################
  234. # UNKNOWN Value
  235. ######################################################################
  236. class _Sentinel:
  237. """
  238. A unique value that won't compare equal to any other value. This
  239. class is used to create L{UNKNOWN}.
  240. """
  241. def __init__(self, name):
  242. self.name = name
  243. def __repr__(self):
  244. return '<%s>' % self.name
  245. def __nonzero__(self):
  246. raise ValueError('Sentinel value <%s> can not be used as a boolean' %
  247. self.name)
  248. UNKNOWN = _Sentinel('UNKNOWN')
  249. """A special value used to indicate that a given piece of
  250. information about an object is unknown. This is used as the
  251. default value for all instance variables."""
  252. ######################################################################
  253. # API Documentation Objects: Abstract Base Classes
  254. ######################################################################
  255. class APIDoc(object):
  256. """
  257. API documentation information for a single element of a Python
  258. program. C{APIDoc} itself is an abstract base class; subclasses
  259. are used to specify what information should be recorded about each
  260. type of program element. In particular, C{APIDoc} has two direct
  261. subclasses, C{VariableDoc} for documenting variables and
  262. C{ValueDoc} for documenting values; and the C{ValueDoc} class is
  263. subclassed further for different value types.
  264. Each C{APIDoc} subclass specifies the set of attributes that
  265. should be used to record information about the corresponding
  266. program element type. The default value for each attribute is
  267. stored in the class; these default values can then be overridden
  268. with instance variables. Most attributes use the special value
  269. L{UNKNOWN} as their default value, to indicate that the correct
  270. value for that attribute has not yet been determined. This makes
  271. it easier to merge two C{APIDoc} objects that are documenting the
  272. same element (in particular, to merge information about an element
  273. that was derived from parsing with information that was derived
  274. from introspection).
  275. For all attributes with boolean values, use only the constants
  276. C{True} and C{False} to designate true and false. In particular,
  277. do I{not} use other values that evaluate as true or false, such as
  278. C{2} or C{()}. This restriction makes it easier to handle
  279. C{UNKNOWN} values. For example, to test if a boolean attribute is
  280. C{True} or C{UNKNOWN}, use 'C{attrib in (True, UNKNOWN)}' or
  281. 'C{attrib is not False}'.
  282. Two C{APIDoc} objects describing the same object can be X{merged},
  283. using the method L{merge_and_overwrite(other)}. After two
  284. C{APIDoc}s are merged, any changes to one will be reflected in the
  285. other. This is accomplished by setting the two C{APIDoc} objects
  286. to use a shared instance dictionary. See the documentation for
  287. L{merge_and_overwrite} for more information, and some important
  288. caveats about hashing.
  289. """
  290. #{ Docstrings
  291. docstring = UNKNOWN
  292. """@ivar: The documented item's docstring.
  293. @type: C{string} or C{None}"""
  294. docstring_lineno = UNKNOWN
  295. """@ivar: The line number on which the documented item's docstring
  296. begins.
  297. @type: C{int}"""
  298. #} end of "docstrings" group
  299. #{ Information Extracted from Docstrings
  300. descr = UNKNOWN
  301. """@ivar: A description of the documented item, extracted from its
  302. docstring.
  303. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  304. summary = UNKNOWN
  305. """@ivar: A summary description of the documented item, extracted from
  306. its docstring.
  307. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  308. other_docs = UNKNOWN
  309. """@ivar: A flag indicating if the entire L{docstring} body (except tags
  310. if any) is entirely included in the L{summary}.
  311. @type: C{bool}"""
  312. metadata = UNKNOWN
  313. """@ivar: Metadata about the documented item, extracted from fields in
  314. its docstring. I{Currently} this is encoded as a list of tuples
  315. C{(field, arg, descr)}. But that may change.
  316. @type: C{(str, str, L{ParsedDocstring<markup.ParsedDocstring>})}"""
  317. extra_docstring_fields = UNKNOWN
  318. """@ivar: A list of new docstring fields tags that are defined by the
  319. documented item's docstring. These new field tags can be used by
  320. this item or by any item it contains.
  321. @type: L{DocstringField <epydoc.docstringparser.DocstringField>}"""
  322. #} end of "information extracted from docstrings" group
  323. #{ Source Information
  324. docs_extracted_by = UNKNOWN # 'parser' or 'introspecter' or 'both'
  325. """@ivar: Information about where the information contained by this
  326. C{APIDoc} came from. Can be one of C{'parser'},
  327. C{'introspector'}, or C{'both'}.
  328. @type: C{str}"""
  329. #} end of "source information" group
  330. def __init__(self, **kwargs):
  331. """
  332. Construct a new C{APIDoc} object. Keyword arguments may be
  333. used to initialize the new C{APIDoc}'s attributes.
  334. @raise TypeError: If a keyword argument is specified that does
  335. not correspond to a valid attribute for this (sub)class of
  336. C{APIDoc}.
  337. """
  338. if epydoc.DEBUG:
  339. for key in kwargs:
  340. if key[0] != '_' and not hasattr(self.__class__, key):
  341. raise TypeError('%s got unexpected arg %r' %
  342. (self.__class__.__name__, key))
  343. self.__dict__.update(kwargs)
  344. def _debug_setattr(self, attr, val):
  345. """
  346. Modify an C{APIDoc}'s attribute. This is used when
  347. L{epydoc.DEBUG} is true, to make sure we don't accidentally
  348. set any inappropriate attributes on C{APIDoc} objects.
  349. @raise AttributeError: If C{attr} is not a valid attribute for
  350. this (sub)class of C{APIDoc}. (C{attr} is considered a
  351. valid attribute iff C{self.__class__} defines an attribute
  352. with that name.)
  353. """
  354. # Don't intercept special assignments like __class__, or
  355. # assignments to private variables.
  356. if attr.startswith('_'):
  357. return object.__setattr__(self, attr, val)
  358. if not hasattr(self, attr):
  359. raise AttributeError('%s does not define attribute %r' %
  360. (self.__class__.__name__, attr))
  361. self.__dict__[attr] = val
  362. if epydoc.DEBUG:
  363. __setattr__ = _debug_setattr
  364. def __repr__(self):
  365. return '<%s>' % self.__class__.__name__
  366. def pp(self, doublespace=0, depth=5, exclude=(), include=()):
  367. """
  368. Return a pretty-printed string representation for the
  369. information contained in this C{APIDoc}.
  370. """
  371. return pp_apidoc(self, doublespace, depth, exclude, include)
  372. __str__ = pp
  373. def specialize_to(self, cls):
  374. """
  375. Change C{self}'s class to C{cls}. C{cls} must be a subclass
  376. of C{self}'s current class. For example, if a generic
  377. C{ValueDoc} was created for a value, and it is determined that
  378. the value is a routine, you can update its class with:
  379. >>> valdoc.specialize_to(RoutineDoc)
  380. """
  381. if not issubclass(cls, self.__class__):
  382. raise ValueError('Can not specialize to %r' % cls)
  383. # Update the class.
  384. self.__class__ = cls
  385. # Update the class of any other apidoc's in the mergeset.
  386. if self.__mergeset is not None:
  387. for apidoc in self.__mergeset:
  388. apidoc.__class__ = cls
  389. # Re-initialize self, in case the subclass constructor does
  390. # any special processing on its arguments.
  391. self.__init__(**self.__dict__)
  392. __has_been_hashed = False
  393. """True iff L{self.__hash__()} has ever been called."""
  394. def __hash__(self):
  395. self.__has_been_hashed = True
  396. return id(self.__dict__)
  397. def __cmp__(self, other):
  398. if not isinstance(other, APIDoc): return -1
  399. if self.__dict__ is other.__dict__: return 0
  400. name_cmp = cmp(self.canonical_name, other.canonical_name)
  401. if name_cmp == 0: return -1
  402. else: return name_cmp
  403. def is_detailed(self):
  404. """
  405. Does this object deserve a box with extra details?
  406. @return: True if the object needs extra details, else False.
  407. @rtype: C{bool}
  408. """
  409. if self.other_docs is True:
  410. return True
  411. if self.metadata is not UNKNOWN:
  412. return bool(self.metadata)
  413. __mergeset = None
  414. """The set of all C{APIDoc} objects that have been merged with
  415. this C{APIDoc} (using L{merge_and_overwrite()}). Each C{APIDoc}
  416. in this set shares a common instance dictionary (C{__dict__})."""
  417. def merge_and_overwrite(self, other, ignore_hash_conflict=False):
  418. """
  419. Combine C{self} and C{other} into a X{merged object}, such
  420. that any changes made to one will affect the other. Any
  421. attributes that C{other} had before merging will be discarded.
  422. This is accomplished by copying C{self.__dict__} over
  423. C{other.__dict__} and C{self.__class__} over C{other.__class__}.
  424. Care must be taken with this method, since it modifies the
  425. hash value of C{other}. To help avoid the problems that this
  426. can cause, C{merge_and_overwrite} will raise an exception if
  427. C{other} has ever been hashed, unless C{ignore_hash_conflict}
  428. is True. Note that adding C{other} to a dictionary, set, or
  429. similar data structure will implicitly cause it to be hashed.
  430. If you do set C{ignore_hash_conflict} to True, then any
  431. existing data structures that rely on C{other}'s hash staying
  432. constant may become corrupted.
  433. @return: C{self}
  434. @raise ValueError: If C{other} has ever been hashed.
  435. """
  436. # If we're already merged, then there's nothing to do.
  437. if (self.__dict__ is other.__dict__ and
  438. self.__class__ is other.__class__): return self
  439. if other.__has_been_hashed and not ignore_hash_conflict:
  440. raise ValueError("%r has already been hashed! Merging it "
  441. "would cause its has value to change." % other)
  442. # If other was itself already merged with anything,
  443. # then we need to merge those too.
  444. a,b = (self.__mergeset, other.__mergeset)
  445. mergeset = (self.__mergeset or [self]) + (other.__mergeset or [other])
  446. other.__dict__.clear()
  447. for apidoc in mergeset:
  448. #if apidoc is self: pass
  449. apidoc.__class__ = self.__class__
  450. apidoc.__dict__ = self.__dict__
  451. self.__mergeset = mergeset
  452. # Sanity chacks.
  453. assert self in mergeset and other in mergeset
  454. for apidoc in mergeset:
  455. assert apidoc.__dict__ is self.__dict__
  456. # Return self.
  457. return self
  458. def apidoc_links(self, **filters):
  459. """
  460. Return a list of all C{APIDoc}s that are directly linked from
  461. this C{APIDoc} (i.e., are contained or pointed to by one or
  462. more of this C{APIDoc}'s attributes.)
  463. Keyword argument C{filters} can be used to selectively exclude
  464. certain categories of attribute value. For example, using
  465. C{includes=False} will exclude variables that were imported
  466. from other modules; and C{subclasses=False} will exclude
  467. subclasses. The filter categories currently supported by
  468. epydoc are:
  469. - C{imports}: Imported variables.
  470. - C{packages}: Containing packages for modules.
  471. - C{submodules}: Contained submodules for packages.
  472. - C{bases}: Bases for classes.
  473. - C{subclasses}: Subclasses for classes.
  474. - C{variables}: All variables.
  475. - C{private}: Private variables.
  476. - C{overrides}: Points from class variables to the variables
  477. they override. This filter is False by default.
  478. """
  479. return []
  480. def reachable_valdocs(root, **filters):
  481. """
  482. Return a list of all C{ValueDoc}s that can be reached, directly or
  483. indirectly from the given root list of C{ValueDoc}s.
  484. @param filters: A set of filters that can be used to prevent
  485. C{reachable_valdocs} from following specific link types when
  486. looking for C{ValueDoc}s that can be reached from the root
  487. set. See C{APIDoc.apidoc_links} for a more complete
  488. description.
  489. """
  490. apidoc_queue = list(root)
  491. val_set = set()
  492. var_set = set()
  493. while apidoc_queue:
  494. api_doc = apidoc_queue.pop()
  495. if isinstance(api_doc, ValueDoc):
  496. val_set.add(api_doc)
  497. else:
  498. var_set.add(api_doc)
  499. apidoc_queue.extend([v for v in api_doc.apidoc_links(**filters)
  500. if v not in val_set and v not in var_set])
  501. return val_set
  502. ######################################################################
  503. # Variable Documentation Objects
  504. ######################################################################
  505. class VariableDoc(APIDoc):
  506. """
  507. API documentation information about a single Python variable.
  508. @note: The only time a C{VariableDoc} will have its own docstring
  509. is if that variable was created using an assignment statement, and
  510. that assignment statement had a docstring-comment or was followed
  511. by a pseudo-docstring.
  512. """
  513. #{ Basic Variable Information
  514. name = UNKNOWN
  515. """@ivar: The name of this variable in its containing namespace.
  516. @type: C{str}"""
  517. container = UNKNOWN
  518. """@ivar: API documentation for the namespace that contains this
  519. variable.
  520. @type: L{ValueDoc}"""
  521. canonical_name = UNKNOWN
  522. """@ivar: A dotted name that serves as a unique identifier for
  523. this C{VariableDoc}. It should be formed by concatenating
  524. the C{VariableDoc}'s C{container} with its C{name}.
  525. @type: L{DottedName}"""
  526. value = UNKNOWN
  527. """@ivar: The API documentation for this variable's value.
  528. @type: L{ValueDoc}"""
  529. #}
  530. #{ Information Extracted from Docstrings
  531. type_descr = UNKNOWN
  532. """@ivar: A description of the variable's expected type, extracted from
  533. its docstring.
  534. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  535. #} end of "information extracted from docstrings" group
  536. #{ Information about Imported Variables
  537. imported_from = UNKNOWN
  538. """@ivar: The fully qualified dotted name of the variable that this
  539. variable's value was imported from. This attribute should only
  540. be defined if C{is_instvar} is true.
  541. @type: L{DottedName}"""
  542. is_imported = UNKNOWN
  543. """@ivar: Was this variable's value imported from another module?
  544. (Exception: variables that are explicitly included in __all__ have
  545. C{is_imported} set to C{False}, even if they are in fact
  546. imported.)
  547. @type: C{bool}"""
  548. #} end of "information about imported variables" group
  549. #{ Information about Variables in Classes
  550. is_instvar = UNKNOWN
  551. """@ivar: If true, then this variable is an instance variable; if false,
  552. then this variable is a class variable. This attribute should
  553. only be defined if the containing namespace is a class
  554. @type: C{bool}"""
  555. overrides = UNKNOWN # [XXX] rename -- don't use a verb.
  556. """@ivar: The API documentation for the variable that is overridden by
  557. this variable. This attribute should only be defined if the
  558. containing namespace is a class.
  559. @type: L{VariableDoc}"""
  560. #} end of "information about variables in classes" group
  561. #{ Flags
  562. is_alias = UNKNOWN
  563. """@ivar: Is this variable an alias for another variable with the same
  564. value? If so, then this variable will be dispreferred when
  565. assigning canonical names.
  566. @type: C{bool}"""
  567. is_public = UNKNOWN
  568. """@ivar: Is this variable part of its container's public API?
  569. @type: C{bool}"""
  570. #} end of "flags" group
  571. def __init__(self, **kwargs):
  572. APIDoc.__init__(self, **kwargs)
  573. if self.is_public is UNKNOWN and self.name is not UNKNOWN:
  574. self.is_public = (not self.name.startswith('_') or
  575. self.name.endswith('_'))
  576. def __repr__(self):
  577. if self.canonical_name is not UNKNOWN:
  578. return '<%s %s>' % (self.__class__.__name__, self.canonical_name)
  579. if self.name is not UNKNOWN:
  580. return '<%s %s>' % (self.__class__.__name__, self.name)
  581. else:
  582. return '<%s>' % self.__class__.__name__
  583. def _get_defining_module(self):
  584. if self.container is UNKNOWN:
  585. return UNKNOWN
  586. return self.container.defining_module
  587. defining_module = property(_get_defining_module, doc="""
  588. A read-only property that can be used to get the variable's
  589. defining module. This is defined as the defining module
  590. of the variable's container.""")
  591. def apidoc_links(self, **filters):
  592. # nb: overrides filter is *False* by default.
  593. if (filters.get('overrides', False) and
  594. (self.overrides not in (None, UNKNOWN))):
  595. overrides = [self.overrides]
  596. else:
  597. overrides = []
  598. if self.value in (None, UNKNOWN):
  599. return []+overrides
  600. else:
  601. return [self.value]+overrides
  602. def is_detailed(self):
  603. pval = super(VariableDoc, self).is_detailed()
  604. if pval or self.value in (None, UNKNOWN):
  605. return pval
  606. if (self.overrides not in (None, UNKNOWN) and
  607. isinstance(self.value, RoutineDoc)):
  608. return True
  609. if isinstance(self.value, GenericValueDoc):
  610. # [XX] This is a little hackish -- we assume that the
  611. # summary lines will have SUMMARY_REPR_LINELEN chars,
  612. # that len(name) of those will be taken up by the name,
  613. # and that 3 of those will be taken up by " = " between
  614. # the name & val. Note that if any docwriter uses a
  615. # different formula for maxlen for this, then it will
  616. # not get the right value for is_detailed().
  617. maxlen = self.value.SUMMARY_REPR_LINELEN-3-len(self.name)
  618. return (not self.value.summary_pyval_repr(maxlen).is_complete)
  619. else:
  620. return self.value.is_detailed()
  621. ######################################################################
  622. # Value Documentation Objects
  623. ######################################################################
  624. class ValueDoc(APIDoc):
  625. """
  626. API documentation information about a single Python value.
  627. """
  628. canonical_name = UNKNOWN
  629. """@ivar: A dotted name that serves as a unique identifier for
  630. this C{ValueDoc}'s value. If the value can be reached using a
  631. single sequence of identifiers (given the appropriate imports),
  632. then that sequence of identifiers is used as its canonical name.
  633. If the value can be reached by multiple sequences of identifiers
  634. (i.e., if it has multiple aliases), then one of those sequences of
  635. identifiers is used. If the value cannot be reached by any
  636. sequence of identifiers (e.g., if it was used as a base class but
  637. then its variable was deleted), then its canonical name will start
  638. with C{'??'}. If necessary, a dash followed by a number will be
  639. appended to the end of a non-reachable identifier to make its
  640. canonical name unique.
  641. When possible, canonical names are chosen when new C{ValueDoc}s
  642. are created. However, this is sometimes not possible. If a
  643. canonical name can not be chosen when the C{ValueDoc} is created,
  644. then one will be assigned by L{assign_canonical_names()
  645. <docbuilder.assign_canonical_names>}.
  646. @type: L{DottedName}"""
  647. #{ Value Representation
  648. pyval = UNKNOWN
  649. """@ivar: A pointer to the actual Python object described by this
  650. C{ValueDoc}. This is used to display the value (e.g., when
  651. describing a variable.) Use L{pyval_repr()} to generate a
  652. plaintext string representation of this value.
  653. @type: Python object"""
  654. parse_repr = UNKNOWN
  655. """@ivar: A text representation of this value, extracted from
  656. parsing its source code. This representation may not accurately
  657. reflect the actual value (e.g., if the value was modified after
  658. the initial assignment).
  659. @type: C{unicode}"""
  660. REPR_MAXLINES = 5
  661. """@cvar: The maximum number of lines of text that should be
  662. generated by L{pyval_repr()}. If the string representation does
  663. not fit in this number of lines, an ellpsis marker (...) will
  664. be placed at the end of the formatted representation."""
  665. REPR_LINELEN = 75
  666. """@cvar: The maximum number of characters for lines of text that
  667. should be generated by L{pyval_repr()}. Any lines that exceed
  668. this number of characters will be line-wrappped; The S{crarr}
  669. symbol will be used to indicate that the line was wrapped."""
  670. SUMMARY_REPR_LINELEN = 75
  671. """@cvar: The maximum number of characters for the single-line
  672. text representation generated by L{summary_pyval_repr()}. If
  673. the value's representation does not fit in this number of
  674. characters, an ellipsis marker (...) will be placed at the end
  675. of the formatted representation."""
  676. REPR_MIN_SCORE = 0
  677. """@cvar: The minimum score that a value representation based on
  678. L{pyval} should have in order to be used instead of L{parse_repr}
  679. as the canonical representation for this C{ValueDoc}'s value.
  680. @see: L{epydoc.markup.pyval_repr}"""
  681. #} end of "value representation" group
  682. #{ Context
  683. defining_module = UNKNOWN
  684. """@ivar: The documentation for the module that defines this
  685. value. This is used, e.g., to lookup the appropriate markup
  686. language for docstrings. For a C{ModuleDoc},
  687. C{defining_module} should be C{self}.
  688. @type: L{ModuleDoc}"""
  689. #} end of "context group"
  690. #{ Information about Imported Variables
  691. proxy_for = None # [xx] in progress.
  692. """@ivar: If C{proxy_for} is not None, then this value was
  693. imported from another file. C{proxy_for} is the dotted name of
  694. the variable that this value was imported from. If that
  695. variable is documented, then its C{value} may contain more
  696. complete API documentation about this value. The C{proxy_for}
  697. attribute is used by the source code parser to link imported
  698. values to their source values (in particular, for base
  699. classes). When possible, these proxy C{ValueDoc}s are replaced
  700. by the imported value's C{ValueDoc} by
  701. L{link_imports()<docbuilder.link_imports>}.
  702. @type: L{DottedName}"""
  703. #} end of "information about imported variables" group
  704. #: @ivar:
  705. #: This is currently used to extract values from __all__, etc, in
  706. #: the docparser module; maybe I should specialize
  707. #: process_assignment and extract it there? Although, for __all__,
  708. #: it's not clear where I'd put the value, since I just use it to
  709. #: set private/public/imported attribs on other vars (that might not
  710. #: exist yet at the time.)
  711. toktree = UNKNOWN
  712. def __repr__(self):
  713. if self.canonical_name is not UNKNOWN:
  714. return '<%s %s>' % (self.__class__.__name__, self.canonical_name)
  715. else:
  716. return '<%s %s>' % (self.__class__.__name__,
  717. self.summary_pyval_repr().to_plaintext(None))
  718. def __setstate__(self, state):
  719. self.__dict__ = state
  720. def __getstate__(self):
  721. """
  722. State serializer for the pickle module. This is necessary
  723. because sometimes the C{pyval} attribute contains an
  724. un-pickleable value.
  725. """
  726. # Construct our pickled dictionary. Maintain this dictionary
  727. # as a private attribute, so we can reuse it later, since
  728. # merged objects need to share a single dictionary.
  729. if not hasattr(self, '_ValueDoc__pickle_state'):
  730. # Make sure __pyval_repr & __summary_pyval_repr are cached:
  731. self.pyval_repr(), self.summary_pyval_repr()
  732. # Construct the dictionary; leave out 'pyval'.
  733. self.__pickle_state = self.__dict__.copy()
  734. self.__pickle_state['pyval'] = UNKNOWN
  735. if not isinstance(self, GenericValueDoc):
  736. assert self.__pickle_state != {}
  737. # Return the pickle state.
  738. return self.__pickle_state
  739. #{ Value Representation
  740. def pyval_repr(self):
  741. """
  742. Return a formatted representation of the Python object
  743. described by this C{ValueDoc}. This representation may
  744. include data from introspection or parsing, and is authorative
  745. as 'the best way to represent a Python value.' Any lines that
  746. go beyond L{REPR_LINELEN} characters will be wrapped; and if
  747. the representation as a whole takes more than L{REPR_MAXLINES}
  748. lines, then it will be truncated (with an ellipsis marker).
  749. This function will never return L{UNKNOWN} or C{None}.
  750. @rtype: L{ColorizedPyvalRepr}
  751. """
  752. # Use self.__pyval_repr to cache the result.
  753. if not hasattr(self, '_ValueDoc__pyval_repr'):
  754. self.__pyval_repr = epydoc.markup.pyval_repr.colorize_pyval(
  755. self.pyval, self.parse_repr, self.REPR_MIN_SCORE,
  756. self.REPR_LINELEN, self.REPR_MAXLINES, linebreakok=True)
  757. return self.__pyval_repr
  758. def summary_pyval_repr(self, max_len=None):
  759. """
  760. Return a single-line formatted representation of the Python
  761. object described by this C{ValueDoc}. This representation may
  762. include data from introspection or parsing, and is authorative
  763. as 'the best way to summarize a Python value.' If the
  764. representation takes more then L{SUMMARY_REPR_LINELEN}
  765. characters, then it will be truncated (with an ellipsis
  766. marker). This function will never return L{UNKNOWN} or
  767. C{None}.
  768. @rtype: L{ColorizedPyvalRepr}
  769. """
  770. # If max_len is specified, then do *not* cache the result.
  771. if max_len is not None:
  772. return epydoc.markup.pyval_repr.colorize_pyval(
  773. self.pyval, self.parse_repr, self.REPR_MIN_SCORE,
  774. max_len, maxlines=1, linebreakok=False)
  775. # Use self.__summary_pyval_repr to cache the result.
  776. if not hasattr(self, '_ValueDoc__summary_pyval_repr'):
  777. self.__summary_pyval_repr = epydoc.markup.pyval_repr.colorize_pyval(
  778. self.pyval, self.parse_repr, self.REPR_MIN_SCORE,
  779. self.SUMMARY_REPR_LINELEN, maxlines=1, linebreakok=False)
  780. return self.__summary_pyval_repr
  781. #} end of "value representation" group
  782. def apidoc_links(self, **filters):
  783. return []
  784. class GenericValueDoc(ValueDoc):
  785. """
  786. API documentation about a 'generic' value, i.e., one that does not
  787. have its own docstring or any information other than its value and
  788. parse representation. C{GenericValueDoc}s do not get assigned
  789. cannonical names.
  790. """
  791. canonical_name = None
  792. def is_detailed(self):
  793. return (not self.summary_pyval_repr().is_complete)
  794. class NamespaceDoc(ValueDoc):
  795. """
  796. API documentation information about a singe Python namespace
  797. value. (I.e., a module or a class).
  798. """
  799. #{ Information about Variables
  800. variables = UNKNOWN
  801. """@ivar: The contents of the namespace, encoded as a
  802. dictionary mapping from identifiers to C{VariableDoc}s. This
  803. dictionary contains all names defined by the namespace,
  804. including imported variables, aliased variables, and variables
  805. inherited from base classes (once L{inherit_docs()
  806. <epydoc.docbuilder.inherit_docs>} has added them).
  807. @type: C{dict} from C{string} to L{VariableDoc}"""
  808. sorted_variables = UNKNOWN
  809. """@ivar: A list of all variables defined by this
  810. namespace, in sorted order. The elements of this list should
  811. exactly match the values of L{variables}. The sort order for
  812. this list is defined as follows:
  813. - Any variables listed in a C{@sort} docstring field are
  814. listed in the order given by that field.
  815. - These are followed by any variables that were found while
  816. parsing the source code, in the order in which they were
  817. defined in the source file.
  818. - Finally, any remaining variables are listed in
  819. alphabetical order.
  820. @type: C{list} of L{VariableDoc}"""
  821. sort_spec = UNKNOWN
  822. """@ivar: The order in which variables should be listed,
  823. encoded as a list of names. Any variables whose names are not
  824. included in this list should be listed alphabetically,
  825. following the variables that are included.
  826. @type: C{list} of C{str}"""
  827. group_specs = UNKNOWN
  828. """@ivar: The groups that are defined by this namespace's
  829. docstrings. C{group_specs} is encoded as an ordered list of
  830. tuples C{(group_name, elt_names)}, where C{group_name} is the
  831. name of a group and C{elt_names} is a list of element names in
  832. that group. (An element can be a variable or a submodule.) A
  833. '*' in an element name will match any string of characters.
  834. @type: C{list} of C{(str,list)}"""
  835. variable_groups = UNKNOWN
  836. """@ivar: A dictionary specifying what group each
  837. variable belongs to. The keys of the dictionary are group
  838. names, and the values are lists of C{VariableDoc}s. The order
  839. that groups should be listed in should be taken from
  840. L{group_specs}.
  841. @type: C{dict} from C{str} to C{list} of L{VariableDoc}"""
  842. #} end of group "information about variables"
  843. def __init__(self, **kwargs):
  844. kwargs.setdefault('variables', {})
  845. APIDoc.__init__(self, **kwargs)
  846. assert self.variables is not UNKNOWN
  847. def is_detailed(self):
  848. return True
  849. def apidoc_links(self, **filters):
  850. variables = filters.get('variables', True)
  851. imports = filters.get('imports', True)
  852. private = filters.get('private', True)
  853. if variables and imports and private:
  854. return self.variables.values() # list the common case first.
  855. elif not variables:
  856. return []
  857. elif not imports and not private:
  858. return [v for v in self.variables.values() if
  859. v.is_imported != True and v.is_public != False]
  860. elif not private:
  861. return [v for v in self.variables.values() if
  862. v.is_public != False]
  863. elif not imports:
  864. return [v for v in self.variables.values() if
  865. v.is_imported != True]
  866. assert 0, 'this line should be unreachable'
  867. def init_sorted_variables(self):
  868. """
  869. Initialize the L{sorted_variables} attribute, based on the
  870. L{variables} and L{sort_spec} attributes. This should usually
  871. be called after all variables have been added to C{variables}
  872. (including any inherited variables for classes).
  873. """
  874. unsorted = self.variables.copy()
  875. self.sorted_variables = []
  876. # Add any variables that are listed in sort_spec
  877. if self.sort_spec is not UNKNOWN:
  878. unused_idents = set(self.sort_spec)
  879. for ident in self.sort_spec:
  880. if ident in unsorted:
  881. self.sorted_variables.append(unsorted.pop(ident))
  882. unused_idents.discard(ident)
  883. elif '*' in ident:
  884. regexp = re.compile('^%s$' % ident.replace('*', '(.*)'))
  885. # sort within matching group?
  886. for name, var_doc in unsorted.items():
  887. if regexp.match(name):
  888. self.sorted_variables.append(unsorted.pop(name))
  889. unused_idents.discard(ident)
  890. for ident in unused_idents:
  891. if ident not in ['__all__', '__docformat__', '__path__']:
  892. log.warning("@sort: %s.%s not found" %
  893. (self.canonical_name, ident))
  894. # Add any remaining variables in alphabetical order.
  895. var_docs = unsorted.items()
  896. var_docs.sort()
  897. for name, var_doc in var_docs:
  898. self.sorted_variables.append(var_doc)
  899. def init_variable_groups(self):
  900. """
  901. Initialize the L{variable_groups} attribute, based on the
  902. L{sorted_variables} and L{group_specs} attributes.
  903. """
  904. if self.sorted_variables is UNKNOWN:
  905. self.init_sorted_variables()
  906. assert len(self.sorted_variables) == len(self.variables)
  907. elts = [(v.name, v) for v in self.sorted_variables]
  908. self._unused_groups = dict([(n,set(i)) for (n,i) in self.group_specs])
  909. self.variable_groups = self._init_grouping(elts)
  910. def group_names(self):
  911. """
  912. Return a list of the group names defined by this namespace, in
  913. the order in which they should be listed, with no duplicates.
  914. """
  915. name_list = ['']
  916. name_set = set()
  917. for name, spec in self.group_specs:
  918. if name not in name_set:
  919. name_set.add(name)
  920. name_list.append(name)
  921. return name_list
  922. def _init_grouping(self, elts):
  923. """
  924. Divide a given a list of APIDoc objects into groups, as
  925. specified by L{self.group_specs}.
  926. @param elts: A list of tuples C{(name, apidoc)}.
  927. @return: A list of tuples C{(groupname, elts)}, where
  928. C{groupname} is the name of a group and C{elts} is a list of
  929. C{APIDoc}s in that group. The first tuple has name C{''}, and
  930. is used for ungrouped elements. The remaining tuples are
  931. listed in the order that they appear in C{self.group_specs}.
  932. Within each tuple, the elements are listed in the order that
  933. they appear in C{api_docs}.
  934. """
  935. # Make the common case fast.
  936. if len(self.group_specs) == 0:
  937. return {'': [elt[1] for elt in elts]}
  938. ungrouped = set([elt_doc for (elt_name, elt_doc) in elts])
  939. ungrouped = dict(elts)
  940. groups = {}
  941. for elt_name, elt_doc in elts:
  942. for (group_name, idents) in self.group_specs:
  943. group = groups.setdefault(group_name, [])
  944. unused_groups = self._unused_groups[group_name]
  945. for ident in idents:
  946. if re.match('^%s$' % ident.replace('*', '(.*)'), elt_name):
  947. unused_groups.discard(ident)
  948. if elt_name in ungrouped:
  949. group.append(ungrouped.pop(elt_name))
  950. else:
  951. log.warning("%s.%s in multiple groups" %
  952. (self.canonical_name, elt_name))
  953. # Convert ungrouped from an unordered set to an ordered list.
  954. groups[''] = [elt_doc for (elt_name, elt_doc) in elts
  955. if elt_name in ungrouped]
  956. return groups
  957. def report_unused_groups(self):
  958. """
  959. Issue a warning for any @group items that were not used by
  960. L{_init_grouping()}.
  961. """
  962. for (group, unused_idents) in self._unused_groups.items():
  963. for ident in unused_idents:
  964. log.warning("@group %s: %s.%s not found" %
  965. (group, self.canonical_name, ident))
  966. class ModuleDoc(NamespaceDoc):
  967. """
  968. API documentation information about a single module.
  969. """
  970. #{ Information about the Module
  971. filename = UNKNOWN
  972. """@ivar: The name of the file that defines the module.
  973. @type: C{string}"""
  974. docformat = UNKNOWN
  975. """@ivar: The markup language used by docstrings in this module.
  976. @type: C{string}"""
  977. #{ Information about Submodules
  978. submodules = UNKNOWN
  979. """@ivar: Modules contained by this module (if this module
  980. is a package). (Note: on rare occasions, a module may have a
  981. submodule that is shadowed by a variable with the same name.)
  982. @type: C{list} of L{ModuleDoc}"""
  983. submodule_groups = UNKNOWN
  984. """@ivar: A dictionary specifying what group each
  985. submodule belongs to. The keys of the dictionary are group
  986. names, and the values are lists of C{ModuleDoc}s. The order
  987. that groups should be listed in should be taken from
  988. L{group_specs}.
  989. @type: C{dict} from C{str} to C{list} of L{ModuleDoc}"""
  990. #{ Information about Packages
  991. package = UNKNOWN
  992. """@ivar: API documentation for the module's containing package.
  993. @type: L{ModuleDoc}"""
  994. is_package = UNKNOWN
  995. """@ivar: True if this C{ModuleDoc} describes a package.
  996. @type: C{bool}"""
  997. path = UNKNOWN
  998. """@ivar: If this C{ModuleDoc} describes a package, then C{path}
  999. contains a list of directories that constitute its path (i.e.,
  1000. the value of its C{__path__} variable).
  1001. @type: C{list} of C{str}"""
  1002. #{ Information about Imported Variables
  1003. imports = UNKNOWN
  1004. """@ivar: A list of the source names of variables imported into
  1005. this module. This is used to construct import graphs.
  1006. @type: C{list} of L{DottedName}"""
  1007. #}
  1008. def apidoc_links(self, **filters):
  1009. val_docs = NamespaceDoc.apidoc_links(self, **filters)
  1010. if (filters.get('packages', True) and
  1011. self.package not in (None, UNKNOWN)):
  1012. val_docs.append(self.package)
  1013. if (filters.get('submodules', True) and
  1014. self.submodules not in (None, UNKNOWN)):
  1015. val_docs += self.submodules
  1016. return val_docs
  1017. def init_submodule_groups(self):
  1018. """
  1019. Initialize the L{submodule_groups} attribute, based on the
  1020. L{submodules} and L{group_specs} attributes.
  1021. """
  1022. if self.submodules in (None, UNKNOWN):
  1023. return
  1024. self.submodules = sorted(self.submodules,
  1025. key=lambda m:m.canonical_name)
  1026. elts = [(m.canonical_name[-1], m) for m in self.submodules]
  1027. self.submodule_groups = self._init_grouping(elts)
  1028. def select_variables(self, group=None, value_type=None, public=None,
  1029. imported=None, detailed=None):
  1030. """
  1031. Return a specified subset of this module's L{sorted_variables}
  1032. list. If C{value_type} is given, then only return variables
  1033. whose values have the specified type. If C{group} is given,
  1034. then only return variables that belong to the specified group.
  1035. @require: The L{sorted_variables}, L{variable_groups}, and
  1036. L{submodule_groups} attributes must be initialized before
  1037. this method can be used. See L{init_sorted_variables()},
  1038. L{init_variable_groups()}, and L{init_submodule_groups()}.
  1039. @param value_type: A string specifying the value type for
  1040. which variables should be returned. Valid values are:
  1041. - 'class' - variables whose values are classes or types.
  1042. - 'function' - variables whose values are functions.
  1043. - 'other' - variables whose values are not classes,
  1044. exceptions, types, or functions.
  1045. @type value_type: C{string}
  1046. @param group: The name of the group for which variables should
  1047. be returned. A complete list of the groups defined by
  1048. this C{ModuleDoc} is available in the L{group_names}
  1049. instance variable. The first element of this list is
  1050. always the special group name C{''}, which is used for
  1051. variables that do not belong to any group.
  1052. @type group: C{string}
  1053. @param detailed: If True (False), return only the variables
  1054. deserving (not deserving) a detailed informative box.
  1055. If C{None}, don't care.
  1056. @type detailed: C{bool}
  1057. """
  1058. if (self.sorted_variables is UNKNOWN or
  1059. self.variable_groups is UNKNOWN):
  1060. raise ValueError('sorted_variables and variable_groups '
  1061. 'must be initialized first.')
  1062. if group is None: var_list = self.sorted_variables
  1063. else:
  1064. var_list = self.variable_groups.get(group, self.sorted_variables)
  1065. # Public/private filter (Count UNKNOWN as public)
  1066. if public is True:
  1067. var_list = [v for v in var_list if v.is_public is not False]
  1068. elif public is False:
  1069. var_list = [v for v in var_list if v.is_public is False]
  1070. # Imported filter (Count UNKNOWN as non-imported)
  1071. if imported is True:
  1072. var_list = [v for v in var_list if v.is_imported is True]
  1073. elif imported is False:
  1074. var_list = [v for v in var_list if v.is_imported is not True]
  1075. # Detailed filter
  1076. if detailed is True:
  1077. var_list = [v for v in var_list if v.is_detailed() is True]
  1078. elif detailed is False:
  1079. var_list = [v for v in var_list if v.is_detailed() is not True]
  1080. # [xx] Modules are not currently included in any of these
  1081. # value types.
  1082. if value_type is None:
  1083. return var_list
  1084. elif value_type == 'class':
  1085. return [var_doc for var_doc in var_list
  1086. if (isinstance(var_doc.value, ClassDoc))]
  1087. elif value_type == 'function':
  1088. return [var_doc for var_doc in var_list
  1089. if isinstance(var_doc.value, RoutineDoc)]
  1090. elif value_type == 'other':
  1091. return [var_doc for var_doc in var_list
  1092. if not isinstance(var_doc.value,
  1093. (ClassDoc, RoutineDoc, ModuleDoc))]
  1094. else:
  1095. raise ValueError('Bad value type %r' % value_type)
  1096. class ClassDoc(NamespaceDoc):
  1097. """
  1098. API documentation information about a single class.
  1099. """
  1100. #{ Information about Base Classes
  1101. bases = UNKNOWN
  1102. """@ivar: API documentation for the class's base classes.
  1103. @type: C{list} of L{ClassDoc}"""
  1104. #{ Information about Subclasses
  1105. subclasses = UNKNOWN
  1106. """@ivar: API documentation for the class's known subclasses.
  1107. @type: C{list} of L{ClassDoc}"""
  1108. #}
  1109. def apidoc_links(self, **filters):
  1110. val_docs = NamespaceDoc.apidoc_links(self, **filters)
  1111. if (filters.get('bases', True) and
  1112. self.bases not in (None, UNKNOWN)):
  1113. val_docs += self.bases
  1114. if (filters.get('subclasses', True) and
  1115. self.subclasses not in (None, UNKNOWN)):
  1116. val_docs += self.subclasses
  1117. return val_docs
  1118. def is_type(self):
  1119. if self.canonical_name == DottedName('type'): return True
  1120. if self.bases is UNKNOWN: return False
  1121. for base in self.bases:
  1122. if isinstance(base, ClassDoc) and base.is_type():
  1123. return True
  1124. return False
  1125. def is_exception(self):
  1126. if self.canonical_name == DottedName('Exception'): return True
  1127. if self.bases is UNKNOWN: return False
  1128. for base in self.bases:
  1129. if isinstance(base, ClassDoc) and base.is_exception():
  1130. return True
  1131. return False
  1132. def is_newstyle_class(self):
  1133. if self.canonical_name == DottedName('object'): return True
  1134. if self.bases is UNKNOWN: return False
  1135. for base in self.bases:
  1136. if isinstance(base, ClassDoc) and base.is_newstyle_class():
  1137. return True
  1138. return False
  1139. def mro(self, warn_about_bad_bases=False):
  1140. if self.is_newstyle_class():
  1141. return self._c3_mro(warn_about_bad_bases)
  1142. else:
  1143. return self._dfs_bases([], set(), warn_about_bad_bases)
  1144. def _dfs_bases(self, mro, seen, warn_about_bad_bases):
  1145. if self in seen: return mro
  1146. mro.append(self)
  1147. seen.add(self)
  1148. if self.bases is not UNKNOWN:
  1149. for base in self.bases:
  1150. if isinstance(base, ClassDoc) and base.proxy_for is None:
  1151. base._dfs_bases(mro, seen, warn_about_bad_bases)
  1152. elif warn_about_bad_bases:
  1153. self._report_bad_base(base)
  1154. return mro
  1155. def _c3_mro(self, warn_about_bad_bases):
  1156. """
  1157. Compute the class precedence list (mro) according to C3.
  1158. @seealso: U{http://www.python.org/2.3/mro.html}
  1159. """
  1160. bases = [base for base in self.bases if isinstance(base, ClassDoc)]
  1161. if len(bases) != len(self.bases) and warn_about_bad_bases:
  1162. for base in self.bases:
  1163. if (not isinstance(base, ClassDoc) or
  1164. base.proxy_for is not None):
  1165. self._report_bad_base(base)
  1166. w = [warn_about_bad_bases]*len(bases)
  1167. return self._c3_merge([[self]] + map(ClassDoc._c3_mro, bases, w) +
  1168. [list(bases)])
  1169. def _report_bad_base(self, base):
  1170. if not isinstance(base, ClassDoc):
  1171. if not isinstance(base, GenericValueDoc):
  1172. base_name = base.canonical_name
  1173. elif base.parse_repr is not UNKNOWN:
  1174. base_name = base.parse_repr
  1175. else:
  1176. base_name = '%r' % base
  1177. log.warning("%s's base %s is not a class" %
  1178. (self.canonical_name, base_name))
  1179. elif base.proxy_for is not None:
  1180. log.warning("No information available for %s's base %s" %
  1181. (self.canonical_name, base.proxy_for))
  1182. def _c3_merge(self, seqs):
  1183. """
  1184. Helper function for L{_c3_mro}.
  1185. """
  1186. res = []
  1187. while 1:
  1188. nonemptyseqs=[seq for seq in seqs if seq]
  1189. if not nonemptyseqs: return res
  1190. for seq in nonemptyseqs: # find merge candidates among seq heads
  1191. cand = seq[0]
  1192. nothead=[s for s in nonemptyseqs if cand in s[1:]]
  1193. if nothead: cand=None #reject candidate
  1194. else: break
  1195. if not cand: raise "Inconsistent hierarchy"
  1196. res.append(cand)
  1197. for seq in nonemptyseqs: # remove cand
  1198. if seq[0] == cand: del seq[0]
  1199. def select_variables(self, group=None, value_type=None, inherited=None,
  1200. public=None, imported=None, detailed=None):
  1201. """
  1202. Return a specified subset of this class's L{sorted_variables}
  1203. list. If C{value_type} is given, then only return variables
  1204. whose values have the specified type. If C{group} is given,
  1205. then only return variables that belong to the specified group.
  1206. If C{inherited} is True, then only return inherited variables;
  1207. if C{inherited} is False, then only return local variables.
  1208. @require: The L{sorted_variables} and L{variable_groups}
  1209. attributes must be initialized before this method can be
  1210. used. See L{init_sorted_variables()} and
  1211. L{init_variable_groups()}.
  1212. @param value_type: A string specifying the value type for
  1213. which variables should be returned. Valid values are:
  1214. - 'instancemethod' - variables whose values are
  1215. instance methods.
  1216. - 'classmethod' - variables whose values are class
  1217. methods.
  1218. - 'staticmethod' - variables whose values are static
  1219. methods.
  1220. - 'properties' - variables whose values are properties.
  1221. - 'class' - variables whose values are nested classes
  1222. (including exceptions and types).
  1223. - 'instancevariable' - instance variables. This includes
  1224. any variables that are explicitly marked as instance
  1225. variables with docstring fields; and variables with
  1226. docstrings that are initialized in the constructor.
  1227. - 'classvariable' - class variables. This includes any
  1228. variables that are not included in any of the above
  1229. categories.
  1230. @type value_type: C{string}
  1231. @param group: The name of the group for which variables should
  1232. be returned. A complete list of the groups defined by
  1233. this C{ClassDoc} is available in the L{group_names}
  1234. instance variable. The first element of this list is
  1235. always the special group name C{''}, which is used for
  1236. variables that do not belong to any group.
  1237. @type group: C{string}
  1238. @param inherited: If C{None}, then return both inherited and
  1239. local variables; if C{True}, then return only inherited
  1240. variables; if C{False}, then return only local variables.
  1241. @param detailed: If True (False), return only the variables
  1242. deserving (not deserving) a detailed informative box.
  1243. If C{None}, don't care.
  1244. @type detailed: C{bool}
  1245. """
  1246. if (self.sorted_variables is UNKNOWN or
  1247. self.variable_groups is UNKNOWN):
  1248. raise ValueError('sorted_variables and variable_groups '
  1249. 'must be initialized first.')
  1250. if group is None: var_list = self.sorted_variables
  1251. else: var_list = self.variable_groups[group]
  1252. # Public/private filter (Count UNKNOWN as public)
  1253. if public is True:
  1254. var_list = [v for v in var_list if v.is_public is not False]
  1255. elif public is False:
  1256. var_list = [v for v in var_list if v.is_public is False]
  1257. # Inherited filter (Count UNKNOWN as non-inherited)
  1258. if inherited is None: pass
  1259. elif inherited:
  1260. var_list = [v for v in var_list if v.container != self]
  1261. else:
  1262. var_list = [v for v in var_list if v.container == self ]
  1263. # Imported filter (Count UNKNOWN as non-imported)
  1264. if imported is True:
  1265. var_list = [v for v in var_list if v.is_imported is True]
  1266. elif imported is False:
  1267. var_list = [v for v in var_list if v.is_imported is not True]
  1268. # Detailed filter
  1269. if detailed is True:
  1270. var_list = [v for v in var_list if v.is_detailed() is True]
  1271. elif detailed is False:
  1272. var_list = [v for v in var_list if v.is_detailed() is not True]
  1273. if value_type is None:
  1274. return var_list
  1275. elif value_type == 'method':
  1276. return [var_doc for var_doc in var_list
  1277. if (isinstance(var_doc.value, RoutineDoc) and
  1278. var_doc.is_instvar in (False, UNKNOWN))]
  1279. elif value_type == 'instancemethod':
  1280. return [var_doc for var_doc in var_list
  1281. if (isinstance(var_doc.value, RoutineDoc) and
  1282. not isinstance(var_doc.value, ClassMethodDoc) and
  1283. not isinstance(var_doc.value, StaticMethodDoc) and
  1284. var_doc.is_instvar in (False, UNKNOWN))]
  1285. elif value_type == 'classmethod':
  1286. return [var_doc for var_doc in var_list
  1287. if (isinstance(var_doc.value, ClassMethodDoc) and
  1288. var_doc.is_instvar in (False, UNKNOWN))]
  1289. elif value_type == 'staticmethod':
  1290. return [var_doc for var_doc in var_list
  1291. if (isinstance(var_doc.value, StaticMethodDoc) and
  1292. var_doc.is_instvar in (False, UNKNOWN))]
  1293. elif value_type == 'property':
  1294. return [var_doc for var_doc in var_list
  1295. if (isinstance(var_doc.value, PropertyDoc) and
  1296. var_doc.is_instvar in (False, UNKNOWN))]
  1297. elif value_type == 'class':
  1298. return [var_doc for var_doc in var_list
  1299. if (isinstance(var_doc.value, ClassDoc) and
  1300. var_doc.is_instvar in (False, UNKNOWN))]
  1301. elif value_type == 'instancevariable':
  1302. return [var_doc for var_doc in var_list
  1303. if var_doc.is_instvar is True]
  1304. elif value_type == 'classvariable':
  1305. return [var_doc for var_doc in var_list
  1306. if (var_doc.is_instvar in (False, UNKNOWN) and
  1307. not isinstance(var_doc.value,
  1308. (RoutineDoc, ClassDoc, PropertyDoc)))]
  1309. else:
  1310. raise ValueError('Bad value type %r' % value_type)
  1311. class RoutineDoc(ValueDoc):
  1312. """
  1313. API documentation information about a single routine.
  1314. """
  1315. #{ Signature
  1316. posargs = UNKNOWN
  1317. """@ivar: The names of the routine's positional arguments.
  1318. If an argument list contains \"unpacking\" arguments, then
  1319. their names will be specified using nested lists. E.g., if
  1320. a function's argument list is C{((x1,y1), (x2,y2))}, then
  1321. posargs will be C{[['x1','y1'], ['x2','y2']]}.
  1322. @type: C{list}"""
  1323. posarg_defaults = UNKNOWN
  1324. """@ivar: API documentation for the positional arguments'
  1325. default values. This list has the same length as C{posargs}, and
  1326. each element of C{posarg_defaults} describes the corresponding
  1327. argument in C{posargs}. For positional arguments with no default,
  1328. C{posargs_defaults} will contain None.
  1329. @type: C{list} of C{ValueDoc} or C{None}"""
  1330. vararg = UNKNOWN
  1331. """@ivar: The name of the routine's vararg argument, or C{None} if
  1332. it has no vararg argument.
  1333. @type: C{string} or C{None}"""
  1334. kwarg = UNKNOWN
  1335. """@ivar: The name of the routine's keyword argument, or C{None} if
  1336. it has no keyword argument.
  1337. @type: C{string} or C{None}"""
  1338. lineno = UNKNOWN # used to look up profiling info from pstats.
  1339. """@ivar: The line number of the first line of the function's
  1340. signature. For Python functions, this is equal to
  1341. C{func.func_code.co_firstlineno}. The first line of a file
  1342. is considered line 1.
  1343. @type: C{int}"""
  1344. #} end of "signature" group
  1345. #{ Decorators
  1346. decorators = UNKNOWN
  1347. """@ivar: A list of names of decorators that were applied to this
  1348. routine, in the order that they are listed in the source code.
  1349. (I.e., in the reverse of the order that they were applied in.)
  1350. @type: C{list} of C{string}"""
  1351. #} end of "decorators" group
  1352. #{ Information Extracted from Docstrings
  1353. arg_descrs = UNKNOWN
  1354. """@ivar: A list of descriptions of the routine's
  1355. arguments. Each element of this list is a tuple C{(args,
  1356. descr)}, where C{args} is a list of argument names; and
  1357. C{descr} is a L{ParsedDocstring
  1358. <epydoc.markup.ParsedDocstring>} describing the argument(s)
  1359. specified by C{arg}.
  1360. @type: C{list}"""
  1361. arg_types = UNKNOWN
  1362. """@ivar: Descriptions of the expected types for the
  1363. routine's arguments, encoded as a dictionary mapping from
  1364. argument names to type descriptions.
  1365. @type: C{dict} from C{string} to L{ParsedDocstring
  1366. <epydoc.markup.ParsedDocstring>}"""
  1367. return_descr = UNKNOWN
  1368. """@ivar: A description of the value returned by this routine.
  1369. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  1370. return_type = UNKNOWN
  1371. """@ivar: A description of expected type for the value
  1372. returned by this routine.
  1373. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  1374. exception_descrs = UNKNOWN
  1375. """@ivar: A list of descriptions of exceptions
  1376. that the routine might raise. Each element of this list is a
  1377. tuple C{(exc, descr)}, where C{exc} is a string contianing the
  1378. exception name; and C{descr} is a L{ParsedDocstring
  1379. <epydoc.markup.ParsedDocstring>} describing the circumstances
  1380. under which the exception specified by C{exc} is raised.
  1381. @type: C{list}"""
  1382. #} end of "information extracted from docstrings" group
  1383. callgraph_uid = None
  1384. """@ivar: L{DotGraph}.uid of the call graph for the function.
  1385. @type: C{str}"""
  1386. def is_detailed(self):
  1387. if super(RoutineDoc, self).is_detailed():
  1388. return True
  1389. if self.arg_descrs not in (None, UNKNOWN) and self.arg_descrs:
  1390. return True
  1391. if self.arg_types not in (None, UNKNOWN) and self.arg_types:
  1392. return True
  1393. if self.return_descr not in (None, UNKNOWN):
  1394. return True
  1395. if self.exception_descrs not in (None, UNKNOWN) and self.exception_descrs:
  1396. return True
  1397. if (self.decorators not in (None, UNKNOWN)
  1398. and [ d for d in self.decorators
  1399. if d not in ('classmethod', 'staticmethod') ]):
  1400. return True
  1401. return False
  1402. def all_args(self):
  1403. """
  1404. @return: A list of the names of all arguments (positional,
  1405. vararg, and keyword), in order. If a positional argument
  1406. consists of a tuple of names, then that tuple will be
  1407. flattened.
  1408. """
  1409. if self.posargs is UNKNOWN:
  1410. return UNKNOWN
  1411. all_args = _flatten(self.posargs)
  1412. if self.vararg not in (None, UNKNOWN):
  1413. all_args.append(self.vararg)
  1414. if self.kwarg not in (None, UNKNOWN):
  1415. all_args.append(self.kwarg)
  1416. return all_args
  1417. def _flatten(lst, out=None):
  1418. """
  1419. Return a flattened version of C{lst}.
  1420. """
  1421. if out is None: out = []
  1422. for elt in lst:
  1423. if isinstance(elt, (list,tuple)):
  1424. _flatten(elt, out)
  1425. else:
  1426. out.append(elt)
  1427. return out
  1428. class ClassMethodDoc(RoutineDoc): pass
  1429. class StaticMethodDoc(RoutineDoc): pass
  1430. class PropertyDoc(ValueDoc):
  1431. """
  1432. API documentation information about a single property.
  1433. """
  1434. #{ Property Access Functions
  1435. fget = UNKNOWN
  1436. """@ivar: API documentation for the property's get function.
  1437. @type: L{RoutineDoc}"""
  1438. fset = UNKNOWN
  1439. """@ivar: API documentation for the property's set function.
  1440. @type: L{RoutineDoc}"""
  1441. fdel = UNKNOWN
  1442. """@ivar: API documentation for the property's delete function.
  1443. @type: L{RoutineDoc}"""
  1444. #}
  1445. #{ Information Extracted from Docstrings
  1446. type_descr = UNKNOWN
  1447. """@ivar: A description of the property's expected type, extracted
  1448. from its docstring.
  1449. @type: L{ParsedDocstring<epydoc.markup.ParsedDocstring>}"""
  1450. #} end of "information extracted from docstrings" group
  1451. def apidoc_links(self, **filters):
  1452. val_docs = []
  1453. if self.fget not in (None, UNKNOWN): val_docs.append(self.fget)
  1454. if self.fset not in (None, UNKNOWN): val_docs.append(self.fset)
  1455. if self.fdel not in (None, UNKNOWN): val_docs.append(self.fdel)
  1456. return val_docs
  1457. def is_detailed(self):
  1458. if super(PropertyDoc, self).is_detailed():
  1459. return True
  1460. if self.fget not in (None, UNKNOWN) and self.fget.pyval is not None:
  1461. return True
  1462. if self.fset not in (None, UNKNOWN) and self.fset.pyval is not None:
  1463. return True
  1464. if self.fdel not in (None, UNKNOWN) and self.fdel.pyval is not None:
  1465. return True
  1466. return False
  1467. ######################################################################
  1468. ## Index
  1469. ######################################################################
  1470. class DocIndex:
  1471. """
  1472. [xx] out of date.
  1473. An index that .. hmm... it *can't* be used to access some things,
  1474. cuz they're not at the root level. Do I want to add them or what?
  1475. And if so, then I have a sort of a new top level. hmm.. so
  1476. basically the question is what to do with a name that's not in the
  1477. root var's name space. 2 types:
  1478. - entirely outside (eg os.path)
  1479. - inside but not known (eg a submodule that we didn't look at?)
  1480. - container of current thing not examined?
  1481. An index of all the C{APIDoc} objects that can be reached from a
  1482. root set of C{ValueDoc}s.
  1483. The members of this index can be accessed by dotted name. In
  1484. particular, C{DocIndex} defines two mappings, accessed via the
  1485. L{get_vardoc()} and L{get_valdoc()} methods, which can be used to
  1486. access C{VariableDoc}s or C{ValueDoc}s respectively by name. (Two
  1487. separate mappings are necessary because a single name can be used
  1488. to refer to both a variable and to the value contained by that
  1489. variable.)
  1490. Additionally, the index defines two sets of C{ValueDoc}s:
  1491. \"reachable C{ValueDoc}s\" and \"contained C{ValueDoc}s\". The
  1492. X{reachable C{ValueDoc}s} are defined as the set of all
  1493. C{ValueDoc}s that can be reached from the root set by following
  1494. I{any} sequence of pointers to C{ValueDoc}s or C{VariableDoc}s.
  1495. The X{contained C{ValueDoc}s} are defined as the set of all
  1496. C{ValueDoc}s that can be reached from the root set by following
  1497. only the C{ValueDoc} pointers defined by non-imported
  1498. C{VariableDoc}s. For example, if the root set contains a module
  1499. C{m}, then the contained C{ValueDoc}s includes the C{ValueDoc}s
  1500. for any functions, variables, or classes defined in that module,
  1501. as well as methods and variables defined in classes defined in the
  1502. module. The reachable C{ValueDoc}s includes all of those
  1503. C{ValueDoc}s, as well as C{ValueDoc}s for any values imported into
  1504. the module, and base classes for classes defined in the module.
  1505. """
  1506. def __init__(self, root):
  1507. """
  1508. Create a new documentation index, based on the given root set
  1509. of C{ValueDoc}s. If any C{APIDoc}s reachable from the root
  1510. set does not have a canonical name, then it will be assigned
  1511. one. etc.
  1512. @param root: A list of C{ValueDoc}s.
  1513. """
  1514. for apidoc in root:
  1515. if apidoc.canonical_name in (None, UNKNOWN):
  1516. raise ValueError("All APIdocs passed to DocIndexer "
  1517. "must already have canonical names.")
  1518. # Initialize the root items list. We sort them by length in
  1519. # ascending order. (This ensures that variables will shadow
  1520. # submodules when appropriate.)
  1521. # When the elements name is the same, list in alphabetical order:
  1522. # this is needed by the check for duplicates below.
  1523. self.root = sorted(root,
  1524. key=lambda d: (len(d.canonical_name), d.canonical_name))
  1525. """The list of C{ValueDoc}s to document.
  1526. @type: C{list}"""
  1527. # Drop duplicated modules
  1528. # [xx] maybe what causes duplicates should be fixed instead.
  1529. # If fixed, adjust the sort here above: sorting by names will not
  1530. # be required anymore
  1531. i = 1
  1532. while i < len(self.root):
  1533. if self.root[i-1] is self.root[i]:
  1534. del self.root[i]
  1535. else:
  1536. i += 1
  1537. self.mlclasses = self._get_module_classes(self.root)
  1538. """A mapping from class names to L{ClassDoc}. Contains
  1539. classes defined at module level for modules in L{root}
  1540. and which can be used as fallback by L{find()} if looking
  1541. in containing namespaces fails.
  1542. @type: C{dict} from C{str} to L{ClassDoc} or C{list}"""
  1543. self.callers = None
  1544. """A dictionary mapping from C{RoutineDoc}s in this index
  1545. to lists of C{RoutineDoc}s for the routine's callers.
  1546. This dictionary is initialized by calling
  1547. L{read_profiling_info()}.
  1548. @type: C{list} of L{RoutineDoc}"""
  1549. self.callees = None
  1550. """A dictionary mapping from C{RoutineDoc}s in this index
  1551. to lists of C{RoutineDoc}s for the routine's callees.
  1552. This dictionary is initialized by calling
  1553. L{read_profiling_info()}.
  1554. @type: C{list} of L{RoutineDoc}"""
  1555. self._funcid_to_doc = {}
  1556. """A mapping from C{profile} function ids to corresponding
  1557. C{APIDoc} objects. A function id is a tuple of the form
  1558. C{(filename, lineno, funcname)}. This is used to update
  1559. the L{callers} and L{callees} variables."""
  1560. self._container_cache = {}
  1561. """A cache for the L{container()} method, to increase speed."""
  1562. self._get_cache = {}
  1563. """A cache for the L{get_vardoc()} and L{get_valdoc()} methods,
  1564. to increase speed."""
  1565. #////////////////////////////////////////////////////////////
  1566. # Lookup methods
  1567. #////////////////////////////////////////////////////////////
  1568. # [xx]
  1569. # Currently these only work for things reachable from the
  1570. # root... :-/ I might want to change this so that imported
  1571. # values can be accessed even if they're not contained.
  1572. # Also, I might want canonical names to not start with ??
  1573. # if the thing is a top-level imported module..?
  1574. def get_vardoc(self, name):
  1575. """
  1576. Return the C{VariableDoc} with the given name, or C{None} if this
  1577. index does not contain a C{VariableDoc} with the given name.
  1578. """
  1579. var, val = self._get(name)
  1580. return var
  1581. def get_valdoc(self, name):
  1582. """
  1583. Return the C{ValueDoc} with the given name, or C{None} if this
  1584. index does not contain a C{ValueDoc} with the given name.
  1585. """
  1586. var, val = self._get(name)
  1587. return val
  1588. def _get(self, name):
  1589. """
  1590. A helper function that's used to implement L{get_vardoc()}
  1591. and L{get_valdoc()}.
  1592. """
  1593. # Convert name to a DottedName, if necessary.
  1594. if not isinstance(name, DottedName):
  1595. name = DottedName(name)
  1596. # Check if the result is cached.
  1597. val = self._get_cache.get(name)
  1598. if val is not None: return val
  1599. # Look for an element in the root set whose name is a prefix
  1600. # of `name`. If we can't find one, then return None.
  1601. for root_valdoc in self.root:
  1602. if root_valdoc.canonical_name.dominates(name):
  1603. # Starting at the root valdoc, walk down the variable/
  1604. # submodule chain until we find the requested item.
  1605. var_doc = None
  1606. val_doc = root_valdoc
  1607. for identifier in name[len(root_valdoc.canonical_name):]:
  1608. if val_doc is None: break
  1609. var_doc, val_doc = self._get_from(val_doc, identifier)
  1610. else:
  1611. # If we found it, then return.
  1612. if var_doc is not None or val_doc is not None:
  1613. self._get_cache[name] = (var_doc, val_doc)
  1614. return var_doc, val_doc
  1615. # We didn't find it.
  1616. self._get_cache[name] = (None, None)
  1617. return None, None
  1618. def _get_from(self, val_doc, identifier):
  1619. if isinstance(val_doc, NamespaceDoc):
  1620. child_var = val_doc.variables.get(identifier)
  1621. if child_var is not None:
  1622. child_val = child_var.value
  1623. if child_val is UNKNOWN: child_val = None
  1624. return child_var, child_val
  1625. # If that fails, then see if it's a submodule.
  1626. if (isinstance(val_doc, ModuleDoc) and
  1627. val_doc.submodules is not UNKNOWN):
  1628. for submodule in val_doc.submodules:
  1629. if submodule.canonical_name[-1] == identifier:
  1630. var_doc = None
  1631. val_doc = submodule
  1632. if val_doc is UNKNOWN: val_doc = None
  1633. return var_doc, val_doc
  1634. return None, None
  1635. def find(self, name, context):
  1636. """
  1637. Look for an C{APIDoc} named C{name}, relative to C{context}.
  1638. Return the C{APIDoc} if one is found; otherwise, return
  1639. C{None}. C{find} looks in the following places, in order:
  1640. - Function parameters (if one matches, return C{None})
  1641. - All enclosing namespaces, from closest to furthest.
  1642. - If C{name} starts with C{'self'}, then strip it off and
  1643. look for the remaining part of the name using C{find}
  1644. - Builtins
  1645. - Parameter attributes
  1646. - Classes at module level (if the name is not ambiguous)
  1647. @type name: C{str} or L{DottedName}
  1648. @type context: L{APIDoc}
  1649. """
  1650. if isinstance(name, basestring):
  1651. name = re.sub(r'\(.*\)$', '', name.strip())
  1652. if re.match('^([a-zA-Z_]\w*)(\.[a-zA-Z_]\w*)*$', name):
  1653. name = DottedName(name)
  1654. else:
  1655. return None
  1656. elif not isinstance(name, DottedName):
  1657. raise TypeError("'name' should be a string or DottedName")
  1658. if context is None or context.canonical_name is None:
  1659. container_name = []
  1660. else:
  1661. container_name = context.canonical_name
  1662. # Check for the name in all containing namespaces, starting
  1663. # with the closest one.
  1664. for i in range(len(container_name), -1, -1):
  1665. relative_name = container_name[:i]+name
  1666. # Is `name` the absolute name of a documented value?
  1667. # (excepting GenericValueDoc values.)
  1668. val_doc = self.get_valdoc(relative_name)
  1669. if (val_doc is not None and
  1670. not isinstance(val_doc, GenericValueDoc)):
  1671. return val_doc
  1672. # Is `name` the absolute name of a documented variable?
  1673. var_doc = self.get_vardoc(relative_name)
  1674. if var_doc is not None: return var_doc
  1675. # If the name begins with 'self', then try stripping that off
  1676. # and see if we can find the variable.
  1677. if name[0] == 'self':
  1678. doc = self.find('.'.join(name[1:]), context)
  1679. if doc is not None: return doc
  1680. # Is it the name of a builtin?
  1681. if len(name)==1 and hasattr(__builtin__, name[0]):
  1682. return None
  1683. # Is it a parameter's name or an attribute of a parameter?
  1684. if isinstance(context, RoutineDoc):
  1685. all_args = context.all_args()
  1686. if all_args is not UNKNOWN and name[0] in all_args:
  1687. return None
  1688. # Is this an object directly contained by any module?
  1689. doc = self.mlclasses.get(name[-1])
  1690. if isinstance(doc, APIDoc):
  1691. return doc
  1692. elif isinstance(doc, list):
  1693. log.warning("%s is an ambiguous name: it may be %s" % (
  1694. name[-1],
  1695. ", ".join([ "'%s'" % d.canonical_name for d in doc ])))
  1696. # Drop this item so that the warning is reported only once.
  1697. # fail() will fail anyway.
  1698. del self.mlclasses[name[-1]]
  1699. def _get_module_classes(self, docs):
  1700. """
  1701. Gather all the classes defined in a list of modules.
  1702. Very often people refers to classes only by class name,
  1703. even if they are not imported in the namespace. Linking
  1704. to such classes will fail if we look for them only in nested
  1705. namespaces. Allow them to retrieve only by name.
  1706. @param docs: containers of the objects to collect
  1707. @type docs: C{list} of C{APIDoc}
  1708. @return: mapping from objects name to the object(s) with that name
  1709. @rtype: C{dict} from C{str} to L{ClassDoc} or C{list}
  1710. """
  1711. classes = {}
  1712. for doc in docs:
  1713. if not isinstance(doc, ModuleDoc):
  1714. continue
  1715. for var in doc.variables.values():
  1716. if not isinstance(var.value, ClassDoc):
  1717. continue
  1718. val = var.value
  1719. if val in (None, UNKNOWN) or val.defining_module is not doc:
  1720. continue
  1721. if val.canonical_name in (None, UNKNOWN):
  1722. continue
  1723. name = val.canonical_name[-1]
  1724. vals = classes.get(name)
  1725. if vals is None:
  1726. classes[name] = val
  1727. elif not isinstance(vals, list):
  1728. classes[name] = [ vals, val ]
  1729. else:
  1730. vals.append(val)
  1731. return classes
  1732. #////////////////////////////////////////////////////////////
  1733. # etc
  1734. #////////////////////////////////////////////////////////////
  1735. def reachable_valdocs(self, **filters):
  1736. """
  1737. Return a list of all C{ValueDoc}s that can be reached,
  1738. directly or indirectly from this C{DocIndex}'s root set.
  1739. @param filters: A set of filters that can be used to prevent
  1740. C{reachable_valdocs} from following specific link types
  1741. when looking for C{ValueDoc}s that can be reached from the
  1742. root set. See C{APIDoc.apidoc_links} for a more complete
  1743. description.
  1744. """
  1745. return reachable_valdocs(self.root, **filters)
  1746. def container(self, api_doc):
  1747. """
  1748. Return the C{ValueDoc} that contains the given C{APIDoc}, or
  1749. C{None} if its container is not in the index.
  1750. """
  1751. # Check if the result is cached.
  1752. val = self._container_cache.get(api_doc)
  1753. if val is not None: return val
  1754. if isinstance(api_doc, GenericValueDoc):
  1755. self._container_cache[api_doc] = None
  1756. return None # [xx] unknown.
  1757. if isinstance(api_doc, VariableDoc):
  1758. self._container_cache[api_doc] = api_doc.container
  1759. return api_doc.container
  1760. if len(api_doc.canonical_name) == 1:
  1761. self._container_cache[api_doc] = None
  1762. return None
  1763. elif isinstance(api_doc, ModuleDoc) and api_doc.package is not UNKNOWN:
  1764. self._container_cache[api_doc] = api_doc.package
  1765. return api_doc.package
  1766. else:
  1767. parent = self.get_valdoc(api_doc.canonical_name.container())
  1768. self._container_cache[api_doc] = parent
  1769. return parent
  1770. #////////////////////////////////////////////////////////////
  1771. # Profiling information
  1772. #////////////////////////////////////////////////////////////
  1773. def read_profiling_info(self, profile_stats):
  1774. """
  1775. Initialize the L{callers} and L{callees} variables, given a
  1776. C{Stat} object from the C{pstats} module.
  1777. @warning: This method uses undocumented data structures inside
  1778. of C{profile_stats}.
  1779. """
  1780. if self.callers is None: self.callers = {}
  1781. if self.callees is None: self.callees = {}
  1782. # The Stat object encodes functions using `funcid`s, or
  1783. # tuples of (filename, lineno, funcname). Create a mapping
  1784. # from these `funcid`s to `RoutineDoc`s.
  1785. self._update_funcid_to_doc(profile_stats)
  1786. for callee, (cc, nc, tt, ct, callers) in profile_stats.stats.items():
  1787. callee = self._funcid_to_doc.get(callee)
  1788. if callee is None: continue
  1789. for caller in callers:
  1790. caller = self._funcid_to_doc.get(caller)
  1791. if caller is None: continue
  1792. self.callers.setdefault(callee, []).append(caller)
  1793. self.callees.setdefault(caller, []).append(callee)
  1794. def _update_funcid_to_doc(self, profile_stats):
  1795. """
  1796. Update the dictionary mapping from C{pstat.Stat} funciton ids to
  1797. C{RoutineDoc}s. C{pstat.Stat} function ids are tuples of
  1798. C{(filename, lineno, funcname)}.
  1799. """
  1800. # Maps (filename, lineno, funcname) -> RoutineDoc
  1801. for val_doc in self.reachable_valdocs():
  1802. # We only care about routines.
  1803. if not isinstance(val_doc, RoutineDoc): continue
  1804. # Get the filename from the defining module.
  1805. module = val_doc.defining_module
  1806. if module is UNKNOWN or module.filename is UNKNOWN: continue
  1807. # Normalize the filename.
  1808. filename = os.path.abspath(module.filename)
  1809. try: filename = py_src_filename(filename)
  1810. except: pass
  1811. # Look up the stat_func_id
  1812. funcid = (filename, val_doc.lineno, val_doc.canonical_name[-1])
  1813. if funcid in profile_stats.stats:
  1814. self._funcid_to_doc[funcid] = val_doc
  1815. ######################################################################
  1816. ## Pretty Printing
  1817. ######################################################################
  1818. def pp_apidoc(api_doc, doublespace=0, depth=5, exclude=(), include=(),
  1819. backpointers=None):
  1820. """
  1821. @return: A multiline pretty-printed string representation for the
  1822. given C{APIDoc}.
  1823. @param doublespace: If true, then extra lines will be
  1824. inserted to make the output more readable.
  1825. @param depth: The maximum depth that pp_apidoc will descend
  1826. into descendent VarDocs. To put no limit on
  1827. depth, use C{depth=-1}.
  1828. @param exclude: A list of names of attributes whose values should
  1829. not be shown.
  1830. @param backpointers: For internal use.
  1831. """
  1832. pyid = id(api_doc.__dict__)
  1833. if backpointers is None: backpointers = {}
  1834. if (hasattr(api_doc, 'canonical_name') and
  1835. api_doc.canonical_name not in (None, UNKNOWN)):
  1836. name = '%s for %s' % (api_doc.__class__.__name__,
  1837. api_doc.canonical_name)
  1838. elif getattr(api_doc, 'name', None) not in (UNKNOWN, None):
  1839. if (getattr(api_doc, 'container', None) not in (UNKNOWN, None) and
  1840. getattr(api_doc.container, 'canonical_name', None)
  1841. not in (UNKNOWN, None)):
  1842. name ='%s for %s' % (api_doc.__class__.__name__,
  1843. api_doc.container.canonical_name+
  1844. api_doc.name)
  1845. else:
  1846. name = '%s for %s' % (api_doc.__class__.__name__, api_doc.name)
  1847. else:
  1848. name = api_doc.__class__.__name__
  1849. if pyid in backpointers:
  1850. return '%s [%s] (defined above)' % (name, backpointers[pyid])
  1851. if depth == 0:
  1852. if hasattr(api_doc, 'name') and api_doc.name is not None:
  1853. return '%s...' % api_doc.name
  1854. else:
  1855. return '...'
  1856. backpointers[pyid] = len(backpointers)
  1857. s = '%s [%s]' % (name, backpointers[pyid])
  1858. # Only print non-empty fields:
  1859. fields = [field for field in api_doc.__dict__.keys()
  1860. if (field in include or
  1861. (getattr(api_doc, field) is not UNKNOWN
  1862. and field not in exclude))]
  1863. if include:
  1864. fields = [field for field in dir(api_doc)
  1865. if field in include]
  1866. else:
  1867. fields = [field for field in api_doc.__dict__.keys()
  1868. if (getattr(api_doc, field) is not UNKNOWN
  1869. and field not in exclude)]
  1870. fields.sort()
  1871. for field in fields:
  1872. fieldval = getattr(api_doc, field)
  1873. if doublespace: s += '\n |'
  1874. s += '\n +- %s' % field
  1875. if (isinstance(fieldval, types.ListType) and
  1876. len(fieldval)>0 and
  1877. isinstance(fieldval[0], APIDoc)):
  1878. s += _pp_list(api_doc, fieldval, doublespace, depth,
  1879. exclude, include, backpointers,
  1880. (field is fields[-1]))
  1881. elif (isinstance(fieldval, types.DictType) and
  1882. len(fieldval)>0 and
  1883. isinstance(fieldval.values()[0], APIDoc)):
  1884. s += _pp_dict(api_doc, fieldval, doublespace,
  1885. depth, exclude, include, backpointers,
  1886. (field is fields[-1]))
  1887. elif isinstance(fieldval, APIDoc):
  1888. s += _pp_apidoc(api_doc, fieldval, doublespace, depth,
  1889. exclude, include, backpointers,
  1890. (field is fields[-1]))
  1891. else:
  1892. s += ' = ' + _pp_val(api_doc, fieldval, doublespace,
  1893. depth, exclude, include, backpointers)
  1894. return s
  1895. def _pp_list(api_doc, items, doublespace, depth, exclude, include,
  1896. backpointers, is_last):
  1897. line1 = (is_last and ' ') or '|'
  1898. s = ''
  1899. for item in items:
  1900. line2 = ((item is items[-1]) and ' ') or '|'
  1901. joiner = '\n %s %s ' % (line1, line2)
  1902. if doublespace: s += '\n %s |' % line1
  1903. s += '\n %s +- ' % line1
  1904. valstr = _pp_val(api_doc, item, doublespace, depth, exclude, include,
  1905. backpointers)
  1906. s += joiner.join(valstr.split('\n'))
  1907. return s
  1908. def _pp_dict(api_doc, dict, doublespace, depth, exclude, include,
  1909. backpointers, is_last):
  1910. items = dict.items()
  1911. items.sort()
  1912. line1 = (is_last and ' ') or '|'
  1913. s = ''
  1914. for item in items:
  1915. line2 = ((item is items[-1]) and ' ') or '|'
  1916. joiner = '\n %s %s ' % (line1, line2)
  1917. if doublespace: s += '\n %s |' % line1
  1918. s += '\n %s +- ' % line1
  1919. valstr = _pp_val(api_doc, item[1], doublespace, depth, exclude,
  1920. include, backpointers)
  1921. s += joiner.join(('%s => %s' % (item[0], valstr)).split('\n'))
  1922. return s
  1923. def _pp_apidoc(api_doc, val, doublespace, depth, exclude, include,
  1924. backpointers, is_last):
  1925. line1 = (is_last and ' ') or '|'
  1926. s = ''
  1927. if doublespace: s += '\n %s | ' % line1
  1928. s += '\n %s +- ' % line1
  1929. joiner = '\n %s ' % line1
  1930. childstr = pp_apidoc(val, doublespace, depth-1, exclude,
  1931. include, backpointers)
  1932. return s + joiner.join(childstr.split('\n'))
  1933. def _pp_val(api_doc, val, doublespace, depth, exclude, include, backpointers):
  1934. from epydoc import markup
  1935. if isinstance(val, APIDoc):
  1936. return pp_apidoc(val, doublespace, depth-1, exclude,
  1937. include, backpointers)
  1938. elif isinstance(val, markup.ParsedDocstring):
  1939. valrepr = `val.to_plaintext(None)`
  1940. if len(valrepr) < 40: return valrepr
  1941. else: return valrepr[:37]+'...'
  1942. else:
  1943. valrepr = repr(val)
  1944. if len(valrepr) < 40: return valrepr
  1945. else: return valrepr[:37]+'...'