pylintext.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 14 Jun 2012 15:21:12 +0200
changeset 8444 7a861620f64f
parent 8216 99ff746e8de8
child 8696 0bb18407c053
permissions -rw-r--r--
[login] redirect to real instance root if no postlogin_path is provided When not postlogin_path is provided, the login form issue a redirect to "/". The instance root may not be at "/" on the server. Then issuing a redirect to "/" send the user to the wrong location. We now redirect to "." which works fine because the "login" controller a direct children of instance root (http://babar.com/instance/login). All other redirection of the login controller use relative path too and then rely on this relative path from the login controleur to the instance root. This mechanism may be considered fragile and may deserve a proper fix. but this is to be discussed and implemented in another changeset.

"""https://pastebin.logilab.fr/show/860/"""

from logilab.astng import MANAGER, nodes, scoped_nodes
from logilab.astng.builder import ASTNGBuilder

def turn_function_to_class(node):
    """turn a Function node into a Class node (in-place)"""
    node.__class__ = scoped_nodes.Class
    node.bases = ()
    # remove return nodes so that we don't get warned about 'return outside
    # function' by pylint
    for rnode in node.nodes_of_class(nodes.Return):
        rnode.parent.body.remove(rnode)
    # that seems to be enough :)


def cubicweb_transform(module):
    # handle objectify_predicate decorator (and its former name until bw compat
    # is kept). Only look at module level functions, should be enough.
    for assnodes in module.locals.values():
        for node in assnodes:
            if isinstance(node, scoped_nodes.Function) and node.decorators:
                for decorator in node.decorators.nodes:
                    for infered in decorator.infer():
                        if infered.name in ('objectify_predicate', 'objectify_selector'):
                            turn_function_to_class(node)
                            break
                    else:
                        continue
                    break
    # add yams base types into 'yams.buildobjs', astng doesn't grasp globals()
    # magic in there
    if module.name == 'yams.buildobjs':
        from yams import BASE_TYPES
        for etype in BASE_TYPES:
            module.locals[etype] = [scoped_nodes.Class(etype, None)]
    # add data() to uiprops module
    if module.name.split('.')[-1] == 'uiprops':
        fake = ASTNGBuilder(MANAGER).string_build('''
def data(string):
  return u''
''')
        module.locals['data'] = fake.locals['data']

def register(linter):
    """called when loaded by pylint --load-plugins, nothing to do here"""
    MANAGER.register_transformer(cubicweb_transform)