Skip to content

Commit

Permalink
Merge pull request #66 from aviatesk/avi/name-extentions
Browse files Browse the repository at this point in the history
allow `[has|load|delete]_preference` to take `name::String`
  • Loading branch information
staticfloat authored Mar 3, 2024
2 parents 040be44 + 5f5a36e commit f395eb2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
61 changes: 43 additions & 18 deletions src/Preferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export load_preference, @load_preference,
include("utils.jl")

"""
load_preference(uuid_or_module, key, default = nothing)
load_preference(uuid_or_module_or_name, key, default = nothing)
Load a particular preference from the `Preferences.toml` file, shallowly merging keys
as it walks the hierarchy of load paths, loading preferences from all environments that
Expand All @@ -24,6 +24,7 @@ list the given UUID as a direct dependency.
Most users should use the `@load_preference` convenience macro which auto-determines the
calling `Module`.
"""
function load_preference end
function load_preference(uuid::UUID, key::String, default = nothing)
# Re-use definition in `base/loading.jl` so as to not repeat code.
d = Base.get_preferences(uuid)
Expand All @@ -35,6 +36,13 @@ end
function load_preference(m::Module, key::String, default = nothing)
return load_preference(get_uuid(m), key, default)
end
function load_preference(name::String, key::String, default = nothing)
uuid = get_uuid(name)
if uuid === nothing
package_lookup_error(name)
end
return load_preference(uuid, key, default)
end

"""
@load_preference(key)
Expand All @@ -48,19 +56,27 @@ macro load_preference(key, default = nothing)
end

"""
has_preference(uuid_or_module, key)
has_preference(uuid_or_module_or_name, key)
Return `true` if the particular preference is found, and `false` otherwise.
See the `has_preference` docstring for more details.
"""
function has_preference end
function has_preference(uuid::UUID, key::String)
value = load_preference(uuid, key, nothing)
return !(value isa Nothing)
end
function has_preference(m::Module, key::String)
return has_preference(get_uuid(m), key)
end
function has_preference(name::String, key::String)
uuid = get_uuid(name)
if uuid === nothing
package_lookup_error(name)
end
return has_preference(uuid, key)
end

"""
@has_preference(key)
Expand Down Expand Up @@ -148,17 +164,17 @@ function set_preferences!(target_toml::String, pkg_name::String, pairs::Pair{Str
end

"""
set_preferences!(uuid_or_module, prefs::Pair{String,Any}...; export_prefs=false,
active_project_only=true, force=false)
Sets a series of preferences for the given UUID/Module, identified by the pairs passed in
as `prefs`. Preferences are loaded from `Project.toml` and `LocalPreferences.toml` files
on the load path, merging values together into a cohesive view, with preferences taking
precedence in `LOAD_PATH` order, just as package resolution does. Preferences stored in
`Project.toml` files are considered "exported", as they are easily shared across package
installs, whereas the `LocalPreferences.toml` file is meant to represent local
preferences that are not typically shared. `LocalPreferences.toml` settings override
`Project.toml` settings where appropriate.
set_preferences!(uuid_or_module_or_name, prefs::Pair{String,Any}...;
export_prefs=false, active_project_only=true, force=false)
Sets a series of preferences for the given uuid::UUID/module::Module/name::String,
identified by the pairs passed in as `prefs`. Preferences are loaded from `Project.toml`
and `LocalPreferences.toml` files on the load path, merging values together into a cohesive
view, with preferences taking precedence in `LOAD_PATH` order, just as package resolution
does. Preferences stored in `Project.toml` files are considered "exported", as they are
easily shared across package installs, whereas the `LocalPreferences.toml` file is meant to
represent local preferences that are not typically shared. `LocalPreferences.toml` settings
override `Project.toml` settings where appropriate.
After running `set_preferences!(uuid, "key" => value)`, a future invocation of
`load_preference(uuid, "key")` will generally result in `value`, with the exception of
Expand Down Expand Up @@ -202,6 +218,8 @@ search up the load path for an environment that does contain that module, settin
preference in the first one it finds. If none are found, it falls back to setting the
preference in the active project and adding it as an extra dependency.
"""
function set_preferences! end

function set_preferences!(u::UUID, prefs::Pair{String,<:Any}...; export_prefs=false,
active_project_only::Bool=true, kwargs...)
# If we try to add preferences for a dependency, we need to make sure
Expand Down Expand Up @@ -308,13 +326,13 @@ macro set_preferences!(prefs...)
end

"""
delete_preferences!(uuid_or_module, prefs::String...; block_inheritance::Bool = false, export_prefs=false, force=false)
delete_preferences!(uuid_or_module_or_name, prefs::String...;
block_inheritance::Bool = false, export_prefs=false, force=false)
Deletes a series of preferences for the given UUID/Module, identified by the
keys passed in as `prefs`.
Deletes a series of preferences for the given uuid::UUID/module::Module/name::String,
identified by the keys passed in as `prefs`.
See the docstring for `set_preferences!`for more details.
See the docstring for [`set_preferences!`](@ref) for more details.
"""
function delete_preferences!(u::UUID, pref_keys::String...; block_inheritance::Bool = false, kwargs...)
if block_inheritance
Expand All @@ -326,6 +344,13 @@ end
function delete_preferences!(m::Module, pref_keys::String...; kwargs...)
return delete_preferences!(get_uuid(m), pref_keys...; kwargs...)
end
function delete_preferences!(name::String, pref_keys::String...; kwargs...)
uuid = get_uuid(name)
if uuid === nothing
package_lookup_error(name)
end
return delete_preferences!(uuid, pref_keys...; kwargs...)
end

"""
@delete_preferences!(prefs...)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ function get_uuid(name::String)
end
end

package_lookup_error(name::String) = throw(ArgumentError(
"Cannot resolve package '$(name)' in load path; have you added the package as a top-level dependency?"))

function find_first_project_with_uuid(uuid::UUID)
# Find first element in `Base.load_path()` that contains this UUID
# This code should look similar to the search in `Base.get_preferences()`
Expand Down
12 changes: 9 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ end
push!(Base.LOAD_PATH, dir)
try
# Can't do this unless `UsesPreferences` is added as a dep
@test_throws ArgumentError Preferences.set_preferences!("UsesPreferences", "location" => "exists")
@test_throws ArgumentError set_preferences!("UsesPreferences", "location" => "exists")
@test_throws ArgumentError has_preference("UsesPreferences", "location")
@test_throws ArgumentError load_preference("UsesPreferences", "location")
@test_throws ArgumentError delete_preferences!("UsesPreferences", "location")

switch()

# After switching `up_path`, it works.
Preferences.set_preferences!("UsesPreferences", "location" => "exists")
@test load_preference(up_uuid, "location") == "exists"
set_preferences!("UsesPreferences", "location" => "exists")
@test has_preference("UsesPreferences", "location")
@test load_preference("UsesPreferences", "location") == "exists"
delete_preferences!("UsesPreferences", "location"; force=true)
@test !has_preference("UsesPreferences", "location")
finally
pop!(Base.LOAD_PATH)
rm(local_prefs_toml; force=true)
Expand Down

0 comments on commit f395eb2

Please sign in to comment.