#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Check usage of (published) ScoDoc methods

Quick method: just grep method name in all constant strings from the source code base.

Usage:
    check_zope_usage.py publishedmethods.csv string-constants.txt

publishedmethods.csv : fichier texte, module et un nom de méthode / ligne
                      ZScoUsers	get_user_list
                    comme extrait par zopelistmethods.py

string-constants.txt : les constantes chaines, extraites par extract_code_strings.py
                      "scolars.py"	"</li><li>"

E. Viennet 2021-01-09
"""
from __future__ import print_function

import sys
import glob

methods_filename = sys.argv[1]
constants_filename = sys.argv[2]

with open(methods_filename) as f:
    # module, method_name, signature
    methods = [l.strip().split("\t") for l in f]

print("%d methods" % len(methods))

with open(constants_filename) as f:
    constants = [l[:-1].split("\t")[1] for l in f]

print("%d constants" % len(constants))

# Add JavaScripts
jss = []
for fn in glob.glob("static/js/*.js"):
    jss.append(open(fn).read())

print("%d javascripts" % len(jss))

L = []
for method in methods:
    n = 0
    for c in constants:
        if method[1] in c:
            n += 1
    nj = 0
    for js in jss:
        if method[1] in js:
            nj += 1
    L.append(method + [n, nj])

# Sort by decreasing popularity
L.sort(key=lambda x: (x[-1] + x[-2], x[1]), reverse=True)
print("\n".join(["%s\t%s\t%s\t%d\t%d" % tuple(l) for l in L]))
