Example #1
0
File: rd.py Project: ltn22/aiocoap
    async def render_get(self, request):
        query = query_split(request)

        candidates = self.common_rd.get_endpoints()

        for search_key, search_value in query.items():
            if search_key in ('page', 'count'):
                continue # filtered last

            if search_value.endswith('*'):
                matches = lambda x, start=search_value[:-1]: x.startswith(start)
            else:
                matches = lambda x: x == search_value

            if search_key in ('if', 'rt'):
                candidates = (c for c in candidates if any(any(matches(x) for x in getattr(r, search_key, '').split()) for r in c.get_based_links().links))
                continue

            if search_key == 'href':
                candidates = (c for c in candidates if
                        matches(c.href) or
                        any(matches(r.href) for r in c.get_based_links().links)
                        )
                continue

            candidates = (c for c in candidates if
                    (search_key in c.registration_parameters and matches(c.registration_parameters[search_key])) or
                    any(_link_matches(r, search_key, matches) for r in c.get_based_links().links)
                    )

        candidates = _paginate(candidates, query)

        result = [c.get_host_link() for c in candidates]

        return link_format_to_message(request, LinkFormat(result))
Example #2
0
    async def render_get(self, request):
        query = query_split(request)

        eps = self.common_rd.get_endpoints()
        candidates = ((e, c) for e in eps for c in e.get_based_links().links)

        for search_key, search_values in query.items():
            if search_key in ('page', 'count'):
                continue  # filtered last

            for search_value in search_values:
                if search_value is not None and search_value.endswith('*'):

                    def matches(x, start=search_value[:-1]):
                        return x.startswith(start)
                else:

                    def matches(x, search_value=search_value):
                        return x == search_value

                if search_key in ('if', 'rt'):

                    def matches(x, original_matches=matches):
                        return any(original_matches(v) for v in x.split())

                if search_key == 'href':
                    candidates = (
                        (e, c)
                        for (e, c) in candidates if matches(c.href) or matches(
                            e.href
                        )  # FIXME: They SHOULD give this as relative as we do, but don't have to
                    )
                    continue

                candidates = (
                    (e, c) for (e, c) in candidates
                    if _link_matches(c, search_key, matches) or (
                        search_key in e.registration_parameters and any(
                            matches(x)
                            for x in e.registration_parameters[search_key])))

        # strip endpoint
        candidates = (c for (e, c) in candidates)

        candidates = _paginate(candidates, query)

        # strip needless anchors
        candidates = [
            Link(l.href, [(k, v) for (k, v) in l.attr_pairs if k != 'anchor'])
            if dict(l.attr_pairs)['anchor'] == urljoin(l.href, '/') else l
            for l in candidates
        ]

        return link_format_to_message(request, LinkFormat(candidates))
Example #3
0
    async def render_get(self, request):
        query = query_split(request)

        eps = self.common_rd.get_endpoints()
        candidates = ((e, c) for e in eps for c in e.get_based_links().links)

        for search_key, search_value in query.items():
            if search_key in ('page', 'count'):
                continue  # filtered last

            # FIXME: maybe we need query_split to turn ?rt=foo&obs into {'rt':
            # 'foo', 'obs': True} to match on obs, and then this needs more
            # type checking
            if search_value.endswith('*'):
                matches = lambda x, start=search_value[:-1]: x.startswith(start
                                                                          )
            else:
                matches = lambda x: x == search_value

            if search_key in ('if', 'rt'):
                candidates = ((e, c) for (e, c) in candidates if any(
                    matches(x)
                    for x in " ".join(getattr(c, search_key, ())).split()))
                continue

            if search_key == 'href':
                candidates = (
                    (e, c) for (e, c) in candidates
                    if matches(urljoin(e.base, c.href)) or matches(
                        e.href
                    )  # FIXME: actually resolved e, but so far we've avoided knowing our own
                )
                continue

            candidates = ((e, c) for (e, c) in candidates
                          if _link_matches(c, search_key, matches) or
                          (search_key in e.registration_parameters
                           and matches(e.registration_parameters[search_key])))

        # strip endpoint
        candidates = (c for (e, c) in candidates)

        candidates = _paginate(candidates, query)

        return link_format_to_message(request, LinkFormat(candidates))