# -*- mode: python -*-
# -*- coding: utf-8 -*-

"""List Zope published methods (helps redesign ScoDoc's API).

Launch ScoDoc as follows: (as root)

 /opt/scodoc/bin/zopectl debug 

Then run this file

E. Viennet 2020-01-26
"""
from __future__ import print_function
from types import FunctionType, MethodType
from pprint import pprint as pp
import inspect

from ZScoDoc import ZScoDoc
from ZScolar import ZScolar
from ZNotes import ZNotes
from ZAbsences import ZAbsences
from ZScoUsers import ZScoUsers
from ZEntreprises import ZEntreprises


def get_methods_description(klass):
    D = klass.__dict__
    M = []
    for method_name in D:
        o = D[method_name]
        if method_name[0] != "_" and type(o) == FunctionType and o.__doc__:
            argspec = inspect.getargspec(D[method_name])
            defaults = argspec.defaults or []
            if defaults:
                args = argspec.args[: -len(defaults)]
            else:
                args = argspec.args
            for a in defaults:
                args.append("%s=%s" % (argspec.args[len(args)], repr(a)))
            M.append((method_name, ", ".join(args)))
    M.sort()
    return M


published_by_module = {}
lines = []
for klass in ZScoDoc, ZScolar, ZNotes, ZAbsences, ZEntreprises, ZScoUsers:
    module_name = klass.__name__
    published_by_module[module_name] = []
    M = get_methods_description(klass)
    for m in M:
        published_by_module[module_name].append(m)
        lines.append((module_name, m[0], m[1]))
    print("%s = %d published methods" % (module_name, len(M)))

print("---\nModule \t #methods")
N = 0
for module_name in published_by_module:
    n = len(published_by_module[module_name])
    print(module_name, "\t", n)
    N += n

print("Total: \t ", N)

open("publishedmethods.csv", "w").write("\n".join(["\t".join(l) for l in lines]))
