Skip to content

Commit 98def6a

Browse files
authored
Merge pull request #79 from dpgrote/add_documented_metaclass
Added metaclass to manipulate the class doc strings
2 parents ad48d99 + 0f13d60 commit 98def6a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

PICMI_Python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ should take the inputs and convert them to the appropriate inputs for the specif
1919
**Warning:**
2020
The standard is still evolving at this point. The most basic components of the standard have been defined and are being refined. New components are being added.
2121

22+
The documentation of the classes is in the numpydoc format - see https://numpydoc.readthedocs.io.
23+
The classes are setup so that the doc strings of the implementation classes will have the picmistandard
24+
doc string prepended, followed by any implementation specific doc string.
2225

2326
# For developers
2427

PICMI_Python/base.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,29 @@ def register_constants(implementation_constants):
3232
def _get_constants():
3333
return _implementation_constants
3434

35-
class _ClassWithInit(object):
35+
36+
class _DocumentedMetaClass(type):
37+
"""This is used as a metaclass that combines the __doc__ of the picmistandard base and of the implementation"""
38+
def __new__(cls, name, bases, attrs):
39+
# "if bases" skips this for the _ClassWithInit (which has no bases)
40+
# "if bases[0].__doc__ is not None" skips this for the picmistandard classes since their bases[0] (i.e. _ClassWithInit)
41+
# has no __doc__.
42+
if bases and bases[0].__doc__ is not None:
43+
implementation_doc = attrs.get('__doc__', '')
44+
if implementation_doc:
45+
# The format of the added string is intentional.
46+
# The double return "\n\n" is needed to start a new section in the documentation.
47+
# Then the four spaces matches the standard level of indentation for doc strings
48+
# (assuming PEP8 formatting).
49+
# The final return "\n" assumes that the implementation doc string begins with a return,
50+
# i.e. a line with only three quotes, """.
51+
attrs['__doc__'] = bases[0].__doc__ + """\n\n Implementation specific documentation\n""" + implementation_doc
52+
else:
53+
attrs['__doc__'] = bases[0].__doc__
54+
return super(_DocumentedMetaClass, cls).__new__(cls, name, bases, attrs)
55+
56+
57+
class _ClassWithInit(metaclass=_DocumentedMetaClass):
3658
def handle_init(self, kw):
3759
# --- Grab all keywords for the current code.
3860
# --- Arguments for other supported codes are ignored.

0 commit comments

Comments
 (0)