A tip for integrating your new format without changing `FileIO`'s registry

ehgus
Mon, 04 Mar 2024 12:33:08 +0000
Data

I tried to implement a new data format for internal use and share how I register a new data format without altering FileIO’s registry. It’s obvious to someone, but I hope someone finds it helpful.

List of solutions

To integrate your I/O packages to FileIO, you should register your custom file format to FileIO. There are following approaches:
(1) Update registry.jl of FileIO.
(2) Explicitly execute add_format(...) before the use.
(3) Automatically call add_format(...) while importing the custom I/O package.

The first method is what is written in the FileIO’s documentation. For my case, I ignore this solution to prevent “custom FileIO” because my I/O package is for internal use and full request is not an option.

A few people facing the similar problem to me choose the second solution [fileio minimal example,MeshIO.jl at experimental stage]. I made an example code (suppose I made a _TestTemplates.jl package):

using _TestTemplates
using FileIO
detect_test(io) = endswith(io.name,".test>")
add_format(format"TEST",detect_test,[".test"], [:_TestTemplates])

save("test.test","Hello, world")
load("test.test") # return "Hello, world"

However, this should be done manually and tedious. I want to hide the registration lines inside package.

I suggest to use __init__ in the package :

# In the middle of package
function __init__()
    detect_test(io) = endswith(io.name,".test>")
    add_format(format"TEST",detect_test,[".test"], [:_TestTemplates])
end

Then the following code works:

using _TestTemplates
using FileIO

save("test.test","Hello, world")
load("test.test") # return "Hello, world"

I made a toy example in github.

1 post - 1 participant

Read full topic