2323 SIGMF_METADATA_EXT ,
2424 SigMFArchive ,
2525)
26- from .error import SigMFAccessError , SigMFConversionError , SigMFError , SigMFFileError
26+ from .error import (
27+ SigMFAccessError ,
28+ SigMFConversionError ,
29+ SigMFError ,
30+ SigMFFileError ,
31+ SigMFFileExistsError ,
32+ )
2733from .utils import dict_merge , get_magic_bytes
2834
2935
@@ -790,16 +796,24 @@ def validate(self):
790796 """
791797 validate .validate (self ._metadata , self .get_schema ())
792798
793- def archive (self , name = None , fileobj = None ):
799+ def archive (self , name = None , fileobj = None , overwrite = False ):
794800 """Dump contents to SigMF archive format.
795801
796802 `name` and `fileobj` are passed to SigMFArchive and are defined there.
797803
798- """
799- archive = SigMFArchive (self , name , fileobj )
804+ Parameters
805+ ----------
806+ name : str, optional
807+ Name of the archive file to create. If None, a temporary file will be created.
808+ fileobj : file-like object, optional
809+ A file-like object to write the archive to. If None, a file will be created at `name`.
810+ overwrite : bool, default False
811+ If False, raise exception if archive file already exists.
812+ """
813+ archive = SigMFArchive (self , name , fileobj , overwrite = overwrite )
800814 return archive .path
801815
802- def tofile (self , file_path , pretty = True , toarchive = False , skip_validate = False ):
816+ def tofile (self , file_path , pretty = True , toarchive = False , skip_validate = False , overwrite = False ):
803817 """
804818 Write metadata file or full archive containing metadata & dataset.
805819
@@ -812,13 +826,21 @@ def tofile(self, file_path, pretty=True, toarchive=False, skip_validate=False):
812826 toarchive : bool, default False
813827 If True will write both dataset & metadata into SigMF archive format as a single `tar` file.
814828 If False will only write metadata to `sigmf-meta`.
829+ skip_validate : bool, default False
830+ Skip validation of metadata before writing.
831+ overwrite : bool, default False
832+ If False, raise exception if output file already exists.
815833 """
816834 if not skip_validate :
817835 self .validate ()
818836 fns = get_sigmf_filenames (file_path )
837+
819838 if toarchive :
820- self .archive (fns ["archive_fn" ])
839+ self .archive (fns ["archive_fn" ], overwrite = overwrite )
821840 else :
841+ # check if metadata file exists
842+ if not overwrite and fns ["meta_fn" ].exists ():
843+ raise SigMFFileExistsError (fns ["meta_fn" ], "Metadata file" )
822844 with open (fns ["meta_fn" ], "w" ) as fp :
823845 self .dump (fp , pretty = pretty )
824846 fp .write ("\n " ) # text files should end in carriage return
@@ -1076,7 +1098,7 @@ def get_collection_field(self, key: str, default=None):
10761098 """
10771099 return self ._metadata [self .COLLECTION_KEY ].get (key , default )
10781100
1079- def tofile (self , file_path , pretty : bool = True ) -> None :
1101+ def tofile (self , file_path , pretty : bool = True , overwrite : bool = False ) -> None :
10801102 """
10811103 Write metadata file
10821104
@@ -1086,8 +1108,15 @@ def tofile(self, file_path, pretty: bool = True) -> None:
10861108 Location to save.
10871109 pretty : bool, default True
10881110 When True will write more human-readable output, otherwise will be flat JSON.
1111+ overwrite : bool, default False
1112+ If False, raise exception if collection file already exists.
10891113 """
10901114 filenames = get_sigmf_filenames (file_path )
1115+
1116+ # check if collection file exists
1117+ if not overwrite and filenames ["collection_fn" ].exists ():
1118+ raise SigMFFileExistsError (filenames ["collection_fn" ], "Collection file" )
1119+
10911120 with open (filenames ["collection_fn" ], "w" ) as handle :
10921121 self .dump (handle , pretty = pretty )
10931122 handle .write ("\n " ) # text files should end in carriage return
0 commit comments