From 47bb4ccad7b772fd504f6b611008086d31d051cf Mon Sep 17 00:00:00 2001 From: Olivier Jolly Date: Mon, 1 Feb 2016 17:59:37 +0100 Subject: [PATCH] Prevent accidental overwrite of destination xrni --- rnsutils/instrument.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rnsutils/instrument.py b/rnsutils/instrument.py index f1c6b52..89e0982 100644 --- a/rnsutils/instrument.py +++ b/rnsutils/instrument.py @@ -1,9 +1,11 @@ +import logging import math import pkgutil import pprint from zipfile import ZipFile, ZIP_DEFLATED import io +import os from lxml import etree, objectify from lxml.objectify import ObjectifiedElement @@ -139,11 +141,15 @@ class RenoiseInstrument(object): self.sample_data = [z.read(sample_filename) for sample_filename in sorted(z.namelist()) if sample_filename.startswith('SampleData')] - def save(self, filename, cleanup=True): + def save(self, filename, overwrite=False, cleanup=True): if cleanup: self.cleanup() + if os.path.isfile(filename) and not overwrite: + logging.error("Destination file %s exists and overwrite was not forced", filename) + return + with ZipFile(filename, 'w', compression=ZIP_DEFLATED) as z: objectify.deannotate(self.root, cleanup_namespaces=True, xsi_nil=True) z.writestr("Instrument.xml", etree.tostring(self.root, pretty_print=True))