ClammingPy 1.8

https://sourceforge.net/projects/clamming/

Module clamming

Class ClammingClassParser

Description

Inspect a python class and store relevant information for further doc.

Constructor

Create lists of ClamInfo from a given Python object.

List of public members:

  • obj_clams (ClamInfo): describes the object. It has a name and a docstring.
  • init_clams (ClamInfo): describes the constructor of the object, if the object does.
  • fct_clams (list of ClamInfo): describes all the functions of the object.
Parameters
  • obj: Any class object; its source code must be available.
Raises
  • TypeError: if the object is not a class.
  • TypeError: if the object is a built-in class.
View Source
def __init__(self, obj: Any):
    """Create lists of ClamInfo from a given Python object.

    List of public members:

    - obj_clams (ClamInfo): describes the object. It has a name and a docstring.
    - init_clams (ClamInfo): describes the constructor of the object, if the object does.
    - fct_clams (list of ClamInfo): describes all the functions of the object.

    :param obj: Any class object; its source code must be available.
    :raises TypeError: if the object is not a class.
    :raises TypeError: if the object is a built-in class.

    """
    if isinstance(obj, object) is False:
        raise TypeError('Expected a class object for clamming.')
    try:
        self.__obj_src = textwrap.dedent(inspect.getsource(obj))
        self.__obj = obj
    except TypeError:
        raise TypeError('Expected a class object for clamming but not a built-in one.')
    self.__obj_clams = self._inspect_class()
    self.__init_clams = self._inspect_constructor()
    self.__fct_clams = self._inspect_functions()

Public functions

get_obj_clams

Parse the information of the class.

Returns
  • (ClamInfo) Information of the object.
View Source
def get_obj_clams(self) -> ClamInfo:
    """Parse the information of the class.

        :return: (ClamInfo) Information of the object.

        """
    return self.__obj_clams

get_init_clams

Inspect constructor of the given object.

Returns
  • (ClamInfo) Information of the constructor of the object.
View Source
def get_init_clams(self) -> ClamInfo:
    """Inspect constructor of the given object.

        :return: (ClamInfo) Information of the constructor of the object.

        """
    return self.__init_clams

get_fct_clams

Inspect functions of the given object.

Returns
  • (dict) key=function name, value=ClamInfo()
View Source
def get_fct_clams(self) -> dict:
    """Inspect functions of the given object.

        :return: (dict) key=function name, value=ClamInfo()

        """
    return self.__fct_clams

Private functions

_inspect_class

Inspect constructor of the given object.

View Source
def _inspect_class(self) -> ClamInfo:
    """Inspect constructor of the given object.

        """
    class_name = self.__obj.__name__
    tree = ast.parse(self.__obj_src)
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef):
            return ClamInfo(class_name, [], '', ast.get_docstring(node))
    return ClamInfo(class_name, [], '', None)

_inspect_constructor

Inspect constructor of the given object.

View Source
def _inspect_constructor(self) -> ClamInfo:
    """Inspect constructor of the given object."""
    try:
        init_src = textwrap.dedent(inspect.getsource(self.__obj.__init__))
    except OSError:
        return ClamInfo('')
    except TypeError:
        return ClamInfo('')
    init_tree = ast.parse(textwrap.dedent(init_src))
    init_args = list()
    init_src = ''
    init_doc = None
    for node in ast.walk(init_tree):
        if isinstance(node, ast.FunctionDef):
            init_args = [arg.arg for arg in node.args.args]
            init_src = ast.unparse(node)
            init_doc = ast.get_docstring(node)
            break
    return ClamInfo(node.name, init_args, init_src, init_doc)

_inspect_functions

Inspect the documented functions of the given object.

Returns
  • (dict) key=function name, value=ClamInfo()
View Source
def _inspect_functions(self) -> dict:
    """Inspect the documented functions of the given object.

        :return: (dict) key=function name, value=ClamInfo()

        """
    fct_infos = dict()
    tree = ast.parse(self.__obj_src)
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            name = node.name
            if name in ('__init__', '__new__'):
                continue
            fct_infos[name] = ClamInfo(name, [arg.arg for arg in node.args.args], ast.unparse(node), ast.get_docstring(node))
    return fct_infos