Skip to content

Commit c895b4f

Browse files
committedMay 31, 2022
fixup! DataSet create/delete + storage driver API changes
1 parent c3769be commit c895b4f

5 files changed

+89
-63
lines changed
 

‎src/FileTree.jl

+4
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ function close_dataset(storage::Union{File,FileTree}, exc=nothing)
474474
close_dataset(storage.root)
475475
end
476476

477+
# Utility functions
478+
is_File_dtype(dtype) = (dtype == "File" || dtype == "Blob")
479+
is_FileTree_dtype(dtype) = (dtype == "FileTree" || dtype == "BlobTree")
480+
477481
#-------------------------------------------------------------------------------
478482
# Path manipulation
479483

‎src/TomlDataStorage.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ end
142142
function open_dataset(driver::TomlDataDriver, dataset, write)
143143
type = dataset.storage["type"]
144144
data = get(dataset.storage, "data", nothing)
145-
if type in ("File", "Blob")
145+
if is_File_dtype(type)
146146
if !(data isa AbstractString)
147147
error("TOML data storage requires string data in the \"storage.data\" key")
148148
end
149149
return File(TomlDataRoot(dataset, _data_strings_to_buffers(data), write))
150-
elseif type in ("FileTree", "BlobTree")
150+
elseif is_FileTree_dtype(type)
151151
if !(data isa AbstractDict)
152152
error("TOML data storage requires a dictionary in the \"storage.data\" key")
153153
end

‎src/data_project.jl

+27-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ function Base.show(io::IO, ::MIME"text/plain", project::AbstractDataProject)
198198
for (i, (name, data)) in enumerate(sorted)
199199
pad = maxwidth - textwidth(name)
200200
storagetype = get(data.storage, "type", nothing)
201-
icon = storagetype in ("File", "Blob") ? '📄' :
202-
storagetype in ("FileTree", "BlobTree") ? '📁' :
201+
icon = is_File_dtype(storagetype) ? '📄' :
202+
is_FileTree_dtype(storagetype) ? '📁' :
203203
''
204204
print(io, " ", icon, ' ', name, ' '^pad, " => ", data.uuid)
205205
if i < length(sorted)
@@ -321,6 +321,31 @@ function Base.show(io::IO, mime::MIME"text/plain", stack::StackedDataProject)
321321
end
322322
end
323323

324+
"""
325+
create!(name; [type=..., driver=..., description=..., tags=..., kws...])
326+
327+
Create a dataset named `name` in the global data project.
328+
329+
create!(proj, name; kws...)
330+
331+
Create a dataset in the given data project. The keyword arguments `kws` are the
332+
same as the global version.
333+
334+
335+
# Examples
336+
337+
To create a `File` named "SomeFile" on the local filesystem,
338+
339+
```
340+
DataSets.create!("SomeFile", type="File", driver="FileSystem")
341+
```
342+
343+
To create a `FileTree` named "Namespace/SomeDir" on the local filesystem,
344+
345+
```
346+
DataSets.create!("Namespace/SomeDir", type="FileTree", driver="FileSystem")
347+
```
348+
"""
324349
function create!(stack::StackedDataProject, name; kws...)
325350
for proj in stack.projects
326351
ds = create!(proj, name; kws...)

‎src/file_data_projects.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ function create!(proj::AbstractTomlFileDataProject, name;
201201

202202
ds = DataSet(conf)
203203
proj[ds.name] = ds
204+
setfield!(ds, :project, proj)
204205
return ds
205206
end
206207

@@ -252,17 +253,14 @@ function Base.delete!(proj::TomlFileDataProject, name::AbstractString)
252253
driver = _find_driver(ds)
253254
delete_storage(proj, driver, ds)
254255
end
255-
256256
Base.delete!(inner_proj, name)
257+
inner_proj
257258
end
258259
end
259260

260261
#-------------------------------------------------------------------------------
261262
default_driver(proj::AbstractTomlFileDataProject) = FileSystemDriver()
262263

263-
project_root_path(proj) = error("No local path for data project type $(typeof(proj))")
264-
project_root_path(proj::TomlFileDataProject) = dirname(proj.path)
265-
266264

267265
#------------------------------------------------------------------------------
268266
"""

‎src/filesystem.jl

+54-55
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,40 @@ function local_data_abspath(::Nothing, path)
255255
end
256256

257257

258+
# Return local filesystem absolute path for the dataset, as a string
259+
function _filesystem_dataset_abspath(dataset)
260+
config = dataset.storage
261+
if haskey(config, "path")
262+
pathstr = config["path"]
263+
# Local absolute paths are not portable. Previously these were allowed
264+
# in the "path" key, but those are now deprecated in favor of
265+
# system-specific path keys unix_path or windows_path
266+
if isabspath(pathstr)
267+
Base.depwarn("""
268+
Absolute paths in Data.toml are deprecated. Instead, use relative
269+
paths (separated with `/`) relative to the Data.toml location.""",
270+
:connect_filesystem)
271+
return pathstr
272+
else
273+
if '\\' in pathstr && Sys.iswindows()
274+
# Heuristic deprecation warning for windows paths in Data.toml
275+
Base.depwarn(
276+
"Relative paths in Data.toml should be separated with '/' characters.",
277+
:connect_filesystem)
278+
pathstr = join(split(pathstr, '\\'), '/')
279+
end
280+
relpath = joinpath(split(pathstr, '/')...)
281+
return local_data_abspath(data_project(dataset), relpath)
282+
end
283+
elseif haskey(config, "unix_path") && Sys.isunix()
284+
return config["unix_path"]
285+
elseif haskey(config, "windows_path") && Sys.iswindows()
286+
return config["windows_path"]
287+
else
288+
error("No \"path\" key found for FileSystem storage driver.")
289+
end
290+
end
291+
258292
"""
259293
## Metadata spec
260294
@@ -284,40 +318,12 @@ For FileTree:
284318
function open_dataset(driver::FileSystemDriver, dataset, write)
285319
config = dataset.storage
286320
# Paths keys can be in three forms documented above;
287-
if haskey(config, "path")
288-
pathstr = config["path"]
289-
# Local absolute paths are not portable. Previously these were allowed
290-
# in the "path" key, but those are now deprecated in favor of
291-
# system-specific path keys unix_path or windows_path
292-
if isabspath(pathstr)
293-
Base.depwarn("""
294-
Absolute paths in Data.toml are deprecated. Instead, use relative
295-
paths (separated with `/`) relative to the Data.toml location.""",
296-
:connect_filesystem)
297-
path = pathstr
298-
else
299-
if '\\' in pathstr && Sys.iswindows()
300-
# Heuristic deprecation warning for windows paths in Data.toml
301-
Base.depwarn(
302-
"Relative paths in Data.toml should be separated with '/' characters.",
303-
:connect_filesystem)
304-
pathstr = join(split(pathstr, '\\'), '/')
305-
end
306-
relpath = joinpath(split(pathstr, '/')...)
307-
path = local_data_abspath(data_project(dataset), relpath)
308-
end
309-
elseif haskey(config, "unix_path") && Sys.isunix()
310-
path = config["unix_path"]
311-
elseif haskey(config, "windows_path") && Sys.iswindows()
312-
path = config["windows_path"]
313-
else
314-
error("No \"path\" key found for FileSystem storage driver.")
315-
end
316321
type = config["type"]
317-
if type in ("File", "Blob")
322+
path = _filesystem_dataset_abspath(dataset)
323+
if is_File_dtype(type)
318324
isfile(path) || throw(ArgumentError("$(repr(path)) should be a file"))
319325
storage = File(FileSystemRoot(path; write=write))
320-
elseif type in ("FileTree", "BlobTree")
326+
elseif is_FileTree_dtype(type)
321327
isdir(path) || throw(ArgumentError("$(repr(path)) should be a directory"))
322328
storage = FileTree(FileSystemRoot(path; write=write))
323329
path = dataspec_fragment_as_path(dataset)
@@ -333,59 +339,54 @@ end
333339
function create_storage(proj, driver::FileSystemDriver,
334340
name::AbstractString;
335341
source::Union{Nothing,DataSet}=nothing,
336-
dtype::Union{Nothing,AbstractString}=nothing,
342+
type::Union{Nothing,AbstractString}=nothing,
337343
kws...)
338-
339344
# For other cases here, we're not linking to the original source, but
340345
# rather making a copy.
341346
if !isnothing(source)
342-
dtype = source.storage["type"]
347+
type = source.storage["type"]
343348
else
344-
if isnothing(dtype)
345-
throw(ArgumentError("Must provide one of `source` or `dtype`."))
349+
if isnothing(type)
350+
throw(ArgumentError("Must provide one of `source` or `type`."))
346351
end
347352
end
348353

349354
has_slashes = '/' in name
350-
local_name = has_slashes ?
351-
joinpath(split(name, '/')) :
352-
name
355+
relpath = has_slashes ? joinpath(split(name, '/')) : name
353356

354-
# project_root_path() will fail
355-
data_path = joinpath(project_root_path(proj), local_name)
357+
data_path = local_data_abspath(proj, relpath)
356358
if ispath(data_path)
357359
error("Local path already exists: $data_path")
358360
end
359361

360-
if !(dtype in ("Blob", "BlobTree"))
361-
error("Unknown storage type for FileSystemDriver: $dtype")
362+
if !(type in ("File", "FileTree"))
363+
error("Unknown storage type for FileSystemDriver: $type")
362364
end
363365

364366
# Create file or directory
365367
if has_slashes
366368
mkpath(dirname(data_path))
367369
end
368-
if dtype == "Blob"
370+
if is_File_dtype(type)
369371
touch(data_path)
370-
elseif dtype == "BlobTree"
372+
elseif is_FileTree_dtype(type)
371373
mkdir(data_path)
372374
end
373375

374376
Dict(
375377
"driver"=>"FileSystem",
376-
"type"=>dtype,
377-
"path"=>"@__DIR__/$name",
378+
"type"=>type,
379+
"path"=>relpath,
378380
)
379381
end
380382

381-
382-
function delete_storage(proj, driver::FileSystemDriver, ds::DataSet)
383-
path = ds.storage["path"]
384-
type = ds.storage["type"]
385-
if type == "Blob"
383+
function delete_storage(proj, driver::FileSystemDriver, dataset::DataSet)
384+
path = _filesystem_dataset_abspath(dataset)
385+
type = dataset.storage["type"]
386+
if is_File_dtype(type)
386387
isfile(path) || throw(ArgumentError("$(repr(path)) should be a file"))
387388
rm(path)
388-
elseif type == "BlobTree"
389+
elseif is_FileTree_dtype(type)
389390
isdir(path) || throw(ArgumentError("$(repr(path)) should be a directory"))
390391
rm(path, recursive=true)
391392
else
@@ -394,8 +395,6 @@ function delete_storage(proj, driver::FileSystemDriver, ds::DataSet)
394395
end
395396

396397

397-
398-
399398
#-------------------------------------------------------------------------------
400399
# Deprecations
401400
function Base.abspath(relpath::RelPath)

0 commit comments

Comments
 (0)