generated from AlgebraicJulia/AlgebraicTemplate.jl
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from AlgebraicJulia/serialization_interface
Generic interface for reading an acset
- Loading branch information
Showing
7 changed files
with
119 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" Read acsets from Microsoft Excel files. | ||
""" | ||
module ExcelACSets | ||
export read_xlsx_acset | ||
|
||
using ...ACSetInterface, ...Schemas, ..ACSetSerialization | ||
|
||
# Data types | ||
############ | ||
|
||
const AbstractMap = Union{AbstractDict,NamedTuple} | ||
|
||
@kwdef struct ExcelTableSpec | ||
sheet::Union{AbstractString,Integer,Missing} = missing | ||
primary_key::Union{Symbol,Missing} = missing | ||
row_range::Union{AbstractUnitRange,Integer,Missing} = missing | ||
column_range::Union{AbstractString,Missing} = missing | ||
column_labels::AbstractMap = (;) | ||
convert::AbstractMap = (;) | ||
end | ||
|
||
@kwdef struct ExcelSpec | ||
tables::AbstractDict{Symbol,ExcelTableSpec} = Dict{Symbol,ExcelTableSpec}() | ||
end | ||
|
||
function ExcelSpec(schema::Schema; tables::AbstractMap=(;), kw...) | ||
table_specs = Dict(ob => ExcelTableSpec(; get(tables, ob, (;))...) | ||
for ob in objects(schema)) | ||
ExcelSpec(; tables=table_specs, kw...) | ||
end | ||
|
||
# Interface | ||
########### | ||
|
||
""" Read acset from an Excel (.xlsx) file. | ||
This is a convenience function that loads the Excel file and then calls | ||
[`read_acset`](@ref). To use this function, the package XLSX.jl must be | ||
installed and imported. | ||
# Arguments | ||
- `cons`: constructor for acset, e.g., the acset type for struct acsets | ||
- `source`: filename or IO stream from which to read Excel file | ||
- `tables=(;)`: dictionary or named tuple mapping object names in acset schema | ||
to Excel table specifications | ||
""" | ||
function read_xlsx_acset(cons, source::Union{AbstractString,IO}; kw...) | ||
read_acset(cons, read_xlsx(source); kw...) | ||
end | ||
|
||
function read_xlsx end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
""" Serializing and deserializing acsets to/from different formats. | ||
""" | ||
module ACSetSerialization | ||
export read_acset, read_acset! | ||
|
||
using Reexport | ||
|
||
include("JSONACSets.jl") | ||
# Interface | ||
########### | ||
|
||
@reexport using .JSONACSets | ||
""" Read/deserialize an acset from an external source. | ||
# Extensions | ||
############ | ||
Supported source types include: | ||
- `AbstractDict`: assumed to be JSON data | ||
- `XLSX.XLSXFile`: Microsoft Excel file (requires XLSX.jl) | ||
# Arguments | ||
- `cons`: constructor for acset, e.g., the type of a struct acset | ||
- `source`: source to read from | ||
""" | ||
function read_acset(cons, source; kw...) | ||
read_acset!(cons(), source; kw...) | ||
end | ||
|
||
function read_xlsx_acset end | ||
""" Mutating variant of [`read_acset`](@ref). | ||
export read_xlsx_acset | ||
# Arguments | ||
- `acset`: acset to write to | ||
- `source`: source to read from | ||
""" | ||
function read_acset! end | ||
|
||
# Serializers | ||
############# | ||
|
||
include("JSONACSets.jl") | ||
include("ExcelACSets.jl") | ||
|
||
@reexport using .JSONACSets | ||
@reexport using .ExcelACSets | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters