2010-03-30 08:21:55 -04:00
|
|
|
require 'psych.so'
|
2010-03-28 17:49:37 -04:00
|
|
|
require 'psych/nodes'
|
2011-02-21 13:09:38 -05:00
|
|
|
require 'psych/streaming'
|
2010-03-28 17:49:37 -04:00
|
|
|
require 'psych/visitors'
|
|
|
|
require 'psych/handler'
|
|
|
|
require 'psych/tree_builder'
|
|
|
|
require 'psych/parser'
|
|
|
|
require 'psych/omap'
|
|
|
|
require 'psych/set'
|
|
|
|
require 'psych/coder'
|
|
|
|
require 'psych/core_ext'
|
2010-04-09 16:33:21 -04:00
|
|
|
require 'psych/deprecated'
|
2011-11-21 14:47:09 -05:00
|
|
|
require 'psych/stream'
|
|
|
|
require 'psych/json/tree_builder'
|
|
|
|
require 'psych/json/stream'
|
2012-03-08 16:21:52 -05:00
|
|
|
require 'psych/handlers/document_stream'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
# = Overview
|
|
|
|
#
|
|
|
|
# Psych is a YAML parser and emitter. Psych leverages
|
|
|
|
# libyaml[http://libyaml.org] for it's YAML parsing and emitting capabilities.
|
|
|
|
# In addition to wrapping libyaml, Psych also knows how to serialize and
|
|
|
|
# de-serialize most Ruby objects to and from the YAML format.
|
|
|
|
#
|
|
|
|
# = I NEED TO PARSE OR EMIT YAML RIGHT NOW!
|
|
|
|
#
|
|
|
|
# # Parse some YAML
|
|
|
|
# Psych.load("--- foo") # => "foo"
|
|
|
|
#
|
|
|
|
# # Emit some YAML
|
|
|
|
# Psych.dump("foo") # => "--- foo\n...\n"
|
|
|
|
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
|
|
|
|
#
|
|
|
|
# Got more time on your hands? Keep on reading!
|
|
|
|
#
|
|
|
|
# == YAML Parsing
|
|
|
|
#
|
|
|
|
# Psych provides a range of interfaces for parsing a YAML document ranging from
|
|
|
|
# low level to high level, depending on your parsing needs. At the lowest
|
|
|
|
# level, is an event based parser. Mid level is access to the raw YAML AST,
|
|
|
|
# and at the highest level is the ability to unmarshal YAML to ruby objects.
|
|
|
|
#
|
|
|
|
# === Low level parsing
|
|
|
|
#
|
|
|
|
# The lowest level parser should be used when the YAML input is already known,
|
|
|
|
# and the developer does not want to pay the price of building an AST or
|
|
|
|
# automatic detection and conversion to ruby objects. See Psych::Parser for
|
|
|
|
# more information on using the event based parser.
|
|
|
|
#
|
|
|
|
# === Mid level parsing
|
|
|
|
#
|
|
|
|
# Psych provides access to an AST produced from parsing a YAML document. This
|
|
|
|
# tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can
|
|
|
|
# be examined and manipulated freely. Please see Psych::parse_stream,
|
|
|
|
# Psych::Nodes, and Psych::Nodes::Node for more information on dealing with
|
|
|
|
# YAML syntax trees.
|
|
|
|
#
|
|
|
|
# === High level parsing
|
|
|
|
#
|
|
|
|
# The high level YAML parser provided by Psych simply takes YAML as input and
|
|
|
|
# returns a Ruby data structure. For information on using the high level parser
|
|
|
|
# see Psych.load
|
|
|
|
#
|
|
|
|
# == YAML Emitting
|
|
|
|
#
|
|
|
|
# Psych provides a range of interfaces ranging from low to high level for
|
|
|
|
# producing YAML documents. Very similar to the YAML parsing interfaces, Psych
|
|
|
|
# provides at the lowest level, an event based system, mid-level is building
|
|
|
|
# a YAML AST, and the highest level is converting a Ruby object straight to
|
|
|
|
# a YAML document.
|
|
|
|
#
|
|
|
|
# === Low level emitting
|
|
|
|
#
|
|
|
|
# The lowest level emitter is an event based system. Events are sent to a
|
|
|
|
# Psych::Emitter object. That object knows how to convert the events to a YAML
|
|
|
|
# document. This interface should be used when document format is known in
|
|
|
|
# advance or speed is a concern. See Psych::Emitter for more information.
|
|
|
|
#
|
|
|
|
# === Mid level emitting
|
|
|
|
#
|
|
|
|
# At the mid level is building an AST. This AST is exactly the same as the AST
|
|
|
|
# used when parsing a YAML document. Users can build an AST by hand and the
|
|
|
|
# AST knows how to emit itself as a YAML document. See Psych::Nodes,
|
|
|
|
# Psych::Nodes::Node, and Psych::TreeBuilder for more information on building
|
|
|
|
# a YAML AST.
|
|
|
|
#
|
|
|
|
# === High level emitting
|
|
|
|
#
|
|
|
|
# The high level emitter has the easiest interface. Psych simply takes a Ruby
|
|
|
|
# data structure and converts it to a YAML document. See Psych.dump for more
|
|
|
|
# information on dumping a Ruby data structure.
|
|
|
|
|
|
|
|
module Psych
|
|
|
|
# The version is Psych you're using
|
2011-10-03 17:44:38 -04:00
|
|
|
VERSION = '1.2.2'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
# The version of libyaml Psych is using
|
|
|
|
LIBYAML_VERSION = Psych.libyaml_version.join '.'
|
|
|
|
|
2010-05-18 23:24:13 -04:00
|
|
|
class Exception < RuntimeError
|
|
|
|
end
|
|
|
|
|
2011-07-18 00:38:37 -04:00
|
|
|
class BadAlias < Exception
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
###
|
|
|
|
# Load +yaml+ in to a Ruby data structure. If multiple documents are
|
|
|
|
# provided, the object contained in the first document will be returned.
|
2011-12-06 18:12:37 -05:00
|
|
|
# +filename+ will be used in the exception message if any exception is raised
|
|
|
|
# while parsing.
|
|
|
|
#
|
|
|
|
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2011-12-06 18:12:37 -05:00
|
|
|
# Psych.load("--- a") # => 'a'
|
|
|
|
# Psych.load("---\n - a\n - b") # => ['a', 'b']
|
|
|
|
#
|
|
|
|
# begin
|
|
|
|
# Psych.load("--- `", "file.txt")
|
|
|
|
# rescue Psych::SyntaxError => ex
|
|
|
|
# ex.file # => 'file.txt'
|
|
|
|
# ex.message # => "(foo.txt): found character that cannot start any token"
|
|
|
|
# end
|
|
|
|
def self.load yaml, filename = nil
|
|
|
|
result = parse(yaml, filename)
|
2010-03-29 19:57:25 -04:00
|
|
|
result ? result.to_ruby : result
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Parse a YAML string in +yaml+. Returns the first object of a YAML AST.
|
2011-12-06 18:12:37 -05:00
|
|
|
# +filename+ is used in the exception message if a Psych::SyntaxError is
|
|
|
|
# raised.
|
|
|
|
#
|
|
|
|
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2010-03-29 16:33:22 -04:00
|
|
|
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Sequence:0x00>
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
2011-12-06 18:12:37 -05:00
|
|
|
# begin
|
|
|
|
# Psych.parse("--- `", "file.txt")
|
|
|
|
# rescue Psych::SyntaxError => ex
|
|
|
|
# ex.file # => 'file.txt'
|
|
|
|
# ex.message # => "(foo.txt): found character that cannot start any token"
|
|
|
|
# end
|
|
|
|
#
|
2010-03-28 17:49:37 -04:00
|
|
|
# See Psych::Nodes for more information about YAML AST.
|
2011-12-06 18:12:37 -05:00
|
|
|
def self.parse yaml, filename = nil
|
|
|
|
children = parse_stream(yaml, filename).children
|
2010-03-29 19:57:25 -04:00
|
|
|
children.empty? ? false : children.first.children.first
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Parse a file at +filename+. Returns the YAML AST.
|
2011-12-06 18:12:37 -05:00
|
|
|
#
|
|
|
|
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
2010-03-28 17:49:37 -04:00
|
|
|
def self.parse_file filename
|
2012-02-27 20:30:15 -05:00
|
|
|
File.open filename, 'r:bom|utf-8' do |f|
|
2011-12-06 18:12:37 -05:00
|
|
|
parse f, filename
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Returns a default parser
|
|
|
|
def self.parser
|
|
|
|
Psych::Parser.new(TreeBuilder.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Parse a YAML string in +yaml+. Returns the full AST for the YAML document.
|
|
|
|
# This method can handle multiple YAML documents contained in +yaml+.
|
2011-12-06 18:12:37 -05:00
|
|
|
# +filename+ is used in the exception message if a Psych::SyntaxError is
|
|
|
|
# raised.
|
|
|
|
#
|
2012-03-08 16:21:52 -05:00
|
|
|
# If a block is given, a Psych::Nodes::Document node will be yielded to the
|
|
|
|
# block as it's being parsed.
|
|
|
|
#
|
2011-12-06 18:12:37 -05:00
|
|
|
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2010-03-29 16:33:22 -04:00
|
|
|
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
2012-03-08 16:21:52 -05:00
|
|
|
# Psych.parse_stream("--- a\n--- b") do |node|
|
|
|
|
# node # => #<Psych::Nodes::Document:0x00>
|
|
|
|
# end
|
|
|
|
#
|
2011-12-06 18:12:37 -05:00
|
|
|
# begin
|
|
|
|
# Psych.parse_stream("--- `", "file.txt")
|
|
|
|
# rescue Psych::SyntaxError => ex
|
|
|
|
# ex.file # => 'file.txt'
|
|
|
|
# ex.message # => "(foo.txt): found character that cannot start any token"
|
|
|
|
# end
|
|
|
|
#
|
2010-03-28 17:49:37 -04:00
|
|
|
# See Psych::Nodes for more information about YAML AST.
|
2012-03-08 16:21:52 -05:00
|
|
|
def self.parse_stream yaml, filename = nil, &block
|
|
|
|
if block_given?
|
|
|
|
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
|
|
|
|
parser.parse yaml, filename
|
|
|
|
else
|
|
|
|
parser = self.parser
|
|
|
|
parser.parse yaml, filename
|
|
|
|
parser.handler.root
|
|
|
|
end
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
2010-07-07 19:05:45 -04:00
|
|
|
# call-seq:
|
|
|
|
# Psych.dump(o) -> string of yaml
|
|
|
|
# Psych.dump(o, options) -> string of yaml
|
|
|
|
# Psych.dump(o, io) -> io object passed in
|
|
|
|
# Psych.dump(o, io, options) -> io object passed in
|
|
|
|
#
|
|
|
|
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
|
|
|
|
# to control the output format. If an IO object is passed in, the YAML will
|
|
|
|
# be dumped to that IO object.
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2010-07-07 19:05:45 -04:00
|
|
|
# # Dump an array, get back a YAML string
|
2010-03-28 17:49:37 -04:00
|
|
|
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
|
2010-07-07 19:05:45 -04:00
|
|
|
#
|
|
|
|
# # Dump an array to an IO object
|
|
|
|
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
|
|
|
|
#
|
|
|
|
# # Dump an array with indentation set
|
|
|
|
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
|
|
|
|
#
|
|
|
|
# # Dump an array to an IO with indentation set
|
|
|
|
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
|
2010-03-31 17:09:58 -04:00
|
|
|
def self.dump o, io = nil, options = {}
|
|
|
|
if Hash === io
|
|
|
|
options = io
|
|
|
|
io = nil
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
visitor = Psych::Visitors::YAMLTree.new options
|
|
|
|
visitor << o
|
2011-10-03 17:31:43 -04:00
|
|
|
visitor.tree.yaml io, options
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Dump a list of objects as separate documents to a document stream.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
|
|
|
|
def self.dump_stream *objects
|
|
|
|
visitor = Psych::Visitors::YAMLTree.new {}
|
|
|
|
objects.each do |o|
|
|
|
|
visitor << o
|
|
|
|
end
|
2011-10-03 17:31:43 -04:00
|
|
|
visitor.tree.yaml
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Dump Ruby object +o+ to a JSON string.
|
|
|
|
def self.to_json o
|
2010-04-24 18:23:37 -04:00
|
|
|
visitor = Psych::Visitors::JSONTree.new
|
2010-03-28 17:49:37 -04:00
|
|
|
visitor << o
|
2011-10-03 17:31:43 -04:00
|
|
|
visitor.tree.yaml
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Load multiple documents given in +yaml+. Returns the parsed documents
|
2012-03-08 16:21:52 -05:00
|
|
|
# as a list. If a block is given, each document will be converted to ruby
|
|
|
|
# and passed to the block during parsing
|
|
|
|
#
|
|
|
|
# Example:
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
2010-04-23 20:20:08 -04:00
|
|
|
# Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
2012-03-08 16:21:52 -05:00
|
|
|
# list = []
|
|
|
|
# Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby|
|
|
|
|
# list << ruby
|
|
|
|
# end
|
|
|
|
# list # => ['foo', 'bar']
|
|
|
|
#
|
2011-12-06 18:12:37 -05:00
|
|
|
def self.load_stream yaml, filename = nil
|
2012-03-08 16:21:52 -05:00
|
|
|
if block_given?
|
|
|
|
parse_stream(yaml, filename) do |node|
|
|
|
|
yield node.to_ruby
|
|
|
|
end
|
|
|
|
else
|
|
|
|
parse_stream(yaml, filename).children.map { |child| child.to_ruby }
|
|
|
|
end
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# Load the document contained in +filename+. Returns the yaml contained in
|
|
|
|
# +filename+ as a ruby object
|
|
|
|
def self.load_file filename
|
2012-02-27 20:30:15 -05:00
|
|
|
File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename }
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
@domain_types = {}
|
|
|
|
def self.add_domain_type domain, type_tag, &block
|
2010-04-24 00:11:27 -04:00
|
|
|
key = ['tag', domain, type_tag].join ':'
|
|
|
|
@domain_types[key] = [key, block]
|
|
|
|
@domain_types["tag:#{type_tag}"] = [key, block]
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_builtin_type type_tag, &block
|
2010-04-24 00:11:27 -04:00
|
|
|
domain = 'yaml.org,2002'
|
|
|
|
key = ['tag', domain, type_tag].join ':'
|
|
|
|
@domain_types[key] = [key, block]
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def self.remove_type type_tag
|
|
|
|
@domain_types.delete type_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
@load_tags = {}
|
|
|
|
@dump_tags = {}
|
|
|
|
def self.add_tag tag, klass
|
|
|
|
@load_tags[tag] = klass
|
|
|
|
@dump_tags[klass] = tag
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :load_tags
|
|
|
|
attr_accessor :dump_tags
|
|
|
|
attr_accessor :domain_types
|
|
|
|
end
|
|
|
|
# :startdoc:
|
|
|
|
end
|