Debian Codegraph @

docs/dev/pysource.rst

raw
1 .. include:: ../header.rst
2
3 ======================
4 Python Source Reader
5 ======================
6 :Author: David Goodger
7 :Contact: [email protected]
8 :Revision: $Revision: 9906 $
9 :Date: $Date: 2024-08-15 10:43:38 +0200 (Do, 15. Aug 2024) $
10 :Copyright: This document has been placed in the public domain.
11
12 This document explores issues around extracting and processing
13 docstrings from Python modules.
14
15 For definitive element hierarchy details, see the "Python Plaintext
16 Document Interface DTD" XML document type definition, pysource.dtd_
17 (which modifies the generic docutils.dtd_). Descriptions below list
18 'DTD elements' (XML 'generic identifiers' or tag names) corresponding
19 to syntax constructs.
20
21
22 .. contents::
23
24
25 Model
26 =====
27
28 The Python Source Reader ("PySource") model that's evolving in my mind
29 goes something like this:
30
31 1. Extract the docstring/namespace [#]_ tree from the module(s) and/or
32 package(s).
33
34 .. [#] See `Docstring Extractor`_ below.
35
36 2. Run the parser on each docstring in turn, producing a forest of
37 doctrees (per nodes.py).
38
39 3. Join the docstring trees together into a single tree, running
40 transforms:
41
42 - merge hyperlinks
43 - merge namespaces
44 - create various sections like "Module Attributes", "Functions",
45 "Classes", "Class Attributes", etc.; see pysource.dtd_
46 - convert the above special sections to ordinary doctree nodes
47
48 4. Run transforms on the combined doctree. Examples: resolving
49 cross-references/hyperlinks (including interpreted text on Python
50 identifiers); footnote auto-numbering; first field list ->
51 bibliographic elements.
52
53 (Or should step 4's transforms come before step 3?)
54
55 5. Pass the resulting unified tree to the writer/builder.
56
57 I've had trouble reconciling the roles of input parser and output
58 writer with the idea of modes ("readers" or "directors"). Does the
59 mode govern the transformation of the input, the output, or both?
60 Perhaps the mode should be split into two.
61
62 For example, say the source of our input is a Python module. Our
63 "input mode" should be the "Python Source Reader". It discovers (from
64 ``__docformat__``) that the input parser is "reStructuredText". If we
65 want HTML, we'll specify the "HTML" output formatter. But there's a
66 piece missing. What *kind* or *style* of HTML output do we want?
67 PyDoc-style, LibRefMan style, etc. (many people will want to specify
68 and control their own style). Is the output style specific to a
69 particular output format (XML, HTML, etc.)? Is the style specific to
70 the input mode? Or can/should they be independent?
71
72 I envision interaction between the input parser, an "input mode" , and
73 the output formatter. The same intermediate data format would be used
74 between each of these, being transformed as it progresses.
75
76
77 Docstring Extractor
78 ===================
79
80 We need code that scans a parsed Python module, and returns an ordered
81 tree containing the names, docstrings (including attribute and
82 additional docstrings), and additional info (in parentheses below) of
83 all of the following objects:
84
85 - packages
86 - modules
87 - module attributes (+ values)
88 - classes (+ inheritance)
89 - class attributes (+ values)
90 - instance attributes (+ values)
91 - methods (+ formal parameters & defaults)
92 - functions (+ formal parameters & defaults)
93
94 (Extract comments too? For example, comments at the start of a module
95 would be a good place for bibliographic field lists.)
96
97 In order to evaluate interpreted text cross-references, namespaces for
98 each of the above will also be required.
99
100 See python-dev/docstring-develop thread "AST mining", started on
101 2001-08-14.
102
103
104 Interpreted Text
105 ================
106
107 DTD elements: package, module, class, method, function,
108 module_attribute, class_attribute, instance_attribute, variable,
109 parameter, type, exception_class, warning_class.
110
111 To classify identifiers explicitly, the role is given along with the
112 identifier in either prefix or suffix form::
113
114 Use :method:`Keeper.storedata` to store the object's data in
115 `Keeper.data`:instance_attribute:.
116
117 The role may be one of 'package', 'module', 'class', 'method',
118 'function', 'module_attribute', 'class_attribute',
119 'instance_attribute', 'variable', 'parameter', 'type',
120 'exception_class', 'exception', 'warning_class', or 'warning'. Other
121 roles may be defined.
122
123 .. _pysource.dtd: pysource.dtd
124 .. _docutils.dtd: ../ref/docutils.dtd
125
126
127 .. Emacs settings
128
129 Local Variables:
130 mode: indented-text
131 mode: rst
132 indent-tabs-mode: nil
133 fill-column: 70
134 End: