PageRenderTime 30ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file