ScoDoc/refactor.py

108 lines
2.8 KiB
Python

# -*- coding: UTF-8 -*
"""Outil pour migration ScoDoc 7 => ScoDoc 8
Pour chaque module dans views:
- construire la liste des fonctions définies dans ce module:
get_module_functions
Pour chaque module dans views et dans scodoc:
- remplacer context.xxx par app.views.M.xxx
où M est le module de views définissant xxx
Si xxx n'est pas trouvé, erreur !
"""
from __future__ import print_function
import re
from pprint import pprint as pp
import sys
import types
import click
import flask
import app
from app import create_app, cli, db
from app.auth.models import User, Role, UserRole
from config import Config
from app.views import notes
TYPES_TO_SCAN = {
types.FunctionType,
# types.ClassType,
# types.DictionaryType,
# types.FloatType,
# types.IntType,
# types.ListType,
# types.StringType,
# types.TupleType,
}
def get_module_symbols(module):
"""returns list of symbols (functions and constants) defined in the given module"""
return [
f.__name__
for f in [getattr(module, name) for name in dir(module)]
if (type(f) in TYPES_TO_SCAN)
and ((type(f) != types.FunctionType) or (f.__module__ == module.__name__))
]
# print("\n".join(f.__name__ for f in get_module_functions(notes)))
def scan_views_symbols():
"""Scan modules in app.views and returns
{ }
"""
views_modules = [
getattr(app.views, mod_name)
for mod_name in dir(app.views)
if type(getattr(app.views, mod_name)) == types.ModuleType
]
sym2mod = {} # symbole_name : module
for module in views_modules:
start = "app.views."
assert module.__name__.startswith(start)
module_name = module.__name__[len(start) :]
symbols = set(get_module_symbols(module))
print("%d symbols defined in %s" % (len(symbols), module))
dups = symbols.intersection(sym2mod)
if len(dups):
print("duplicated symbols !")
for dup in dups:
print("%s:\t%s\t%s" % (dup, sym2mod[dup], module_name))
sym2mod.update({s: module_name for s in symbols})
return sym2mod
def replace_context_calls(sourcefilename, sym2mod):
undefined_list = [] # noms de fonctions non présents dans les modules "views"
def repl(m):
funcname = m.group(1)
module = sym2mod.get(funcname, False)
if module:
return module + "." + funcname
else:
undefined_list.append((sourcefilename, funcname))
return m.group(0) # leave unchanged
print("reading %s" % sourcefilename)
source = open(sourcefilename).read()
exp = re.compile(r"context\.([a-zA-Z0-9_]+)")
source2 = exp.sub(repl, source)
return source2, undefined_list
sym2mod = scan_views_symbols()
source2, undefined_list = replace_context_calls("app/scodoc/sco_core.py", sym2mod)