• Stars
    star
    114
  • Rank 297,603 (Top 7 %)
  • Language
    Julia
  • License
    Other
  • Created over 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Project Preferences Package

Preferences.jl

Docs-stable Docs-dev Continuous Integration Code Coverage License: MIT

The Preferences package provides a convenient, integrated way for packages to store configuration switches to persistent TOML files, and use those pieces of information at both run time and compile time in Julia v1.6+. This enables the user to modify the behavior of a package, and have that choice reflected in everything from run time algorithm choice to code generation at compile time. Preferences are stored as TOML dictionaries and are, by default, stored within a (Julia)LocalPreferences.toml file next to the currently-active project. If a preference is "exported" (export_prefs=true), it is instead stored within the (Julia)Project.toml. The intention is to allow shared projects to contain shared preferences, while allowing for users themselves to override those preferences with their own settings in the LocalPreferences.toml file, which should be .gitignored as the name implies.

Preferences can be set with depot-wide defaults; if package Foo is installed within your global environment and it has preferences set, these preferences will apply as long as your global environment is part of your LOAD_PATH. Preferences in environments higher up in the environment stack get overridden by the more proximal entries in the load path, ending with the currently active project. This allows depot-wide preference defaults to exist, with active projects able to merge or even completely overwrite these inherited preferences. See the docstring for set_preferences!() for the full details of how to set preferences to allow or disallow merging.

Preferences that are accessed during compilation are automatically marked as compile-time preferences, and any change recorded to these preferences will cause the Julia compiler to recompile any cached precompilation .ji files for that module. This allows preferences to be used to influence code generation. When your package sets a compile-time preference, it is usually best to suggest to the user that they should restart Julia, to allow recompilation to occur.

Note that the package can be installed on Julia v1.0+ but is only functional on Julia v1.6+.

API

Preferences use is very simple; it is all based around four functions (which each have convenience macros): @set_preferences!(), @load_preference(), @has_preference(), and @delete_preferences!().

  • @load_preference(key, default = nothing): This loads a preference named key for the current package. If no such preference is found, it returns default.

  • @set_preferences!(pairs...; export_prefs=false): This allows setting multiple preferences at once as pairs.

  • @has_preference(key): Returns true if the preference named key is found, and false otherwise.

  • @delete_preferences!(keys...): Delete one or more preferences.

To illustrate the usage, we show a toy module, taken directly from this package's tests:

module UsesPreferences

function set_backend(new_backend::String)
    if !(new_backend in ("OpenCL", "CUDA", "jlFPGA"))
        throw(ArgumentError("Invalid backend: \"$(new_backend)\""))
    end

    # Set it in our runtime values, as well as saving it to disk
    @set_preferences!("backend" => new_backend)
    @info("New backend set; restart your Julia session for this change to take effect!")
end

const backend = @load_preference("backend", "OpenCL")

# An example that helps us to prove that things are happening at compile-time
function do_computation()
    @static if backend == "OpenCL"
        return "OpenCL is the best!"
    elseif backend == "CUDA"
        return "CUDA; so fast, so fresh!"
    elseif backend == "jlFPGA"
        return "The Future is Now, jlFPGA online!"
    else
        return nothing
    end
end


# A non-compiletime preference
function set_username(username::String)
    @set_preferences!("username" => username)
end
function get_username()
    return @load_preference("username")
end

end # module UsesPreferences

Conditional Loading

To use Preferences with Julia 1.6 and later but falling back to a default value for older Julia versions, you can conditionally load Preferences like this:

@static if VERSION >= v"1.6"
    using Preferences
end

@static if VERSION >= v"1.6"
    preference = @load_preference("preference", "default")
else
    preference = "default"
end

Note that these cannot be merged into a single @static if. Loading the package with using Preferences must be done on its own.

Authors

This repository was initiated by Elliot Saba (@staticfloat) and continues to be maintained by him and other contributors.

License and contributing

Preferences.jl is licensed under the MIT license (see LICENSE.md). Contributions by volunteers are welcome!

More Repositories

1

BinaryBuilder.jl

Binary Dependency Builder for Julia
Julia
373
star
2

Yggdrasil

Collection of builder repositories for BinaryBuilder.jl
Fortran
278
star
3

Requires.jl

Lazy code loading for Julia
Julia
193
star
4

ArtifactUtils.jl

A tool for package developers for working with artifacts.
Julia
60
star
5

BinDeps.jl

Tool for building binary dependencies for Julia modules
Julia
56
star
6

Scratch.jl

Scratch spaces for all your persistent mutable data needs
Julia
54
star
7

BinaryProvider.jl

A reliable binary provider for Julia
Julia
44
star
8

PkgServer.jl

Julia
38
star
9

RelocatableFolders.jl

Julia
34
star
10

Homebrew.jl

OSX Binary dependency provider for Julia
Julia
27
star
11

WinRPM.jl

RPM-md processing library
Julia
21
star
12

JLLPrefixes.jl

Make yourself at home; JLLs are here to stay
Julia
20
star
13

CMake.jl

Julia package to provide access to CMake
Julia
13
star
14

OutputCollectors.jl

Capture subprocess stdout and stderr streams independently, resynthesizing and colorizing the streams appropriately
Julia
13
star
15

PkgServerLogAnalysis.jl

Log analysis prototyping
Julia
12
star
16

CMakeWrapper.jl

Build CMake packages from Julia
Julia
11
star
17

BinaryBuilderBase.jl

Julia
9
star
18

JLLWrappers.jl

Julia
8
star
19

BinaryBuilder2.jl

BinaryBuilder2 monorepo
Julia
7
star
20

MetadataTools.jl

Functionality to analyze the structure of Julia's METADATA repository
Julia
5
star
21

SimpleBufferStream.jl

What Base.BufferStream should be
Julia
4
star
22

cloneallpackages

Script to clone all registered Julia packages (useful to search for code usage)
Julia
4
star
23

JLLTools.jl

Tools for creating JLL wrappers
Julia
3
star
24

CondaBinDeps.jl

Conda BinDeps provider for Julia
Julia
3
star
25

Archspec.jl

Julia
3
star
26

binarybuilder.org

A website for BinaryBuilder.jl
CSS
2
star
27

PkgLicenses.jl

Provides LICENSE texts for common software licenses
Julia
2
star
28

Nix.jl

Nix package manager for Julia
Julia
1
star
29

PkgServerS3Mirror

PkgServer with an S3Mirror attached
Makefile
1
star
30

Jotunheimr

Testing, miniaturized buildtree 🌴
Shell
1
star
31

Spack.jl

1
star