def create(self, req, filename=None, verbose=True, outfile=None): with safe_overwrite(outfile or self.abspath(req)) as tmp: if self.rdf: # we do not create archives with a readme for rdf downloads, because each # RDF entity points to the dataset and the void description of the dataset # covers all relevant metadata. # # TODO: write test for the file name things!? # with contextlib.closing(gzip.GzipFile( filename=pathlib.Path(tmp.stem).stem, fileobj=tmp.open('wb') )) as fp: self.before(req, fp) for i, item in enumerate(page_query(self.query(req), verbose=verbose)): self.dump(req, fp, item, i) self.after(req, fp) else: with zipfile.ZipFile(tmp.as_posix(), 'w', zipfile.ZIP_DEFLATED) as zipf: if not filename: fp = self.get_stream() self.before(req, fp) for i, item in enumerate(page_query(self.query(req), verbose=verbose)): self.dump(req, fp, item, i) self.after(req, fp) zipf.writestr(self.name, self.read_stream(fp)) else: # pragma: no cover zipf.write(str(pathlib.Path(filename)), self.name) zipf.writestr( 'README.txt', format_readme(req, req.db.query(Dataset).first()).encode('utf8'))
def create(self, req, filename=None, verbose=True, outfile=None): with safe_overwrite(outfile or self.abspath(req)) as tmp: with Archive(tmp, 'w') as zipfile: for contrib in self.iterdatasets(): ds = req.registry.getAdapter(contrib, ICldfDataset, 'cldf') ds.write(req, zipfile) zipfile.write_text(format_readme(req), 'README.txt')
def create(self, req, filename=None, verbose=True, outfile=None): with safe_overwrite(outfile or self.abspath(req)) as tmp: if self.rdf: # we do not create archives with a readme for rdf downloads, because each # RDF entity points to the dataset and the void description of the dataset # covers all relevant metadata. # # TODO: write test for the file name things!? # with closing(GzipFile( filename=Path(tmp.stem).stem, fileobj=tmp.open('wb') )) as fp: self.before(req, fp) for i, item in enumerate(page_query(self.query(req), verbose=verbose)): self.dump(req, fp, item, i) self.after(req, fp) else: with ZipFile(tmp.as_posix(), 'w', ZIP_DEFLATED) as zipfile: if not filename: fp = self.get_stream() self.before(req, fp) for i, item in enumerate( page_query(self.query(req), verbose=verbose)): self.dump(req, fp, item, i) self.after(req, fp) zipfile.writestr(self.name, self.read_stream(fp)) else: # pragma: no cover zipfile.write(Path(filename).as_posix(), self.name) zipfile.writestr('README.txt', format_readme(req).encode('utf8'))
def test_safe_overwrite(self): from clld.util import safe_overwrite target = self.tmp_path('a', 'b') with safe_overwrite(target) as tmp: with tmp.open('w', encoding='utf8') as fp: fp.write('stuff') self.assertFalse(tmp.exists()) self.assertTrue(target.exists()) with safe_overwrite(target) as tmp: with tmp.open('w', encoding='utf8') as fp: fp.write('other') with target.open(encoding='utf8') as fp: self.assertEqual(fp.read(), 'other')
def test_safe_overwrite(tmppath): from clld.util import safe_overwrite target = tmppath / 'a' / 'b' with safe_overwrite(target) as tmp: with tmp.open('w', encoding='utf8') as fp: fp.write('stuff') assert not tmp.exists() assert target.exists() with safe_overwrite(target) as tmp: with tmp.open('w', encoding='utf8') as fp: fp.write('other') with target.open(encoding='utf8') as fp: assert fp.read() == 'other'