Skip to content

Commit 6991b86

Browse files
committed
Move most of the code to MathOptFormat.
1 parent b82f70c commit 6991b86

File tree

3 files changed

+8
-77
lines changed

3 files changed

+8
-77
lines changed

Project.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ version = "0.20.0"
55

66
[deps]
77
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
8-
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
9-
CodecXz = "ba30903b-d9e8-5048-a5ec-d1f5b0d4b47b"
10-
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
118
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
129
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1310
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/JuMP.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import MathOptInterface
1717
const MOI = MathOptInterface
1818
const MOIU = MOI.Utilities
1919
using MathOptFormat
20-
import CodecBzip2
21-
import CodecXz
22-
import CodecZlib
2320

2421
import Calculus
2522
import DataStructures.OrderedDict

src/file_formats.jl

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,6 @@
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
8317
For 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

Comments
 (0)