pylintext.py
author Vladimir Popescu <vladimir.popescu@logilab.fr>
Tue, 12 Mar 2013 18:31:15 +0100
changeset 8836 8a57802d40d3
parent 8696 0bb18407c053
child 9311 8833ead6f3e4
permissions -rw-r--r--
[cubicweb/doc] Add tutorial on data import in CubicWeb. This involves creating the "tutorials/dataimport" directory structure under "cubicweb/doc" and, inside the "dataimport" directory, putting several files: - a ResT file containing the tutorial *per se*; this tutorial addresses the following issues: * creating a CubicWeb schema for representing a given data set (here, the Diseasome RDF data, for illustration purposes); * parsing the data; * importing the data, by using several stores: + the ``RQLObjectStore``, ``NoHookRQLObjectStore`` and ``SQLGenObjectStore`` from the ``dataimport`` module in CubicWeb; + the ``MassiveObjectStore`` from the ``dataimport`` module in the ``dataio`` cube. The tutorial also provides timing benchmarks of the various stores. - a set of Python files illustrating the data import, in the context of Diseasome RDF data parsing: * a Diseasome RDF data parse module, * a Diseasome data import module, * a CubicWeb schema for representing Diseasome data.

"""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.itervalues():
        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)