Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 8 | **Source code:** :source:`Lib/pyclbr.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 12 | The :mod:`pyclbr` module can be used to determine some limited information |
| 13 | about the classes, methods and top-level functions defined in a module. The |
| 14 | information provided is sufficient to implement a traditional three-pane |
| 15 | class browser. The information is extracted from the source code rather |
| 16 | than by importing the module, so this module is safe to use with untrusted |
| 17 | code. This restriction makes it impossible to use this module with modules |
| 18 | not implemented in Python, including all standard and optional extension |
| 19 | modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
| 21 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 22 | .. function:: readmodule(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 24 | Read a module and return a dictionary mapping class names to class |
| 25 | descriptor objects. The parameter *module* should be the name of a |
| 26 | module as a string; it may be the name of a module within a package. The |
| 27 | *path* parameter should be a sequence, and is used to augment the value |
| 28 | of ``sys.path``, which is used to locate module source code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
| 30 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 31 | .. function:: readmodule_ex(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 33 | Like :func:`readmodule`, but the returned dictionary, in addition to |
| 34 | mapping class names to class descriptor objects, also maps top-level |
| 35 | function names to function descriptor objects. Moreover, if the module |
| 36 | being read is a package, the key ``'__path__'`` in the returned |
| 37 | dictionary has as its value a list which contains the package search |
| 38 | path. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | .. _pyclbr-class-objects: |
| 42 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 43 | Class Objects |
| 44 | ------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 46 | The :class:`Class` objects used as values in the dictionary returned by |
| 47 | :func:`readmodule` and :func:`readmodule_ex` provide the following data |
Senthil Kumaran | a6bac95 | 2011-07-04 11:28:30 -0700 | [diff] [blame] | 48 | attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
| 50 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 51 | .. attribute:: Class.module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | The name of the module defining the class described by the class descriptor. |
| 54 | |
| 55 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 56 | .. attribute:: Class.name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
| 58 | The name of the class. |
| 59 | |
| 60 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 61 | .. attribute:: Class.super |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 63 | A list of :class:`Class` objects which describe the immediate base |
| 64 | classes of the class being described. Classes which are named as |
| 65 | superclasses but which are not discoverable by :func:`readmodule` are |
| 66 | listed as a string with the class name instead of as :class:`Class` |
| 67 | objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 69 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 70 | .. attribute:: Class.methods |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | A dictionary mapping method names to line numbers. |
| 73 | |
| 74 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 75 | .. attribute:: Class.file |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | Name of the file containing the ``class`` statement defining the class. |
| 78 | |
| 79 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 80 | .. attribute:: Class.lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | The line number of the ``class`` statement within the file named by |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 83 | :attr:`~Class.file`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | .. _pyclbr-function-objects: |
| 87 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 88 | Function Objects |
| 89 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 91 | The :class:`Function` objects used as values in the dictionary returned by |
Senthil Kumaran | a6bac95 | 2011-07-04 11:28:30 -0700 | [diff] [blame] | 92 | :func:`readmodule_ex` provide the following attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 95 | .. attribute:: Function.module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
| 97 | The name of the module defining the function described by the function |
| 98 | descriptor. |
| 99 | |
| 100 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 101 | .. attribute:: Function.name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | The name of the function. |
| 104 | |
| 105 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 106 | .. attribute:: Function.file |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | Name of the file containing the ``def`` statement defining the function. |
| 109 | |
| 110 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 111 | .. attribute:: Function.lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 113 | The line number of the ``def`` statement within the file named by |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 114 | :attr:`~Function.file`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |