Commit 02d0d44a authored by Romain Reuillon's avatar Romain Reuillon

[Core] enh: refactor writable output

parent 9fa387e2
......@@ -38,7 +38,7 @@ object CSVOutputFormat {
import WritableOutput._
(output, content) match {
case (FileValue(file), PlainContent(variables, name))
case (Store(file), PlainContent(variables, name))
val f =
name match {
case None file.from(context)
......@@ -46,12 +46,12 @@ object CSVOutputFormat {
}
writeFile(f, variables)
case (FileValue(file), SectionContent(sections))
case (Store(file), SectionContent(sections))
val directory = file.from(context)
for { section sections } writeFile(directory / s"${section.name.from(context)}.csv", section.variables)
case (StreamValue(ps), PlainContent(variables, name))
case (Display(ps), PlainContent(variables, name))
writeStream(ps, name.map(_.from(context)), variables)
case (StreamValue(ps), SectionContent(sections))
case (Display(ps), SectionContent(sections))
for { section sections } writeStream(ps, Some(section.name.from(context)), section.variables)
}
}
......
......@@ -23,19 +23,17 @@ import org.openmole.core.expansion.FromContext
object WritableOutput {
implicit def fromFile(file: File) = FileValue(file)
implicit def fromFileContext(file: FromContext[File]) = FileValue(file)
implicit def fromPrintStream(ps: PrintStream) = StreamValue(ps)
implicit def fromFile(file: File) = Store(file)
implicit def fromFileContext(file: FromContext[File]) = Store(file)
implicit def fromPrintStream(ps: PrintStream) = Display(ps)
type Display = PrintStream
case class FileValue(file: FromContext[File]) extends WritableOutput
case class StreamValue(stream: PrintStream) extends WritableOutput
case class Store(file: FromContext[File]) extends WritableOutput
case class Display(stream: PrintStream) extends WritableOutput
def file(writableOutput: WritableOutput) =
writableOutput match {
case FileValue(file) Some(file)
case _ None
case Store(file) Some(file)
case _ None
}
}
......
......@@ -6,7 +6,7 @@ package format {
trait FormatPackage {
type Display = WritableOutput.Display
def display(implicit outputRedirection: OutputRedirection): Display = outputRedirection.output
def display(implicit outputRedirection: OutputRedirection): Display = WritableOutput.Display(outputRedirection.output)
def CSVOutputFormat = format.CSVOutputFormat
}
}
......
......@@ -26,7 +26,7 @@ object JSONOutputFormat {
)
(output, content) match {
case (FileValue(file), PlainContent(variables, name))
case (Store(file), PlainContent(variables, name))
val f =
name match {
case Some(n) file / s"${n.from(context)}.json"
......@@ -36,18 +36,18 @@ object JSONOutputFormat {
f.from(context).withPrintStream(append = false, create = true) { ps
ps.print(compact(render(variablesToJValue(variables))))
}
case (FileValue(file), sections: SectionContent)
case (Store(file), sections: SectionContent)
file.from(context).withPrintStream(append = false, create = true) { ps
ps.print(compact(render(sectionContent(sections))))
}
case (StreamValue(ps), PlainContent(variables, name))
case (Display(ps), PlainContent(variables, name))
name match {
case Some(f)
ps.println(s"${f.from(context)}:")
ps.println(pretty(render(variablesToJValue(variables))).split("\n").map(" " + _).mkString("\n"))
case None ps.println(pretty(render(variablesToJValue(variables))))
}
case (StreamValue(ps), sections: SectionContent)
case (Display(ps), sections: SectionContent)
ps.println(pretty(render(sectionContent(sections))))
}
......
......@@ -34,7 +34,7 @@ class Activator extends PluginInfoActivator {
val keyWords: Vector[KeyWord] =
Vector( // TaskKeyWord(objectName(JSONHook)),
// OtherKeyWord(objectName(JSONOutputFormat))
OtherKeyWord(objectName(OMROutputFormat))
)
PluginInfo.register(this, Vector(this.getClass.getPackage), keyWords = keyWords)
......
//package org.openmole.plugin.hook.omr
package org.openmole.plugin.hook.omr
import org.openmole.core.dsl._
import org.openmole.core.dsl.extension._
import org.openmole.core.workflow.format.CSVOutputFormat
import org.openmole.core.workflow.hook.FromContextHook
import org.openmole.plugin.tool.json._
object OMROutputFormat {
implicit def outputFormat: OutputFormat[OMROutputFormat, Any] = new OutputFormat[OMROutputFormat, Any] {
override def write(format: OMROutputFormat, output: WritableOutput, content: OutputContent, method: Any): FromContext[Unit] = FromContext { p
import p._
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats = DefaultFormats
output match {
case WritableOutput.Display(stream) => implicitly[OutputFormat[CSVOutputFormat, Any]].write(CSVOutputFormat(), output, content, method)
case WritableOutput.Store(file) =>
import org.openmole.tool.tar._
// file.from(context).withTarGZOutputStream {
//
//import org.openmole.core.dsl._
//import org.openmole.core.dsl.extension._
//import org.openmole.core.workflow.hook.FromContextHook
//import org.openmole.plugin.tool.json._
//
//object OMROutputFormat {
//
// implicit def outputFormat = new OutputFormat[OMROutputFormat, Any] {
// override def write(format: OMROutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p ⇒
// import p._
// import org.json4s._
// import org.json4s.jackson.JsonMethods._
//
// implicit val formats = DefaultFormats
//
// output match {
// case WritableOutput.FileValue(file) ⇒
// file.from(context).withPrintStream(append = false, create = true) { ps ⇒
// ps.print(compact(render(variablesToJValue(variables))))
// }
// case WritableOutput.StreamValue(ps, prelude) ⇒
// prelude.foreach(ps.print)
// ps.println(pretty(render(variablesToJValue(variables))))
// }
// }
//
// override def validate(format: OMROutputFormat): FromContextHook.ValidateParameters ⇒ Seq[Throwable] = { p ⇒ Seq() }
// override def extension = ".omr"
// }
//
//}
//
//case class OMROutputFormat()
}
}
override def validate(format: OMROutputFormat): FromContextHook.ValidateParameters Seq[Throwable] = { p Seq() }
}
}
case class OMROutputFormat()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment