def getParserInfoInfoDictionary(info):
    """Return a PT-friendly info dictionary for a parser info object."""
    return {'file': relativizePath(info.file),
            'url': truncateSysPath(info.file).replace('\\', '/'),
            'line': info.line,
            'eline': info.eline,
            'column': info.column,
            'ecolumn': info.ecolumn}
def getViewFactoryData(factory):
    """Squeeze some useful information out of the view factory"""
    info = {'path': None, 'url': None, 'template': None, 'resource': None,
            'referencable': True}

    # Always determine the most basic factory
    # Commonly, factories are wrapped to provide security or location, for
    # example. If those wrappers play nice, then they provide a `factory`
    # attribute, that points to the original factory.
    while hasattr(factory, 'factory'):
        factory = factory.factory

    if getattr(factory, '__name__', '').startswith('SimpleViewClass'):
        # In the case of a SimpleView, the base is really what we are
        # interested in. Usually the first listed class is the interesting one.
        base = factory.__bases__[0]
        info['path'] = base.__module__ + '.' + base.__name__
        info['template'] = relativizePath(factory.index.filename)
        info['template_obj'] = factory.index

    # Basic Type is a factory
    elif isinstance(factory, (six.string_types, float, int, list, tuple)):
        info['referencable'] = False

    elif factory.__module__ is not None:
        if factory.__module__.startswith(BROWSER_DIRECTIVES_MODULE):
            info['path'] = getPythonPath(factory.__bases__[0])
        # XML-RPC view factory, generated during registration
        elif (factory.__module__.startswith(XMLRPC_DIRECTIVES_MODULE)
            # JSON-RPC view factory, generated during registration
            # This is needed for the 3rd party jsonserver implementation
            # TODO: See issue http://www.zope.org/Collectors/Zope3-dev/504, ri
            or factory.__module__.startswith(JSONRPC_DIRECTIVES_MODULE)):
            # Those factories are method publisher and security wrapped
            info['path'] = getPythonPath(factory.__bases__[0].__bases__[0])

    if not info['path'] and not hasattr(factory, '__name__'):
        # A factory that is a class instance; since we cannot reference instances,
        # reference the class.
        info['path'] = getPythonPath(factory.__class__)

    if not info['path']:
        # Either a simple class-based factory, or not. It doesn't
        # matter. We have tried our best; just get the Python path as
        # good as you can.
        info['path'] = getPythonPath(factory)

    if info['referencable']:
        info['url'] = info['path'].replace('.', '/')

    if isinstance(factory, IconViewFactory):
        info['resource'] = factory.rname

    return info
Beispiel #3
0
 def getFileInfo(self):
     """Get the file where the directive was declared."""
     # ZCML directive `info` objects do not have security declarations, so
     # everything is forbidden by default. We need to remove the security
     # proxies in order to get to the data.
     info = removeSecurityProxy(self.context.info)
     if isinstance(info, ParserInfo):
         return {'file': relativizePath(info.file),
                 'line': info.line,
                 'column': info.column,
                 'eline': info.eline,
                 'ecolumn': info.ecolumn}
     return None