def prepare_link(self, link): """The "link" protocol. Set or update the link of the dataplan. Can be cached. """ self.link = link if link.startswith(self.FILE_PROTOCOL): filepath = link[len(self.FILE_PROTOCOL):] if not path.exists(filepath): Log.fatal("Cannot get content from file: file does not exist") Log.debug(u"Getting content from local file: {}".format(link)) content = "" with open(filepath) as fd: content = fd.read() return self.prepare_source_code_from_cache(content) elif link.startswith(self.HTTP_PROTOCOL) \ or link.startswith(self.HTTPS_PROTOCOL): if not self.no_cache: ok, cache = Cache.read_link(self.link) if ok: Log.debug(u"Getting content from cache: {}".format(link)) return self.prepare_source_code_from_cache(cache) Log.debug(u"Getting content from URL: {}".format(link)) self.open_url() return self.prepare_source_code() Log.fatal(u"Unsupported link protocol: must be file or http(s)")
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)
def test_bad_read(self): success, data = Cache.read_link("not existing link") self.assertFalse(success) self.assertEqual("no cache to read", data)