1- """
2- List of accepted export formats. `AUTOMATIC_FILE_FORMAT` corresponds to
3- a detection from the file name.
4- """
5- @enum (FileFormat, CBF, LP, MOF, MPS, AUTOMATIC_FILE_FORMAT)
6-
7- function _filename_to_format (filename:: String )
8- return if endswith (filename, " .mof.json.gz" ) || endswith (filename, " .mof.json" )
9- MOF
10- elseif endswith (filename, " .cbf.gz" ) || endswith (filename, " .cbf" )
11- CBF
12- elseif endswith (filename, " .mps.gz" ) || endswith (filename, " .mps" )
13- MPS
14- elseif endswith (filename, " .lp.gz" ) || endswith (filename, " .lp" )
15- LP
16- else
17- error (" File type of $(filename) not recognized by JuMP." )
18- end
19- end
20-
21- """
22- List of accepted export compression formats. `AUTOMATIC_FILE_COMPRESSION`
23- corresponds to a detection from the file name.
24- """
25- @enum (FileCompression, NO_FILE_COMPRESSION, BZIP2, GZIP, XZ, AUTOMATIC_FILE_COMPRESSION)
26-
27- function _filename_to_compression (filename:: String )
28- return if endswith (filename, " .bz2" )
29- BZIP2
30- elseif endswith (filename, " .gz" )
31- GZIP
32- elseif endswith (filename, " .xz" )
33- XZ
34- else
35- NO_FILE_COMPRESSION
36- end
37- end
38-
39- function _open (f:: Function , filename:: String , mode:: String ; compression:: FileCompression = AUTOMATIC_FILE_COMPRESSION)
40- if compression == AUTOMATIC_FILE_COMPRESSION
41- compression = _filename_to_compression (filename)
42- end
43-
44- if compression == BZIP2 || compression == GZIP || compression == XZ
45- stream = if compression == BZIP2
46- (mode == " r" ) ? CodecBzip2. Bzip2DecompressorStream : CodecBzip2. Bzip2CompressorStream
47- elseif compression == GZIP
48- (mode == " r" ) ? CodecZlib. GzipDecompressorStream : CodecZlib. GzipCompressorStream
49- else # compression == XZ
50- (mode == " r" ) ? CodecXz. XzDecompressorStream : CodecXz. XzCompressorStream
51- end
52- return open (f, stream, filename, mode)
53- else
54- return open (f, filename, mode)
55- end
56- end
57-
58- function write_to_file (model:: Model , io:: IO , format:: FileFormat ; kwargs... )
59- dest = if format == CBF
60- MathOptFormat. CBF. Model (; kwargs... )
61- elseif format == LP
62- MathOptFormat. LP. Model (; kwargs... )
63- elseif format == MOF
64- MathOptFormat. MOF. Model (; kwargs... )
65- elseif format == MPS
66- MathOptFormat. MPS. Model (; kwargs... )
67- else
68- error (" When passing an IO object to write_to_file, the file format cannot be guessed from the file name." )
69- end
1+ function write_to_file (model:: Model , io:: IO , format:: MathOptFormat.FileFormat ; kwargs... )
2+ @assert format != AUTOMATIC_FILE_FORMAT
3+ dest = MathOptFormat. _file_formats[format]()
704 MOI. copy_to (dest, backend (model))
715 MOI. write_to_file (dest, io)
726 return
@@ -82,12 +16,15 @@ are given by the enum [`FileCompression`](@ref).
8216
8317For keyword options, see [MathOptFormat.jl](https://github.com/odow/MathOptFormat.jl).
8418"""
85- function write_to_file (model:: Model , filename:: String ; format:: FileFormat = AUTOMATIC_FILE_FORMAT, compression:: FileCompression = AUTOMATIC_FILE_COMPRESSION, kwargs... )
19+ function write_to_file (model:: Model , filename:: String ;
20+ format:: MathOptFormat.FileFormat = MathOptFormat. AUTOMATIC_FILE_FORMAT,
21+ compression:: MathOptFormat.FileCompression = AUTOMATIC_FILE_COMPRESSION,
22+ kwargs... )
8623 if format == AUTOMATIC_FILE_FORMAT
8724 format = _filename_to_format (filename)
8825 end
8926
90- _open (filename, " w" , compression= compression) do io
27+ MathOptFormat . gzip_open (filename, " w" , compression= compression) do io
9128 write_to_file (model, io, format; kwargs... )
9229 end
9330 return
0 commit comments