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 | |
| 8 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 9 | The :mod:`pyclbr` module can be used to determine some limited information |
| 10 | about the classes, methods and top-level functions defined in a module. The |
| 11 | information provided is sufficient to implement a traditional three-pane |
| 12 | class browser. The information is extracted from the source code rather |
| 13 | than by importing the module, so this module is safe to use with untrusted |
| 14 | code. This restriction makes it impossible to use this module with modules |
| 15 | not implemented in Python, including all standard and optional extension |
| 16 | modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
| 18 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 19 | .. function:: readmodule(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 21 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 28 | .. function:: readmodule_ex(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 30 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | .. _pyclbr-class-objects: |
| 39 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 40 | Class Objects |
| 41 | ------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 43 | The :class:`Class` objects used as values in the dictionary returned by |
| 44 | :func:`readmodule` and :func:`readmodule_ex` provide the following data |
| 45 | members: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
| 47 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 48 | .. attribute:: Class.module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
| 50 | The name of the module defining the class described by the class descriptor. |
| 51 | |
| 52 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 53 | .. attribute:: Class.name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | The name of the class. |
| 56 | |
| 57 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 58 | .. attribute:: Class.super |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 60 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
| 66 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 67 | .. attribute:: Class.methods |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 69 | A dictionary mapping method names to line numbers. |
| 70 | |
| 71 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 72 | .. attribute:: Class.file |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | Name of the file containing the ``class`` statement defining the class. |
| 75 | |
| 76 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 77 | .. attribute:: Class.lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | The line number of the ``class`` statement within the file named by |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 80 | :attr:`~Class.file`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. _pyclbr-function-objects: |
| 84 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 85 | Function Objects |
| 86 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 88 | The :class:`Function` objects used as values in the dictionary returned by |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | :func:`readmodule_ex` provide the following data members: |
| 90 | |
| 91 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 92 | .. attribute:: Function.module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | The name of the module defining the function described by the function |
| 95 | descriptor. |
| 96 | |
| 97 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 98 | .. attribute:: Function.name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
| 100 | The name of the function. |
| 101 | |
| 102 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 103 | .. attribute:: Function.file |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
| 105 | Name of the file containing the ``def`` statement defining the function. |
| 106 | |
| 107 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 108 | .. attribute:: Function.lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 110 | The line number of the ``def`` statement within the file named by |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 111 | :attr:`~Function.file`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |