Skip to content

Commit

Permalink
replace underscore functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Nov 12, 2024
1 parent 5d0f436 commit 31bcdfb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
28 changes: 14 additions & 14 deletions MakieCore/src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,13 @@ plottype(plot_args...) = Plot{plot} # default to dispatch to type recipes!
deprecated_attributes(_) = ()

struct InvalidAttributeError <: Exception
plottype::Type
type::Type
object_name::String # Generic name like plot, block
attributes::Set{Symbol}
end
function InvalidAttributeError(::Type{PT}, attributes::Set{Symbol}) where {PT <: Plot}
return InvalidAttributeError(PT, "plot", attributes)
end

function print_columns(io::IO, v::Vector{String}; gapsize = 2, rows_first = true, cols = displaysize(io)[2])

Expand Down Expand Up @@ -718,26 +722,22 @@ function print_columns(io::IO, v::Vector{String}; gapsize = 2, rows_first = true
return
end

__obj_name(_) = "plot"
__valid_attributes(p) = attribute_names(p)
__has_generic_attributes(_) = true

function Base.showerror(io::IO, i::InvalidAttributeError)
n = length(i.attributes)
function Base.showerror(io::IO, err::InvalidAttributeError)
n = length(err.attributes)
print(io, "Invalid attribute$(n > 1 ? "s" : "") ")
for (j, att) in enumerate(i.attributes)
j > 1 && print(io, j == length(i.attributes) ? " and " : ", ")
for (j, att) in enumerate(err.attributes)
j > 1 && print(io, j == length(err.attributes) ? " and " : ", ")
printstyled(io, att; color = :red, bold = true)
end
print(io, " for $(__obj_name(i.plottype)) type ")
printstyled(io, i.plottype; color = :blue, bold = true)
print(io, " for $(err.object_name) type ")
printstyled(io, err.type; color = :blue, bold = true)
println(io, ".")
nameset = sort(string.(collect(__valid_attributes(i.plottype))))
nameset = sort(string.(collect(attribute_names(err.type))))
println(io)
println(io, "The available $(__obj_name(i.plottype)) attributes for $(i.plottype) are:")
println(io, "The available $(err.object_name) attributes for $(err.type) are:")
println(io)
print_columns(io, nameset; cols = displaysize(stderr)[2], rows_first = true)
if __has_generic_attributes(i.plottype)
if err.type isa Plot
allowlist = attribute_name_allowlist()
println(io)
println(io)
Expand Down
18 changes: 10 additions & 8 deletions src/makielayout/blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,23 @@ function block_defaults(::Type{B}, attribute_kwargs::Dict, scene::Union{Nothing,
return attributes
end

MakieCore.__obj_name(::Type{<:Block}) = "block"
function MakieCore.__valid_attributes(T::Type{S}) where {S<:Block}
function MakieCore.InvalidAttributeError(::Type{BT}, attributes::Set{Symbol}) where {BT <: Block}
return MakieCore.InvalidAttributeError(BT, "block", attributes)

Check warning on line 292 in src/makielayout/blocks.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks.jl#L291-L292

Added lines #L291 - L292 were not covered by tests
end

function MakieCore.attribute_names(::Type{T}) where {T <: Block}
attrs = _attribute_docs(T)

Check warning on line 296 in src/makielayout/blocks.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks.jl#L295-L296

Added lines #L295 - L296 were not covered by tests
# Some blocks have keyword arguments that are not attributes.
# TODO: Refactor intiailize_block! to just not use kwargs?
(S <: Axis || S <: PolarAxis) && (attrs[:palette] = "")
S <: Legend && (attrs[:entrygroups] = "")
S <: Menu && (attrs[:default] = "")
S <: LScene && (attrs[:scenekw] = "")
(T <: Axis || T <: PolarAxis) && (attrs[:palette] = "")
T <: Legend && (attrs[:entrygroups] = "")
T <: Menu && (attrs[:default] = "")
T <: LScene && (attrs[:scenekw] = "")
return keys(attrs)

Check warning on line 303 in src/makielayout/blocks.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks.jl#L299-L303

Added lines #L299 - L303 were not covered by tests
end
MakieCore.__has_generic_attributes(::Type{<:Block}) = false

function _check_remaining_kwargs(T::Type{<:Block}, kwdict::Dict)
badnames = setdiff(keys(kwdict), MakieCore.__valid_attributes(T))
badnames = setdiff(keys(kwdict), MakieCore.attribute_names(T))
if !isempty(badnames)
throw(MakieCore.InvalidAttributeError(T, badnames))

Check warning on line 309 in src/makielayout/blocks.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks.jl#L306-L309

Added lines #L306 - L309 were not covered by tests
end
Expand Down
26 changes: 11 additions & 15 deletions test/pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ end
@test all(x -> x isa Volume, plots)
end

import Makie.MakieCore:
__obj_name,
__valid_attributes,
__has_generic_attributes,
import Makie.MakieCore:
InvalidAttributeError,
attribute_names
import Makie: _attribute_docs
Expand Down Expand Up @@ -183,14 +180,13 @@ end
@test testrecipe(1:4, 1:4, color=:red) isa Makie.FigureAxisPlot
end

@testset "validated attributes for blocks" begin
@test __obj_name(Lines) == "plot"
@test __valid_attributes(Lines) == attribute_names(Lines)
@test __has_generic_attributes(Lines)
@testset "validated attributes for blocks" begin
err = InvalidAttributeError(Lines, Set{Symbol}())
@test err.object_name == "plot"

@test __obj_name(Axis) == "block"
@test __valid_attributes(Axis3) == keys(_attribute_docs(Axis3))
@test !__has_generic_attributes(Axis3)
err = InvalidAttributeError(Axis, Set{Symbol}())
@test err.object_name == "block"
@test attribute_names(Axis3) == keys(_attribute_docs(Axis3))

fig = Figure()
@test_throws InvalidAttributeError Axis(fig[1, 1], does_not_exist = 123)
Expand All @@ -214,8 +210,8 @@ end
@test Menu(fig[1, 2], default = nothing) isa Menu
@test Legend(fig[1, 3], entrygroups = []) isa Legend
@test PolarAxis(fig[1, 4], palette = nothing) isa PolarAxis
@test :palette in __valid_attributes(Axis)
@test :default in __valid_attributes(Menu)
@test :entrygroups in __valid_attributes(Legend)
@test :palette in __valid_attributes(PolarAxis)
@test :palette in attribute_names(Axis)
@test :default in attribute_names(Menu)
@test :entrygroups in attribute_names(Legend)
@test :palette in attribute_names(PolarAxis)
end

0 comments on commit 31bcdfb

Please sign in to comment.