blob: 11a0b6d37cb63531c22fde31c74992ae2b4c6ca7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pyclbr` --- Python class browser support
2==============================================
3
4.. module:: pyclbr
5 :synopsis: Supports information extraction for a Python class browser.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7
8
Christian Heimes81ee3ef2008-05-04 22:42:01 +00009The :mod:`pyclbr` module can be used to determine some limited information
10about the classes, methods and top-level functions defined in a module. The
11information provided is sufficient to implement a traditional three-pane
12class browser. The information is extracted from the source code rather
13than by importing the module, so this module is safe to use with untrusted
14code. This restriction makes it impossible to use this module with modules
15not implemented in Python, including all standard and optional extension
16modules.
Georg Brandl116aa622007-08-15 14:28:22 +000017
18
Georg Brandl18244152009-09-02 20:34:52 +000019.. function:: readmodule(module, path=None)
Georg Brandl116aa622007-08-15 14:28:22 +000020
Christian Heimes81ee3ef2008-05-04 22:42:01 +000021 Read a module and return a dictionary mapping class names to class
22 descriptor objects. The parameter *module* should be the name of a
23 module as a string; it may be the name of a module within a package. The
24 *path* parameter should be a sequence, and is used to augment the value
25 of ``sys.path``, which is used to locate module source code.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27
Georg Brandl18244152009-09-02 20:34:52 +000028.. function:: readmodule_ex(module, path=None)
Georg Brandl116aa622007-08-15 14:28:22 +000029
Christian Heimes81ee3ef2008-05-04 22:42:01 +000030 Like :func:`readmodule`, but the returned dictionary, in addition to
31 mapping class names to class descriptor objects, also maps top-level
32 function names to function descriptor objects. Moreover, if the module
33 being read is a package, the key ``'__path__'`` in the returned
34 dictionary has as its value a list which contains the package search
35 path.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37
38.. _pyclbr-class-objects:
39
Christian Heimes81ee3ef2008-05-04 22:42:01 +000040Class Objects
41-------------
Georg Brandl116aa622007-08-15 14:28:22 +000042
Christian Heimes81ee3ef2008-05-04 22:42:01 +000043The :class:`Class` objects used as values in the dictionary returned by
44:func:`readmodule` and :func:`readmodule_ex` provide the following data
45members:
Georg Brandl116aa622007-08-15 14:28:22 +000046
47
Christian Heimes81ee3ef2008-05-04 22:42:01 +000048.. attribute:: Class.module
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 The name of the module defining the class described by the class descriptor.
51
52
Christian Heimes81ee3ef2008-05-04 22:42:01 +000053.. attribute:: Class.name
Georg Brandl116aa622007-08-15 14:28:22 +000054
55 The name of the class.
56
57
Christian Heimes81ee3ef2008-05-04 22:42:01 +000058.. attribute:: Class.super
Georg Brandl116aa622007-08-15 14:28:22 +000059
Christian Heimes81ee3ef2008-05-04 22:42:01 +000060 A list of :class:`Class` objects which describe the immediate base
61 classes of the class being described. Classes which are named as
62 superclasses but which are not discoverable by :func:`readmodule` are
63 listed as a string with the class name instead of as :class:`Class`
64 objects.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66
Christian Heimes81ee3ef2008-05-04 22:42:01 +000067.. attribute:: Class.methods
Georg Brandl116aa622007-08-15 14:28:22 +000068
69 A dictionary mapping method names to line numbers.
70
71
Christian Heimes81ee3ef2008-05-04 22:42:01 +000072.. attribute:: Class.file
Georg Brandl116aa622007-08-15 14:28:22 +000073
74 Name of the file containing the ``class`` statement defining the class.
75
76
Christian Heimes81ee3ef2008-05-04 22:42:01 +000077.. attribute:: Class.lineno
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 The line number of the ``class`` statement within the file named by
Georg Brandl502d9a52009-07-26 15:02:41 +000080 :attr:`~Class.file`.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
83.. _pyclbr-function-objects:
84
Christian Heimes81ee3ef2008-05-04 22:42:01 +000085Function Objects
86----------------
Georg Brandl116aa622007-08-15 14:28:22 +000087
Christian Heimes81ee3ef2008-05-04 22:42:01 +000088The :class:`Function` objects used as values in the dictionary returned by
Georg Brandl116aa622007-08-15 14:28:22 +000089:func:`readmodule_ex` provide the following data members:
90
91
Christian Heimes81ee3ef2008-05-04 22:42:01 +000092.. attribute:: Function.module
Georg Brandl116aa622007-08-15 14:28:22 +000093
94 The name of the module defining the function described by the function
95 descriptor.
96
97
Christian Heimes81ee3ef2008-05-04 22:42:01 +000098.. attribute:: Function.name
Georg Brandl116aa622007-08-15 14:28:22 +000099
100 The name of the function.
101
102
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000103.. attribute:: Function.file
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 Name of the file containing the ``def`` statement defining the function.
106
107
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000108.. attribute:: Function.lineno
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000110 The line number of the ``def`` statement within the file named by
Georg Brandl502d9a52009-07-26 15:02:41 +0000111 :attr:`~Function.file`.
Georg Brandl116aa622007-08-15 14:28:22 +0000112