All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jove.notebook.Frontend.scala Maven / Gradle / Ivy

The newest version!
package jove
package notebook

import com.typesafe.config.{Config => Configg, _}
import org.http4s.dsl.Path

trait Frontend {
  def apply(state: ServerState, prefix: Path): Plan
}

object Frontend extends LazyLogging {
  def fromConfig(configName: String = "jove"): Map[String, Frontend] = {
    val config = ConfigFactory load configName

    def configMap(c: Configg, path: String): Map[String, ConfigValue] = {
      import scala.collection.JavaConverters._

      if (c.hasPath(path))
        (c.getObject(path): java.util.Map[String, ConfigValue]).asScala.toMap
      else
        Map.empty
    }

    val runtimeMirror = scala.reflect.runtime.universe runtimeMirror getClass.getClassLoader
    import runtimeMirror.{reflectModule, staticModule}

    for ((frontendId, v: ConfigObject) <- configMap(config, "jove.frontend"))
      yield frontendId -> reflectModule(staticModule(v.toConfig getString "frontend")).instance.asInstanceOf[jove.notebook.Frontend]
  }

  def default(configName: String = "jove"): Option[Frontend] = {
    val m = fromConfig(configName)

    if (m.isEmpty) {
      logger debug s"No frontend found"
      None
    } else if (m.size == 1)
      Some(m.head._2)
    else {
      logger debug s"Several frontends found: ${m.keys.toList.sorted mkString ", "}"

      def _default: Frontend = {
        val f = m.minBy(_._1)
        logger warn s"No default frontend specified, using ${f._1}"
        Console.err println s"Warning: no default frontend specified, using ${f._1}"
        f._2
      }

      Some {
        val config = ConfigFactory load configName
        if (config hasPath "jove.frontend.default")
          m.get(config getString "jove.frontend.default") match {
            case Some(frontend) => frontend
            case None => _default
          }
        else
          _default
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy