From b8869570cea07412ecf391906813f8bb48509ad4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 25 May 2022 19:00:12 +0300 Subject: [PATCH] refactor file reading --- .../space/kscience/dataforge/io/IOFormat.kt | 3 +- .../kscience/dataforge/workspace/fileData.kt | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt index 3a0d4eea..f4735878 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt @@ -37,7 +37,6 @@ public fun interface IOWriter { public fun writeObject(output: Output, obj: T) } - /** * And interface for reading and writing objects into with IO streams */ @@ -83,7 +82,7 @@ public object DoubleIOFormat : IOFormat, IOFormatFactory { override val type: KType get() = typeOf() - override fun writeObject(output: Output, obj: kotlin.Double) { + override fun writeObject(output: Output, obj: Double) { output.writeDouble(obj) } diff --git a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt index 67f1ca12..21151113 100644 --- a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt @@ -4,6 +4,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import space.kscience.dataforge.context.error +import space.kscience.dataforge.context.logger import space.kscience.dataforge.data.* import space.kscience.dataforge.io.* import space.kscience.dataforge.meta.Meta @@ -48,7 +50,7 @@ public class FileData internal constructor(private val data: Data) : Data< public val META_FILE_PATH_KEY: Name = META_FILE_KEY + "path" public val META_FILE_EXTENSION_KEY: Name = META_FILE_KEY + "extension" public val META_FILE_CREATE_TIME_KEY: Name = META_FILE_KEY + "created" - public val META_FILE_UPDATE_TIME_KEY: Name = META_FILE_KEY + "update" + public val META_FILE_UPDATE_TIME_KEY: Name = META_FILE_KEY + "updated" } } @@ -229,16 +231,21 @@ public fun DataSetBuilder.file( path: Path, formatResolver: FileFormatResolver, ) { - //If path is a single file or a special directory, read it as single datum - if (!Files.isDirectory(path) || Files.list(path).allMatch { it.fileName.toString().startsWith("@") }) { - val data = readDataFile(path, formatResolver) - val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension - data(name, data) - } else { - //otherwise, read as directory - val data = readDataDirectory(dataType, path, formatResolver) - val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension - node(name, data) + try { + + //If path is a single file or a special directory, read it as single datum + if (!Files.isDirectory(path) || Files.list(path).allMatch { it.fileName.toString().startsWith("@") }) { + val data = readDataFile(path, formatResolver) + val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension + data(name, data) + } else { + //otherwise, read as directory + val data = readDataDirectory(dataType, path, formatResolver) + val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension + node(name, data) + } + } catch (ex: Exception) { + logger.error { "Failed to read file or directory at $path: ${ex.message}" } } }