| 1 |
=========================================== |
| 2 |
Plan for Enthought API Documentation Tool |
| 3 |
=========================================== |
| 4 |
|
| 5 |
:Author: David Goodger |
| 6 |
:Contact: [email protected] |
| 7 |
:Date: $Date$ |
| 8 |
:Revision: $Revision$ |
| 9 |
:Copyright: 2004 by `Enthought, Inc. <http://www.enthought.com>`_ |
| 10 |
:License: `Enthought License`_ (BSD-style) |
| 11 |
|
| 12 |
.. _Enthought License: https://docutils.sourceforge.io/licenses/enthought.txt |
| 13 |
|
| 14 |
This document should be read in conjunction with the `Enthought API |
| 15 |
Documentation Tool RFP`__ prepared by Janet Swisher. |
| 16 |
|
| 17 |
__ enthought-rfp.html |
| 18 |
|
| 19 |
.. contents:: |
| 20 |
.. sectnum:: |
| 21 |
|
| 22 |
|
| 23 |
Introduction |
| 24 |
============ |
| 25 |
|
| 26 |
In March 2004 at I met Eric Jones, president and CTO of `Enthought, |
| 27 |
Inc.`_, at `PyCon 2004`_ in Washington DC. He told me that Enthought |
| 28 |
was using reStructuredText_ for source code documentation, but they |
| 29 |
had some issues. He asked if I'd be interested in doing some work on |
| 30 |
a customized API documentation tool. Shortly after PyCon, Janet |
| 31 |
Swisher, Enthought's senior technical writer, contacted me to work out |
| 32 |
details. Some email, a trip to Austin in May, and plenty of Texas |
| 33 |
hospitality later, we had a project. This document will record the |
| 34 |
details, milestones, and evolution of the project. |
| 35 |
|
| 36 |
In a nutshell, Enthought is sponsoring the implementation of an open |
| 37 |
source API documentation tool that meets their needs. Fortuitously, |
| 38 |
their needs coincide well with the "Python Source Reader" description |
| 39 |
in `PEP 258`_. In other words, Enthought is funding some significant |
| 40 |
improvements to Docutils, improvements that were planned but never |
| 41 |
implemented due to time and other constraints. The implementation |
| 42 |
will take place gradually over several months, on a part-time basis. |
| 43 |
|
| 44 |
This is an ideal example of cooperation between a corporation and an |
| 45 |
open-source project. The corporation, the project, I personally, and |
| 46 |
the community all benefit. Enthought, whose commitment to open source |
| 47 |
is also evidenced by their sponsorship of SciPy_, benefits by |
| 48 |
obtaining a useful piece of software, much more quickly than would |
| 49 |
have been possible without their support. Docutils benefits directly |
| 50 |
from the implementation of one of its core subsystems. I benefit from |
| 51 |
the funding, which allows me to justify the long hours to my wife and |
| 52 |
family. All the corporations, projects, and individuals that make up |
| 53 |
the community will benefit from the end result, which will be great. |
| 54 |
|
| 55 |
All that's left now is to actually do the work! |
| 56 |
|
| 57 |
.. _PyCon 2004: http://pycon.org/dc2004/ |
| 58 |
.. _reStructuredText: https://docutils.sourceforge.io/rst.html |
| 59 |
.. _SciPy: http://www.scipy.org/ |
| 60 |
|
| 61 |
|
| 62 |
Development Plan |
| 63 |
================ |
| 64 |
|
| 65 |
1. Analyze prior art, most notably Epydoc_ and HappyDoc_, to see how |
| 66 |
they do what they do. I have no desire to reinvent wheels |
| 67 |
unnecessarily. I want to take the best ideas from each tool, |
| 68 |
combined with the outline in `PEP 258`_ (which will evolve), and |
| 69 |
build at least the foundation of the definitive Python |
| 70 |
auto-documentation tool. |
| 71 |
|
| 72 |
.. _Epydoc: http://epydoc.sourceforge.net/ |
| 73 |
.. _HappyDoc: http://happydoc.sourceforge.net/ |
| 74 |
.. _PEP 258: |
| 75 |
https://docutils.sourceforge.io/docs/peps/pep-0258.html#python-source-reader |
| 76 |
|
| 77 |
2. Decide on a base platform. The best way to achieve Enthought's |
| 78 |
goals in a reasonable time frame may be to extend Epydoc or |
| 79 |
HappyDoc. Or it may be necessary to start fresh. |
| 80 |
|
| 81 |
3. Extend the reStructuredText parser. See `Proposed Changes to |
| 82 |
reStructuredText`_ below. |
| 83 |
|
| 84 |
4. Depending on the base platform chosen, build or extend the |
| 85 |
docstring & doc comment extraction tool. This may be the biggest |
| 86 |
part of the project, but I won't be able to break it down into |
| 87 |
details until more is known. |
| 88 |
|
| 89 |
|
| 90 |
Repository |
| 91 |
========== |
| 92 |
|
| 93 |
If possible, all software and documentation files will be stored in |
| 94 |
the Subversion repository of Docutils and/or the base project, which |
| 95 |
are all publicly-available via anonymous pserver access. |
| 96 |
|
| 97 |
The Docutils project is very open about granting Subversion write |
| 98 |
access; so far, everyone who asked has been given access. Any |
| 99 |
Enthought staff member who would like Subversion write access will get |
| 100 |
it. |
| 101 |
|
| 102 |
If either Epydoc or HappyDoc is chosen as the base platform, I will |
| 103 |
ask the project's administrator for CVS access for myself and any |
| 104 |
Enthought staff member who wants it. If sufficient access is not |
| 105 |
granted -- although I doubt that there would be any problem -- we may |
| 106 |
have to begin a fork, which could be hosted on SourceForge, on |
| 107 |
Enthought's Subversion server, or anywhere else deemed appropriate. |
| 108 |
|
| 109 |
|
| 110 |
Copyright & License |
| 111 |
=================== |
| 112 |
|
| 113 |
Most existing Docutils files have been placed in the public domain, as |
| 114 |
follows:: |
| 115 |
|
| 116 |
:Copyright: This document has been placed in the public domain. |
| 117 |
|
| 118 |
This is in conjunction with the "Public Domain Dedication" section of |
| 119 |
COPYING.txt__. |
| 120 |
|
| 121 |
__ https://docutils.sourceforge.io/COPYING.html |
| 122 |
|
| 123 |
The code and documentation originating from Enthought funding will |
| 124 |
have Enthought's copyright and license declaration. While I will try |
| 125 |
to keep Enthought-specific code and documentation separate from the |
| 126 |
existing files, there will inevitably be cases where it makes the most |
| 127 |
sense to extend existing files. |
| 128 |
|
| 129 |
I propose the following: |
| 130 |
|
| 131 |
1. New files related to this Enthought-funded work will be identified |
| 132 |
with the following field-list headers:: |
| 133 |
|
| 134 |
:Copyright: 2004 by Enthought, Inc. |
| 135 |
:License: Enthought License (BSD Style) |
| 136 |
|
| 137 |
The license field text will be linked to the license file itself. |
| 138 |
|
| 139 |
2. For significant or major changes to an existing file (more than 10% |
| 140 |
change), the headers shall change as follows (for example):: |
| 141 |
|
| 142 |
:Copyright: 2001-2004 by David Goodger |
| 143 |
:Copyright: 2004 by Enthought, Inc. |
| 144 |
:License: BSD-style |
| 145 |
|
| 146 |
If the Enthought-funded portion becomes greater than the previously |
| 147 |
existing portion, Enthought's copyright line will be shown first. |
| 148 |
|
| 149 |
3. In cases of insignificant or minor changes to an existing file |
| 150 |
(less than 10% change), the public domain status shall remain |
| 151 |
unchanged. |
| 152 |
|
| 153 |
A section describing all of this will be added to the Docutils |
| 154 |
`COPYING`__ instructions file. |
| 155 |
|
| 156 |
If another project is chosen as the base project, similar changes |
| 157 |
would be made to their files, subject to negotiation. |
| 158 |
|
| 159 |
__ https://docutils.sourceforge.io/COPYING.html |
| 160 |
|
| 161 |
|
| 162 |
Proposed Changes to reStructuredText |
| 163 |
==================================== |
| 164 |
|
| 165 |
Doc Comment Syntax |
| 166 |
------------------ |
| 167 |
|
| 168 |
The "traits" construct is implemented as dictionaries, where |
| 169 |
standalone strings would be Python syntax errors. Therefore traits |
| 170 |
require documentation in comments. We also need a way to |
| 171 |
differentiate between ordinary "internal" comments and documentation |
| 172 |
comments (doc comments). |
| 173 |
|
| 174 |
Javadoc uses the following syntax for doc comments:: |
| 175 |
|
| 176 |
/** |
| 177 |
* The first line of a multi-line doc comment begins with a slash |
| 178 |
* and *two* asterisks. The doc comment ends normally. |
| 179 |
*/ |
| 180 |
|
| 181 |
Python doesn't have multi-line comments; only single-line. A similar |
| 182 |
convention in Python might look like this:: |
| 183 |
|
| 184 |
## |
| 185 |
# The first line of a doc comment begins with *two* hash marks. |
| 186 |
# The doc comment ends with the first non-comment line. |
| 187 |
'data' : AnyValue, |
| 188 |
|
| 189 |
## The double-hash-marks could occur on the first line of text, |
| 190 |
# saving a line in the source. |
| 191 |
'data' : AnyValue, |
| 192 |
|
| 193 |
How to indicate the end of the doc comment? :: |
| 194 |
|
| 195 |
## |
| 196 |
# The first line of a doc comment begins with *two* hash marks. |
| 197 |
# The doc comment ends with the first non-comment line, or another |
| 198 |
# double-hash-mark. |
| 199 |
## |
| 200 |
# This is an ordinary, internal, non-doc comment. |
| 201 |
'data' : AnyValue, |
| 202 |
|
| 203 |
## First line of a doc comment, terse syntax. |
| 204 |
# Second (and last) line. Ends here: ## |
| 205 |
# This is an ordinary, internal, non-doc comment. |
| 206 |
'data' : AnyValue, |
| 207 |
|
| 208 |
Or do we even need to worry about this case? A simple blank line |
| 209 |
could be used:: |
| 210 |
|
| 211 |
## First line of a doc comment, terse syntax. |
| 212 |
# Second (and last) line. Ends with a blank line. |
| 213 |
|
| 214 |
# This is an ordinary, internal, non-doc comment. |
| 215 |
'data' : AnyValue, |
| 216 |
|
| 217 |
Other possibilities:: |
| 218 |
|
| 219 |
#" Instead of double-hash-marks, we could use a hash mark and a |
| 220 |
# quotation mark to begin the doc comment. |
| 221 |
'data' : AnyValue, |
| 222 |
|
| 223 |
## We could require double-hash-marks on every line. This has the |
| 224 |
## added benefit of delimiting the *end* of the doc comment, as |
| 225 |
## well as working well with line wrapping in Emacs |
| 226 |
## ("fill-paragraph" command). |
| 227 |
# Ordinary non-doc comment. |
| 228 |
'data' : AnyValue, |
| 229 |
|
| 230 |
#" A hash mark and a quotation mark on each line looks funny, and |
| 231 |
#" it doesn't work well with line wrapping in Emacs. |
| 232 |
'data' : AnyValue, |
| 233 |
|
| 234 |
These styles (repeated on each line) work well with line wrapping in |
| 235 |
Emacs:: |
| 236 |
|
| 237 |
## #> #| #- #% #! #* |
| 238 |
|
| 239 |
These styles do *not* work well with line wrapping in Emacs:: |
| 240 |
|
| 241 |
#" #' #: #) #. #/ #@ #$ #^ #= #+ #_ #~ |
| 242 |
|
| 243 |
The style of doc comment indicator used could be a runtime, global |
| 244 |
and/or per-module setting. That may add more complexity than it's |
| 245 |
worth though. |
| 246 |
|
| 247 |
|
| 248 |
Recommendation |
| 249 |
`````````````` |
| 250 |
|
| 251 |
I recommend adopting "#*" on every line:: |
| 252 |
|
| 253 |
# This is an ordinary non-doc comment. |
| 254 |
|
| 255 |
#* This is a documentation comment, with an asterisk after the |
| 256 |
#* hash marks on every line. |
| 257 |
'data' : AnyValue, |
| 258 |
|
| 259 |
I initially recommended adopting double-hash-marks:: |
| 260 |
|
| 261 |
# This is an ordinary non-doc comment. |
| 262 |
|
| 263 |
## This is a documentation comment, with double-hash-marks on |
| 264 |
## every line. |
| 265 |
'data' : AnyValue, |
| 266 |
|
| 267 |
But Janet Swisher rightly pointed out that this could collide with |
| 268 |
ordinary comments that are then block-commented. This applies to |
| 269 |
double-hash-marks on the first line only as well. So they're out. |
| 270 |
|
| 271 |
On the other hand, the JavaDoc-comment style ("##" on the first line |
| 272 |
only, "#" after that) is used in Fredrik Lundh's PythonDoc_. It may |
| 273 |
be worthwhile to conform to this syntax, reinforcing it as a standard. |
| 274 |
PythonDoc does not support terse doc comments (text after "##" on the |
| 275 |
first line). |
| 276 |
|
| 277 |
.. _PythonDoc: http://effbot.org/zone/pythondoc.htm |
| 278 |
|
| 279 |
|
| 280 |
Update |
| 281 |
`````` |
| 282 |
|
| 283 |
Enthought's Traits system has switched to a metaclass base, and traits |
| 284 |
are now defined via ordinary attributes. Therefore doc comments are |
| 285 |
no longer absolutely necessary; attribute docstrings will suffice. |
| 286 |
Doc comments may still be desirable though, since they allow |
| 287 |
documentation to precede the thing being documented. |
| 288 |
|
| 289 |
|
| 290 |
Docstring Density & Whitespace Minimization |
| 291 |
------------------------------------------- |
| 292 |
|
| 293 |
One problem with extensively documented classes & functions, is that |
| 294 |
there is a lot of screen space wasted on whitespace. Here's some |
| 295 |
current Enthought code (from lib/cp/fluids/gassmann.py):: |
| 296 |
|
| 297 |
def max_gas(temperature, pressure, api, specific_gravity=.56): |
| 298 |
""" |
| 299 |
Computes the maximum dissolved gas in oil using Batzle and |
| 300 |
Wang (1992). |
| 301 |
|
| 302 |
Parameters |
| 303 |
---------- |
| 304 |
temperature : sequence |
| 305 |
Temperature in degrees Celsius |
| 306 |
pressure : sequence |
| 307 |
Pressure in MPa |
| 308 |
api : sequence |
| 309 |
Stock tank oil API |
| 310 |
specific_gravity : sequence |
| 311 |
Specific gravity of gas at STP, default is .56 |
| 312 |
|
| 313 |
Returns |
| 314 |
------- |
| 315 |
max_gor : sequence |
| 316 |
Maximum dissolved gas in liters/liter |
| 317 |
|
| 318 |
Description |
| 319 |
----------- |
| 320 |
This estimate is based on equations given by Mavko, Mukerji, |
| 321 |
and Dvorkin, (1998, pp. 218-219, or 2003, p. 236) obtained |
| 322 |
originally from Batzle and Wang (1992). |
| 323 |
""" |
| 324 |
code... |
| 325 |
|
| 326 |
The docstring is 24 lines long. |
| 327 |
|
| 328 |
Rather than using subsections, field lists (which exist now) can save |
| 329 |
6 lines:: |
| 330 |
|
| 331 |
def max_gas(temperature, pressure, api, specific_gravity=.56): |
| 332 |
""" |
| 333 |
Computes the maximum dissolved gas in oil using Batzle and |
| 334 |
Wang (1992). |
| 335 |
|
| 336 |
:Parameters: |
| 337 |
temperature : sequence |
| 338 |
Temperature in degrees Celsius |
| 339 |
pressure : sequence |
| 340 |
Pressure in MPa |
| 341 |
api : sequence |
| 342 |
Stock tank oil API |
| 343 |
specific_gravity : sequence |
| 344 |
Specific gravity of gas at STP, default is .56 |
| 345 |
:Returns: |
| 346 |
max_gor : sequence |
| 347 |
Maximum dissolved gas in liters/liter |
| 348 |
:Description: This estimate is based on equations given by |
| 349 |
Mavko, Mukerji, and Dvorkin, (1998, pp. 218-219, or 2003, |
| 350 |
p. 236) obtained originally from Batzle and Wang (1992). |
| 351 |
""" |
| 352 |
code... |
| 353 |
|
| 354 |
As with the "Description" field above, field bodies may begin on the |
| 355 |
same line as the field name, which also saves space. |
| 356 |
|
| 357 |
The output for field lists is typically a table structure. For |
| 358 |
example: |
| 359 |
|
| 360 |
:Parameters: |
| 361 |
temperature : sequence |
| 362 |
Temperature in degrees Celsius |
| 363 |
pressure : sequence |
| 364 |
Pressure in MPa |
| 365 |
api : sequence |
| 366 |
Stock tank oil API |
| 367 |
specific_gravity : sequence |
| 368 |
Specific gravity of gas at STP, default is .56 |
| 369 |
:Returns: |
| 370 |
max_gor : sequence |
| 371 |
Maximum dissolved gas in liters/liter |
| 372 |
:Description: |
| 373 |
This estimate is based on equations given by Mavko, |
| 374 |
Mukerji, and Dvorkin, (1998, pp. 218-219, or 2003, p. 236) |
| 375 |
obtained originally from Batzle and Wang (1992). |
| 376 |
|
| 377 |
But the definition lists describing the parameters and return values |
| 378 |
are still wasteful of space. There are a lot of half-filled lines. |
| 379 |
|
| 380 |
Definition lists are currently defined as:: |
| 381 |
|
| 382 |
term : classifier |
| 383 |
definition |
| 384 |
|
| 385 |
Where the classifier part is optional. Ideas for improvements: |
| 386 |
|
| 387 |
1. We could allow multiple classifiers:: |
| 388 |
|
| 389 |
term : classifier one : two : three ... |
| 390 |
definition |
| 391 |
|
| 392 |
2. We could allow the definition on the same line as the term, using |
| 393 |
some embedded/inline markup: |
| 394 |
|
| 395 |
* "--" could be used, but only in limited and well-known contexts:: |
| 396 |
|
| 397 |
term -- definition |
| 398 |
|
| 399 |
This is the syntax used by StructuredText (one of |
| 400 |
reStructuredText's predecessors). It was not adopted for |
| 401 |
reStructuredText because it is ambiguous -- people often use "--" |
| 402 |
in their text, as I just did. But given a constrained context, |
| 403 |
the ambiguity would be acceptable (or would it?). That context |
| 404 |
would be: in docstrings, within a field list, perhaps only with |
| 405 |
certain well-defined field names (parameters, returns). |
| 406 |
|
| 407 |
* The "constrained context" above isn't really enough to make the |
| 408 |
ambiguity acceptable. Instead, a slightly more verbose but far |
| 409 |
less ambiguous syntax is possible:: |
| 410 |
|
| 411 |
term === definition |
| 412 |
|
| 413 |
This syntax has advantages. Equals signs lend themselves to the |
| 414 |
connotation of "definition". And whereas one or two equals signs |
| 415 |
are commonly used in program code, three equals signs in a row |
| 416 |
have no conflicting meanings that I know of. (Update: there |
| 417 |
*are* uses out there.) |
| 418 |
|
| 419 |
The problem with this approach is that using inline markup for |
| 420 |
structure is inherently ambiguous in reStructuredText. For |
| 421 |
example, writing *about* definition lists would be difficult:: |
| 422 |
|
| 423 |
``term === definition`` is an example of a compact definition list item |
| 424 |
|
| 425 |
The parser checks for structural markup before it does inline |
| 426 |
markup processing. But the "===" should be protected by its inline |
| 427 |
literal context. |
| 428 |
|
| 429 |
3. We could allow the definition on the same line as the term, using |
| 430 |
structural markup. A variation on bullet lists would work well:: |
| 431 |
|
| 432 |
: term :: definition |
| 433 |
: another term :: and a definition that |
| 434 |
wraps across lines |
| 435 |
|
| 436 |
Some ambiguity remains:: |
| 437 |
|
| 438 |
: term ``containing :: double colons`` :: definition |
| 439 |
|
| 440 |
But the likelihood of such cases is negligible, and they can be |
| 441 |
covered in the documentation. |
| 442 |
|
| 443 |
Other possibilities for the definition delimiter include:: |
| 444 |
|
| 445 |
: term : classifier -- definition |
| 446 |
: term : classifier --- definition |
| 447 |
: term : classifier : : definition |
| 448 |
: term : classifier === definition |
| 449 |
|
| 450 |
The third idea currently has the best chance of being adopted and |
| 451 |
implemented. |
| 452 |
|
| 453 |
|
| 454 |
Recommendation |
| 455 |
`````````````` |
| 456 |
|
| 457 |
Combining these ideas, the function definition becomes:: |
| 458 |
|
| 459 |
def max_gas(temperature, pressure, api, specific_gravity=.56): |
| 460 |
""" |
| 461 |
Computes the maximum dissolved gas in oil using Batzle and |
| 462 |
Wang (1992). |
| 463 |
|
| 464 |
:Parameters: |
| 465 |
: temperature : sequence :: Temperature in degrees Celsius |
| 466 |
: pressure : sequence :: Pressure in MPa |
| 467 |
: api : sequence :: Stock tank oil API |
| 468 |
: specific_gravity : sequence :: Specific gravity of gas at |
| 469 |
STP, default is .56 |
| 470 |
:Returns: |
| 471 |
: max_gor : sequence :: Maximum dissolved gas in liters/liter |
| 472 |
:Description: This estimate is based on equations given by |
| 473 |
Mavko, Mukerji, and Dvorkin, (1998, pp. 218-219, or 2003, |
| 474 |
p. 236) obtained originally from Batzle and Wang (1992). |
| 475 |
""" |
| 476 |
code... |
| 477 |
|
| 478 |
The docstring is reduced to 14 lines, from the original 24. For |
| 479 |
longer docstrings with many parameters and return values, the |
| 480 |
difference would be more significant. |