Debian Codegraph @

doc/sphinx/attributes.py

raw
1 # -*- coding: utf-8 -*-
2
3 """Generate the attributes.rst and attributes_dev.rst files from the
4 ccData docstring that describes attributes."""
5
6 from __future__ import print_function
7
8 from docs_common import check_cclib
9
10 import cclib
11 check_cclib(cclib)
12
13
14 def generate_attributes():
15 """Generate a string containing a reStructuredText table
16 representation of the ccData docstring, which contains a list of
17 all supported attributes with
18 1. the name of each attribute,
19 2. the text definition of each attribute,
20 3. the attribute's container data type, shape (if relevant), and
21 4. the physical units for each attribute.
22 """
23 lines = []
24
25 # Need to parse the ccData docstring, since only that currently
26 # contains all the information needed for this table.
27 data_doc = cclib.parser.data.ccData.__doc__
28 attributes = [line for line in data_doc.split('\n') if line[:8].strip() == '']
29 attributes = [line for line in attributes if "--" in line]
30
31 # These are the widths of the columns in the table
32 wattr = 20
33 wdesc = 65
34 wunit = 28
35 wtype = 32
36
37 dashes = " "
38 for w in [wattr, wdesc, wunit, wtype]:
39 dashes += "="*(w-1) + " "
40 header = " "
41 header += "Name".ljust(wattr)
42 header += "Description".ljust(wdesc)
43 header += "Units".ljust(wunit)
44 header += "Data type".ljust(wtype)
45 lines.append(dashes)
46 lines.append(header)
47 lines.append(dashes)
48
49 names = []
50 for line in attributes:
51
52 # There is always a double dash after the name.
53 attr, desc = line.strip().split(' -- ')
54 names.append(attr)
55
56 # The type and unit are in parentheses, but these
57 # are not always the only parentheses on the line.
58 other = desc.split('(')[-1]
59 desc = desc[:-len(other)-1].strip()
60 other = other.split(')')[0]
61
62 # Furthermore, the unit is not always there.
63 if "," in other:
64 atype, aunit = other.split(", ")
65 else:
66 atype = other
67 aunit = ''
68
69 # Print the line with columns align to the table. Note that
70 # the description sometimes contain Unicode characters, so
71 # decode-encode when justifying to get the correct length.
72 attr = ("`%s`_" % attr).ljust(wattr)
73 desc = desc.ljust(wdesc)
74 aunit = aunit.ljust(wunit)
75 for i in range(1, 4):
76 atype = atype.replace('[%i]' % i, ' of rank %i' % i)
77 lines.append(" " + attr + desc + aunit + atype)
78
79 lines.append(dashes)
80 lines.append("")
81
82 for n in names:
83 lines.append(".. _`%s`: data_notes.html#%s" % (n, n))
84
85 return "\n".join(lines)
86
87 if __name__ == "__main__":
88 print(generate_attributes())