Esempio n. 1
0
class Database(object):
    """
    Interface to access a database published as clld app.

    >>> wals = Database('wals.info')
    """
    __resource_map__ = {
        'index': Index,
        'language': Language,
        'parameter': Parameter,
    }

    def __init__(self, host):
        self.host = host
        self.cache = Cache()
        self._dataset = None

    @property
    def dataset(self):
        if self._dataset is None:
            self._dataset = Dataset(self.get(), self, 'dataset')
        return self._dataset

    def resource(self, id, type=None):
        if isinstance(id, string_types) and (id.startswith('/') or id.startswith('http')):
            id = URIRef(self.url(id).as_string())
        assert isinstance(id, URIRef) or type
        if not isinstance(id, URIRef):
            resource_type = self.dataset.resource_types[type]
            id = URIRef(expand(resource_type.uritemplate, dict(id=id)))
        res = self.get(id)
        rtype = get_resource_type(res.content, URIRef(res.canonical_url))
        cls = self.__resource_map__.get(rtype, Resource)
        return cls(res, self, rtype)

    def resources(self, type):
        if not isinstance(type, URIRef):
            type = self.dataset.resource_types[type].uriref
        return self.resource(type)

    def url(self, path='/', **query):
        url = URL(path)
        if not url.host():
            url = url.host(self.host)
        if not url.scheme():
            url = url.scheme('http')
        for k, v in query.items():
            url = url.query_param(k, v)
        return url

    def get(self, url='/', **query):
        _u = self.url(url, **query)
        if _u:
            return self.cache.get(
                self.url(url, **query).as_string(),
                default=None,
                headers={'Accept': 'application/rdf+xml'})

    def table(self, rsc, strip_html=True, **constraints):  # pragma: no cover
        return Table(rsc, self, strip_html=strip_html, **constraints)

    def formats(self, url):
        if isinstance(url, RdfResource):
            url = url.uriref
        for link in self.get(url).links:
            if link['rel'] == 'alternate':
                yield link
Esempio n. 2
0
 def __init__(self, host):
     self.host = host
     self.cache = Cache()
     self._dataset = None
Esempio n. 3
0
    def test_Cache(self):
        from clldclient.cache import Cache

        with patch('clldclient.cache.user_cache_dir', Mock(return_value=self.tmp)):
            with HTTMock(clld):
                cache = Cache()
                r1 = cache.get('http://glottolog.org/resource/languoid/id/stan1295.json')
                self.assertEqual(r1.content['id'], 'stan1295')
                self.assertEqual(r1.canonical_url, 'http://glottolog.org/')
                r2 = cache.get('http://glottolog.org/resource/languoid/id/stan1295.json')
                self.assertEqual(r1.created, r2.created)
                cache.drop()
                r2 = cache.get('http://glottolog.org/resource/languoid/id/stan1295.json')
                self.assertNotEqual(r1.created, r2.created)
                self.assertRaises(KeyError, cache.get, 'http://glottolog.org/unknown')
                self.assertEqual(cache.get('http://glottolog.org/unknown', default=1), 1)
                res = cache.get('http://glottolog.org/resource/languoid/id/stan1295.rdf')
                self.assertEqual(res.mimetype, 'application/rdf+xml')
                self.assertEqual(
                    res.canonical_url,
                    'http://glottolog.org/resource/languoid/id/stan1295.rdf')
                assert hasattr(res.content, 'triples')
                self.assertEqual(res.links, [])
                cached = {r[0]: r[1] for r in cache.stats()}['glottolog.org']
                self.assertEqual(cached, cache.purge(host='glottolog.org'))
                now = datetime.datetime.utcnow()
                time.sleep(0.2)
                cache.get('http://glottolog.org/resource/languoid/id/stan1295.json')
                self.assertEqual(0, cache.purge(before=now, host='glottolog.org'))
                self.assertEqual(1, cache.purge(after=now, host='glottolog.org'))