Packages
livebook
0.6.1
0.19.8
0.19.7
0.19.6
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.14.0-rc.1
0.14.0-rc.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
Automate code & data workflows with interactive notebooks
Current section
Files
Jump to
Current section
Files
lib/livebook/settings.ex
defmodule Livebook.Settings do
@moduledoc false
# Keeps all Livebook settings that are backed by storage.
alias Livebook.FileSystem
@typedoc """
An id that is used for filesystem's manipulation, either insertion or removal.
"""
@type file_system_id :: :local | String.t()
@doc """
Returns the current autosave path.
"""
@spec autosave_path() :: String.t() | nil
def autosave_path() do
case storage().fetch_key(:settings, "global", :autosave_path) do
{:ok, value} -> value
:error -> default_autosave_path()
end
end
@doc """
Returns the default autosave path.
"""
@spec default_autosave_path() :: String.t()
def default_autosave_path() do
Path.join(Livebook.Config.data_path(), "autosaved")
end
@doc """
Sets the current autosave path.
"""
@spec set_autosave_path(String.t()) :: :ok
def set_autosave_path(autosave_path) do
storage().insert(:settings, "global", autosave_path: autosave_path)
end
@doc """
Restores the default autosave path.
"""
@spec reset_autosave_path() :: :ok
def reset_autosave_path() do
storage().delete_key(:settings, "global", :autosave_path)
end
@doc """
Returns all known filesystems with their associated ids.
In case of the local filesystem the id resolves to `:local` atom.
"""
@spec file_systems() :: [{file_system_id(), Filesystem.t()}]
def file_systems() do
restored_file_systems =
storage().all(:filesystem)
|> Enum.sort_by(&Map.get(&1, :order, System.os_time()))
|> Enum.map(fn %{id: fs_id} = raw_fs ->
{fs_id, storage_to_fs(raw_fs)}
end)
[{:local, Livebook.Config.local_filesystem()} | restored_file_systems]
end
@doc """
Saves a new file system to the configured ones.
"""
@spec save_filesystem(FileSystem.t()) :: file_system_id()
def save_filesystem(%FileSystem.S3{} = file_system) do
attributes =
file_system
|> FileSystem.S3.to_config()
|> Map.to_list()
id = Livebook.Utils.random_short_id()
:ok =
storage().insert(:filesystem, id, [{:type, "s3"}, {:order, System.os_time()} | attributes])
id
end
@doc """
Removes the given file system from the configured ones.
"""
@spec remove_file_system(file_system_id()) :: :ok
def remove_file_system(filesystem_id) do
storage().delete(:filesystem, filesystem_id)
end
defp storage() do
Livebook.Storage.current()
end
defp storage_to_fs(%{type: "s3"} = config) do
case FileSystem.S3.from_config(config) do
{:ok, fs} -> fs
{:error, message} -> raise ArgumentError, "invalid S3 filesystem: #{message}"
end
end
@doc """
Returns whether the update check is enabled.
"""
@spec update_check_enabled?() :: boolean()
def update_check_enabled?() do
case storage().fetch_key(:settings, "global", :update_check_enabled) do
{:ok, value} -> value
:error -> true
end
end
@doc """
Sets whether the update check is enabled.
"""
@spec set_update_check_enabled(boolean()) :: :ok
def set_update_check_enabled(enabled) do
storage().insert(:settings, "global", update_check_enabled: enabled)
end
end