Data tree refactored to a uniform tree instead of sealed class.
This commit is contained in:
parent
466e460989
commit
6e209ab5cd
@ -4,7 +4,7 @@ plugins {
|
|||||||
|
|
||||||
description = "IO module"
|
description = "IO module"
|
||||||
|
|
||||||
val ioVersion = "0.3.0"
|
val ioVersion = "0.3.1"
|
||||||
|
|
||||||
kscience {
|
kscience {
|
||||||
jvm()
|
jvm()
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package space.kscience.dataforge.workspace
|
package space.kscience.dataforge.workspace
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import space.kscience.dataforge.data.*
|
import space.kscience.dataforge.data.Data
|
||||||
|
import space.kscience.dataforge.data.DataSink
|
||||||
|
import space.kscience.dataforge.data.StaticData
|
||||||
import space.kscience.dataforge.io.*
|
import space.kscience.dataforge.io.*
|
||||||
import space.kscience.dataforge.meta.Meta
|
import space.kscience.dataforge.meta.Meta
|
||||||
import space.kscience.dataforge.meta.copy
|
import space.kscience.dataforge.meta.copy
|
||||||
@ -21,11 +23,6 @@ import kotlin.io.path.*
|
|||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
|
|
||||||
//public typealias FileFormatResolver<T> = (Path, Meta) -> IOFormat<T>
|
|
||||||
|
|
||||||
public typealias FileFormatResolver<T> = (path: Path, meta: Meta) -> IOReader<T>?
|
|
||||||
|
|
||||||
|
|
||||||
public object FileData {
|
public object FileData {
|
||||||
public val FILE_KEY: Name = "file".asName()
|
public val FILE_KEY: Name = "file".asName()
|
||||||
public val FILE_PATH_KEY: Name = FILE_KEY + "path"
|
public val FILE_PATH_KEY: Name = FILE_KEY + "path"
|
||||||
@ -116,7 +113,7 @@ public fun DataSink<Binary>.files(
|
|||||||
//Using explicit Zip file system to avoid bizarre compatibility bugs
|
//Using explicit Zip file system to avoid bizarre compatibility bugs
|
||||||
val fsProvider = FileSystemProvider.installedProviders().find { it.scheme == "jar" }
|
val fsProvider = FileSystemProvider.installedProviders().find { it.scheme == "jar" }
|
||||||
?: error("Zip file system provider not found")
|
?: error("Zip file system provider not found")
|
||||||
val fs = fsProvider.newFileSystem(path, mapOf("create" to "true"))
|
val fs = fsProvider.newFileSystem(path, emptyMap<String, Any>())
|
||||||
|
|
||||||
files(io, name, fs.rootDirectories.first())
|
files(io, name, fs.rootDirectories.first())
|
||||||
}
|
}
|
||||||
@ -170,37 +167,6 @@ public fun DataSink<Binary>.monitorFiles(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the data tree to existing directory or create a new one using default [java.nio.file.FileSystem] provider
|
|
||||||
*
|
|
||||||
* @param nameToPath a [Name] to [Path] converter used to create
|
|
||||||
*/
|
|
||||||
@DFExperimental
|
|
||||||
public suspend fun <T : Any> IOPlugin.writeDataDirectory(
|
|
||||||
path: Path,
|
|
||||||
dataSet: DataTree<T>,
|
|
||||||
format: IOWriter<T>,
|
|
||||||
envelopeFormat: EnvelopeFormat? = null,
|
|
||||||
): Unit = withContext(Dispatchers.IO) {
|
|
||||||
if (!Files.exists(path)) {
|
|
||||||
Files.createDirectories(path)
|
|
||||||
} else if (!Files.isDirectory(path)) {
|
|
||||||
error("Can't write a node into file")
|
|
||||||
}
|
|
||||||
dataSet.forEach { (name, data) ->
|
|
||||||
val childPath = path.resolve(name.tokens.joinToString("/") { token -> token.toStringUnescaped() })
|
|
||||||
childPath.parent.createDirectories()
|
|
||||||
val envelope = data.toEnvelope(format)
|
|
||||||
if (envelopeFormat != null) {
|
|
||||||
writeEnvelopeFile(childPath, envelope, envelopeFormat)
|
|
||||||
} else {
|
|
||||||
writeEnvelopeDirectory(childPath, envelope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dataSet.meta?.let { writeMetaFile(path, it) }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param resources The names of the resources to read.
|
* @param resources The names of the resources to read.
|
||||||
* @param classLoader The class loader to use for loading the resources. By default, it uses the current thread's context class loader.
|
* @param classLoader The class loader to use for loading the resources. By default, it uses the current thread's context class loader.
|
@ -0,0 +1,72 @@
|
|||||||
|
package space.kscience.dataforge.workspace
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import space.kscience.dataforge.data.*
|
||||||
|
import space.kscience.dataforge.io.*
|
||||||
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
|
import space.kscience.dataforge.names.Name
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.spi.FileSystemProvider
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.createDirectories
|
||||||
|
import kotlin.io.path.exists
|
||||||
|
import kotlin.io.path.extension
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the data tree to existing directory or create a new one using default [java.nio.file.FileSystem] provider
|
||||||
|
*
|
||||||
|
* @param nameToPath a [Name] to [Path] converter used to create
|
||||||
|
*/
|
||||||
|
@DFExperimental
|
||||||
|
public suspend fun <T : Any> IOPlugin.writeDataDirectory(
|
||||||
|
path: Path,
|
||||||
|
dataSet: DataTree<T>,
|
||||||
|
format: IOWriter<T>,
|
||||||
|
envelopeFormat: EnvelopeFormat? = null,
|
||||||
|
): Unit = withContext(Dispatchers.IO) {
|
||||||
|
if (!Files.exists(path)) {
|
||||||
|
Files.createDirectories(path)
|
||||||
|
} else if (!Files.isDirectory(path)) {
|
||||||
|
error("Can't write a node into file")
|
||||||
|
}
|
||||||
|
dataSet.forEach { (name, data) ->
|
||||||
|
val childPath = path.resolve(name.tokens.joinToString("/") { token -> token.toStringUnescaped() })
|
||||||
|
childPath.parent.createDirectories()
|
||||||
|
val envelope = data.toEnvelope(format)
|
||||||
|
if (envelopeFormat != null) {
|
||||||
|
writeEnvelopeFile(childPath, envelope, envelopeFormat)
|
||||||
|
} else {
|
||||||
|
writeEnvelopeDirectory(childPath, envelope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dataSet.meta?.let { writeMetaFile(path, it) }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write this [DataTree] as a zip archive
|
||||||
|
*/
|
||||||
|
@DFExperimental
|
||||||
|
public suspend fun <T : Any> IOPlugin.writeZip(
|
||||||
|
path: Path,
|
||||||
|
dataSet: DataTree<T>,
|
||||||
|
format: IOWriter<T>,
|
||||||
|
envelopeFormat: EnvelopeFormat? = null,
|
||||||
|
): Unit = withContext(Dispatchers.IO) {
|
||||||
|
if (path.exists()) error("Can't override existing zip data file $path")
|
||||||
|
val actualFile = if (path.extension == "zip") {
|
||||||
|
path
|
||||||
|
} else {
|
||||||
|
path.resolveSibling(path.fileName.toString() + ".zip")
|
||||||
|
}
|
||||||
|
val fsProvider = FileSystemProvider.installedProviders().find { it.scheme == "jar" }
|
||||||
|
?: error("Zip file system provider not found")
|
||||||
|
//val fs = FileSystems.newFileSystem(actualFile, mapOf("create" to true))
|
||||||
|
val fs = fsProvider.newFileSystem(actualFile, mapOf("create" to true))
|
||||||
|
fs.use {
|
||||||
|
writeDataDirectory(fs.rootDirectories.first(), dataSet, format, envelopeFormat)
|
||||||
|
}
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
package space.kscience.dataforge.workspace
|
|
||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import space.kscience.dataforge.data.DataTree
|
|
||||||
import space.kscience.dataforge.io.EnvelopeFormat
|
|
||||||
import space.kscience.dataforge.io.IOPlugin
|
|
||||||
import space.kscience.dataforge.io.IOWriter
|
|
||||||
import space.kscience.dataforge.misc.DFExperimental
|
|
||||||
import java.nio.file.Path
|
|
||||||
import java.nio.file.spi.FileSystemProvider
|
|
||||||
import kotlin.io.path.exists
|
|
||||||
import kotlin.io.path.extension
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write this [DataTree] as a zip archive
|
|
||||||
*/
|
|
||||||
@DFExperimental
|
|
||||||
public suspend fun <T : Any> IOPlugin.writeZip(
|
|
||||||
path: Path,
|
|
||||||
dataSet: DataTree<T>,
|
|
||||||
format: IOWriter<T>,
|
|
||||||
envelopeFormat: EnvelopeFormat? = null,
|
|
||||||
): Unit = withContext(Dispatchers.IO) {
|
|
||||||
if (path.exists()) error("Can't override existing zip data file $path")
|
|
||||||
val actualFile = if (path.extension == "zip") {
|
|
||||||
path
|
|
||||||
} else {
|
|
||||||
path.resolveSibling(path.fileName.toString() + ".zip")
|
|
||||||
}
|
|
||||||
val fsProvider = FileSystemProvider.installedProviders().find { it.scheme == "jar" }
|
|
||||||
?: error("Zip file system provider not found")
|
|
||||||
//val fs = FileSystems.newFileSystem(actualFile, mapOf("create" to true))
|
|
||||||
val fs = fsProvider.newFileSystem(actualFile, mapOf("create" to true))
|
|
||||||
fs.use {
|
|
||||||
writeDataDirectory(fs.rootDirectories.first(), dataSet, format, envelopeFormat)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user