Example #1
0
        def method(self,
                   method,
                   url,
                   params,
                   body=None,
                   root_path=None,
                   **headers):
            from flask.ctx import AppContext
            from flask import Flask
            import os
            if not root_path:
                root_path = os.path.join(
                    os.path.dirname(os.path.dirname(__file__)),
                    'iommi/',
                )
            app = AppContext(Flask('iommi', root_path=root_path))
            app.push()
            from werkzeug.test import create_environ
            from iommi._web_compat import HttpRequest

            # We use the django style where headers are HTTP_
            for k, v in items(headers):
                assert k.startswith('HTTP_')

            # ...but flask adds HTTP_ itself, so we have to cut them off here
            headers = {k[len('HTTP_'):]: v for k, v in items(headers)}

            return HttpRequest(
                create_environ(path=url,
                               query_string=params,
                               method=method,
                               data=body,
                               headers=headers))
Example #2
0
File: debug.py Project: tltx/iommi
def endpoint__debug_tree(endpoint, **_):
    root = endpoint.iommi_root()
    assert root._is_bound

    def rows(node, name='', path=None):
        if path is None:
            path = []
        is_struct = type(node) is Struct
        is_bound = getattr(node, '_is_bound', False)

        try:
            p = node.iommi_path if is_bound else None
        except PathNotFoundException:
            p = None

        type_name = type(node).__name__ if not is_struct else None
        base_type_name = type_name
        if isinstance(node, Members) and node._declared_members:
            if name == 'parts':
                member_type = 'Part'
            else:
                member_type = type(list(values(declared_members(node)))[0]).__name__
            type_name = f'Members[{member_type}]'

        children = []
        if isinstance(node, dict):
            children = list(node.items())
        elif isinstance(node, Traversable):
            children = [
                (
                    k,
                    node.iommi_bound_members().get(k, v)
                )
                for k, v in items(declared_members(node))
            ]

        if (isinstance(node, Members) or isinstance(node, dict)) and not children:
            return

        yield Struct(
            name=name,
            obj=node,
            type=type_name,
            base_type=base_type_name,
            path=p,
            dunder_path='__'.join(path),
            included=is_bound
        )

        for k, v in children:
            yield from rows(v, name=k, path=path + [k])

    from iommi import (
        Column,
        Table,
    )

    class TreeTable(Table):
        class Meta:
            template = Template("""
                <style>
                    .full-path {
                        opacity: 0.0;
                    }
                    tr:hover .full-path {
                        opacity: 0.6;
                    }
                    
                    tr {
                        opacity: 0.4;
                    }
                    tr.included {
                        opacity: 1;
                    }
                    
                </style>
                
                {% include "iommi/table/table.html" %}            
            """)
            sortable = False
            row__attrs__class__included = (lambda row, **_: row.included)
            page_size = None

        dunder_path = Column(
            cell__value=lambda row, **_: row.dunder_path,
            cell__format=dunder_path__format,
        )
        path = Column()
        type = Column(
            cell__url=lambda row, **_: f'https://docs.iommi.rocks/en/latest/{row.base_type}.html' if row.base_type else None
        )
        included = Column.boolean()

    request = HttpRequest()
    request.method = 'GET'
    if hasattr(root._request, 'user'):
        request.user = root._request.user

    return TreeTable(rows=rows(root)).bind(request=request)