def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert is_fsnative(filename) mkdir(os.path.dirname(filename)) with atomic_save(filename, ".tmp", "wb") as fileobj: self._config.write(fileobj)
def test_basic(self): fd, filename = mkstemp(".cfg") os.close(fd) with open(filename, "wb") as fobj: fobj.write("nope") with open(filename + ".tmp", "wb") as fobj: fobj.write("temp_nope") with util.atomic_save(filename, ".tmp", "wb") as fobj: fobj.write("foo") with open(filename, "rb") as fobj: self.assertEqual(fobj.read(), "foo") self.assertFalse(os.path.exists(filename + ".tmp"))
def dump_items(filename, items): """Pickle items to disk. Doesn't handle exceptions. """ dirname = os.path.dirname(filename) mkdir(dirname) with util.atomic_save(filename, ".tmp", "wb") as fileobj: # While protocol 2 is usually faster it uses __setitem__ # for unpickle and we override it to clear the sort cache. # This roundtrip makes it much slower, so we use protocol 1 # unpickle numbers (py2.7): # 2: 0.66s / 2 + __set_item__: 1.18s / 1 + __set_item__: 0.72s # see: http://bugs.python.org/issue826897 pickle.dump(items, fileobj, 1)
def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert is_fsnative(filename) mkdir(os.path.dirname(filename)) # temporary set the new version for saving if self._version is not None: self.add_section("__config__") self.set("__config__", "version", self._version) try: with atomic_save(filename, ".tmp", "wb") as fileobj: self._config.write(fileobj) finally: if self._loaded_version is not None: self.set("__config__", "version", self._loaded_version)