本文整理汇总了Scala中org.yaml.snakeyaml.Yaml类的典型用法代码示例。如果您正苦于以下问题:Scala Yaml类的具体用法?Scala Yaml怎么用?Scala Yaml使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Yaml类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ConfigLoader
//设置package包名称以及导入依赖的类
package co.teapot.tempest.util
import java.io.File
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import scala.io.BufferedSource
import scala.reflect.ClassTag
object ConfigLoader {
def loadConfig[T:ClassTag](source: BufferedSource): T = {
val yaml = new Yaml(new Constructor(implicitly[ClassTag[T]].runtimeClass))
yaml.load(source.getLines().mkString("\n")).asInstanceOf[T]
}
def loadConfig[T:ClassTag](configFile: File): T = {
if (configFile.exists) {
loadConfig(scala.io.Source.fromFile(configFile))
} else {
throw new Exception("Configuration file not found: " + configFile.getCanonicalPath)
}
}
def loadConfig[T:ClassTag](configFileName: String): T = {
loadConfig(new File(configFileName))
}
}
示例2: parse
//设置package包名称以及导入依赖的类
package com.box.castle
import com.box.castle.core.config.CastleConfig
import org.yaml.snakeyaml.Yaml
package object config {
def parse(config: java.util.LinkedHashMap[String, java.util.LinkedHashMap[String, Object]]): CastleConfig =
CastleConfigParser(config).castleConfig
def parse(yamlConfig: String): CastleConfig = {
val yaml = new Yaml()
val list = yaml.load(yamlConfig)
.asInstanceOf[java.util.LinkedHashMap[String, java.util.LinkedHashMap[String, Object]]]
this.parse(list)
}
}
示例3: getString
//设置package包名称以及导入依赖的类
package iotus.core
import java.io.{File, FileInputStream, InputStream}
import org.yaml.snakeyaml.Yaml
import scala.collection.JavaConversions._
def getString(name: String, section: String): String = {
getSection(section).get(name).get
}
def getSection(name: String): Map[String, String] = {
val obj = yamlConfig.get(name).get.asInstanceOf[java.util.LinkedHashMap[String, AnyRef]]
val section = mapAsScalaMap[String, AnyRef](obj).toMap
section.asInstanceOf[Map[String, String]]
}
}
object YamlConfig {
val CONFIG_PATH = "/opt/iotus/conf/iotus.yaml"
//def apply(): YamlConfig = {
// return new YamlConfig()
//}
}
示例4: BuildInfo
//设置package包名称以及导入依赖的类
package cjp.catalogue.util
import java.io.{InputStream, FileNotFoundException, FileInputStream, File}
import java.util
import org.yaml.snakeyaml.Yaml
import play.api.Play
import play.api.Play._
import scala.util.{Success, Try}
case class BuildInfo(buildNumber: Int, lastCommit: String)
object BuildInfo {
lazy val fromApplicationConfig : Option[BuildInfo] = {
Play.configuration.getString("buildInfoFile").flatMap {
case buildInfoFile => Try(BuildInfo(new File(buildInfoFile))) match {
case Success(info) => Some(info)
case _ => None
}
}
}
def apply(buildInfoFile: File): BuildInfo = {
try {
apply(new FileInputStream(buildInfoFile))
}
catch {
case e: FileNotFoundException => sys.error(e.getMessage)
}
}
def apply(buildInfo: InputStream): BuildInfo = {
try {
val BuildInfoMap = new Yaml().load(buildInfo).asInstanceOf[util.Map[String, Any]]
BuildInfo(
buildNumber = BuildInfoMap.get("buildNumber").asInstanceOf[Int],
lastCommit = BuildInfoMap.get("lastCommit").asInstanceOf[String]
)
}
catch {
case e: Exception => sys.error("failed to parse build info file")
}
}
}
示例5: Connector
//设置package包名称以及导入依赖的类
package com.messhall.dao
import java.sql.{Connection, DriverManager}
import java.io.{File, FileInputStream, InputStream}
import org.yaml.snakeyaml.Yaml
import scala.collection.JavaConverters._
object Connector {
val inFile: InputStream = new FileInputStream (new File ("etc/mysql_login"))
val yaml: Yaml = new Yaml
// tfw java: Convert java.util.Map -> scala.collection.mutable.Map
val configData = yaml.load(inFile).asInstanceOf[java.util.Map[String, String]].asScala
val url = configData.getOrElse("url", "jdbc:mysql://localhost:3305")
val dbName = configData.getOrElse("dbName", "no_name")
val driver = configData.getOrElse("driver", "com.mysql.jdbc.Driver")
val username = configData.getOrElse("username", "user") // NEVER DEFAULT TO ROOT
val password = configData.getOrElse("password", "hunter3")
try {
Class.forName(driver)
val connection: Connection = DriverManager.getConnection(url + dbName, username, password)
} catch {
case e: Exception => e.printStackTrace
}
}
示例6: YamlConfig
//设置package包名称以及导入依赖的类
package io.armee.config
import java.io.{File, FileInputStream}
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import scala.beans.BeanProperty
class YamlConfig {
@BeanProperty var shellPort = 1336
@BeanProperty var masterPort = 1337
@BeanProperty var workerPort = 1338
@BeanProperty var apiPort = 1335
@BeanProperty var masterServer = "127.0.0.1"
@BeanProperty var numExecutorsPerServer = 4
@BeanProperty var awsAccessKey = ""
@BeanProperty var awsSecretKey = ""
def readConfig() : YamlConfig = {
//load config yaml file, using some java code due to The Snake Yaml lib
val fileName = "config.yaml"
val classLoader = getClass().getClassLoader();
val input : FileInputStream =
try {
new FileInputStream(new File(fileName))
}
catch {
case x: java.io.FileNotFoundException => {
println("config.yaml in current dir not found. Using file in resource directory: ")
new FileInputStream(classLoader.getResource(fileName).getFile());
}
}
val yaml = new Yaml(new Constructor(classOf[YamlConfig]))
val e = yaml.load(input).asInstanceOf[YamlConfig]
println("Configuration loaded:")
println(e)
println("")
e
}
override def toString: String = s"executors : $numExecutorsPerServer, apiPort: $apiPort, shellPort: $shellPort, workerPort: $workerPort, masterPort: $masterPort, masterServer: $masterServer"
}
示例7: BuildInformation
//设置package包名称以及导入依赖的类
package conf
import java.io.{File, FileInputStream, FileNotFoundException, InputStream}
import java.util
import com.google.inject.{Inject, Singleton}
import org.yaml.snakeyaml.Yaml
import play.api.Configuration
import scala.util.{Success, Try}
case class BuildInformation(buildNumber: Int, lastCommit: String)
@Singleton
class BuildInfo @Inject() (configuration: Configuration) {
lazy val fromApplicationConfig: Option[BuildInformation] = {
val buildInfoFile = configuration.getString("buildInfoFile").get
Try(new File(buildInfoFile)).flatMap(buildInfo(_)) match {
case Success(info) => Some(info)
case _ => None
}
}
def buildInfo(buildInfoFile: File): Try[BuildInformation] = {
Try(new FileInputStream(buildInfoFile)).recover {
case e: FileNotFoundException => sys.error(e.getMessage)
} flatMap buildInfo
}
def buildInfo(buildInfo: InputStream): Try[BuildInformation] = {
Try {
val BuildInfoMap = new Yaml().load(buildInfo).asInstanceOf[util.Map[String, Any]]
BuildInformation(
buildNumber = BuildInfoMap.get("buildNumber").asInstanceOf[Int],
lastCommit = BuildInfoMap.get("lastCommit").asInstanceOf[String]
)
} recover {
case _: Exception => sys.error("failed to parse build info file")
}
}
}