Directory and file accessors

This commit is contained in:
Alexander Nozik 2021-01-27 22:55:25 +03:00
parent e52924a5e2
commit 7658f65bd3
3 changed files with 19 additions and 19 deletions

View File

@ -4,7 +4,7 @@ plugins {
allprojects {
group = "ru.inr.mass"
version = "0.1.0-SHAPSHOT"
version = "0.1.0"
}
val dataforgeVersion by extra("0.3.0-dev-1")

View File

@ -4,6 +4,7 @@ import hep.dataforge.context.Context
import hep.dataforge.context.logger
import hep.dataforge.io.io
import hep.dataforge.io.readEnvelopeFile
import hep.dataforge.meta.DFExperimental
import hep.dataforge.meta.Meta
import ru.inr.mass.data.api.NumassPoint
import ru.inr.mass.data.api.NumassSet
@ -21,31 +22,43 @@ public class NumassDirectorySet internal constructor(
public val path: Path,
) : NumassSet {
@OptIn(DFExperimental::class)
override val meta: Meta by lazy {
val metaFilePath = path / "meta"
if (metaFilePath.exists()) {
val envelope = context.io.readEnvelopeFile(path) ?: error("Envelope could not be read from $path")
val envelope = context.io.readEnvelopeFile(metaFilePath) ?: error("Envelope could not be read from $metaFilePath")
envelope.meta
} else {
context.logger.warn { "Meta file does not exist for Numass set $metaFilePath" }
Meta.EMPTY
}
}
override val points: List<NumassPoint> by lazy<List<NumassPoint>> {
Files.list(path).filter { it.fileName.startsWith("p") }.map { path ->
Files.list(path).filter { it.fileName.startsWith("p") }.map { pointPath ->
try {
context.readNumassFile(path)
context.readNumassFile(pointPath)
} catch (e: Exception) {
context.logger.error(e) { "Error reading Numass point file $path" }
context.logger.error(e) { "Error reading Numass point file $pointPath" }
null
}
}.toList().filterNotNull()
}
}
@OptIn(DFExperimental::class)
public fun Context.readNumassFile(path: Path): ProtoNumassPoint? {
val envelope = io.readEnvelopeFile(path) ?: error("Envelope could not be read from $path")
return ProtoNumassPoint.fromEnvelope(envelope)
}
public fun Context.readNumassFile(path: String): ProtoNumassPoint? = readNumassFile(Path.of(path))
@OptIn(ExperimentalPathApi::class)
public fun Context.readNumassDirectory(path: Path): NumassDirectorySet {
if(!path.exists()) error("Path $path does not exist")
if(!path.isDirectory()) error("The path $path is not a directory")
return NumassDirectorySet(this, path)
}
public fun Context.readNumassDirectory(path: String): NumassDirectorySet = readNumassDirectory(Path.of(path))

View File

@ -1,13 +0,0 @@
package ru.inr.mass.data.proto
import hep.dataforge.context.Context
import hep.dataforge.io.io
import hep.dataforge.io.readEnvelopeFile
import java.nio.file.Path
public fun Context.readNumassFile(path: Path): ProtoNumassPoint? {
val envelope = io.readEnvelopeFile(path) ?: error("Envelope could not be read from $path")
return ProtoNumassPoint.fromEnvelope(envelope)
}
public fun Context.readNumassFile(path: String): ProtoNumassPoint? = readNumassFile(Path.of(path))