Exemplo n.º 1
0
 def test_write_read(self):
     Cache.directory = ".cache_test"
     link = "http://github.com/lexndru/hap"
     success, _ = Cache.write_link(link, "hap")
     self.assertTrue(success)
     success, data = Cache.read_link(link)
     self.assertTrue(success)
     self.assertEqual(data, "hap")
     rmtree(Cache.directory)
Exemplo n.º 2
0
    def open_url(self):
        """Simple URL reader.

        Access an URL and read it's content. Can be cached.

        If URL returns a non-OK (200) status code, a warning is printed, but if
        it return a non-HTML content-type, a fatal log is set.
        """

        status, source = self.read_url(self.link)
        if not status:
            Log.fatal(u"Cannot reach link: {}".format(source))
        if status and not str(source.code).startswith("2"):
            Log.warn(u"Non-2xx status code: {}".format(source.code))
        if hasattr(source.info(), "gettype"):
            mimetype = source.info().gettype()
            if mimetype not in self.supported_mime_types:
                Log.fatal(u"Unsupported content, got {}".format(mimetype))
        self.source = source.read()
        if not self.no_cache:
            ok, status = Cache.write_link(self.link, self.source)
            if not ok:
                Log.warn(status)
        return self
Exemplo n.º 3
0
 def setUp(self):
     Cache.directory = ".cache_test"
     success, _ = Cache.write_link("http://localhost/mockup", HTMLDATA)
     self.assertTrue(success)