Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: yaml.rb
# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
[0] Fix | Delete
# $Id: yaml.rb 16084 2008-04-19 11:45:39Z knu $
[1] Fix | Delete
#
[2] Fix | Delete
# = yaml.rb: top-level module with methods for loading and parsing YAML documents
[3] Fix | Delete
#
[4] Fix | Delete
# Author:: why the lucky stiff
[5] Fix | Delete
#
[6] Fix | Delete
[7] Fix | Delete
require 'stringio'
[8] Fix | Delete
require 'yaml/error'
[9] Fix | Delete
require 'yaml/syck'
[10] Fix | Delete
require 'yaml/tag'
[11] Fix | Delete
require 'yaml/stream'
[12] Fix | Delete
require 'yaml/constants'
[13] Fix | Delete
[14] Fix | Delete
# == YAML
[15] Fix | Delete
#
[16] Fix | Delete
# YAML(tm) (rhymes with 'camel') is a
[17] Fix | Delete
# straightforward machine parsable data serialization format designed for
[18] Fix | Delete
# human readability and interaction with scripting languages such as Perl
[19] Fix | Delete
# and Python. YAML is optimized for data serialization, formatted
[20] Fix | Delete
# dumping, configuration files, log files, Internet messaging and
[21] Fix | Delete
# filtering. This specification describes the YAML information model and
[22] Fix | Delete
# serialization format. Together with the Unicode standard for characters, it
[23] Fix | Delete
# provides all the information necessary to understand YAML Version 1.0
[24] Fix | Delete
# and construct computer programs to process it.
[25] Fix | Delete
#
[26] Fix | Delete
# See http://yaml.org/ for more information. For a quick tutorial, please
[27] Fix | Delete
# visit YAML In Five Minutes (http://yaml.kwiki.org/?YamlInFiveMinutes).
[28] Fix | Delete
#
[29] Fix | Delete
# == About This Library
[30] Fix | Delete
#
[31] Fix | Delete
# The YAML 1.0 specification outlines four stages of YAML loading and dumping.
[32] Fix | Delete
# This library honors all four of those stages, although data is really only
[33] Fix | Delete
# available to you in three stages.
[34] Fix | Delete
#
[35] Fix | Delete
# The four stages are: native, representation, serialization, and presentation.
[36] Fix | Delete
#
[37] Fix | Delete
# The native stage refers to data which has been loaded completely into Ruby's
[38] Fix | Delete
# own types. (See +YAML::load+.)
[39] Fix | Delete
#
[40] Fix | Delete
# The representation stage means data which has been composed into
[41] Fix | Delete
# +YAML::BaseNode+ objects. In this stage, the document is available as a
[42] Fix | Delete
# tree of node objects. You can perform YPath queries and transformations
[43] Fix | Delete
# at this level. (See +YAML::parse+.)
[44] Fix | Delete
#
[45] Fix | Delete
# The serialization stage happens inside the parser. The YAML parser used in
[46] Fix | Delete
# Ruby is called Syck. Serialized nodes are available in the extension as
[47] Fix | Delete
# SyckNode structs.
[48] Fix | Delete
#
[49] Fix | Delete
# The presentation stage is the YAML document itself. This is accessible
[50] Fix | Delete
# to you as a string. (See +YAML::dump+.)
[51] Fix | Delete
#
[52] Fix | Delete
# For more information about the various information models, see Chapter
[53] Fix | Delete
# 3 of the YAML 1.0 Specification (http://yaml.org/spec/#id2491269).
[54] Fix | Delete
#
[55] Fix | Delete
# The YAML module provides quick access to the most common loading (YAML::load)
[56] Fix | Delete
# and dumping (YAML::dump) tasks. This module also provides an API for registering
[57] Fix | Delete
# global types (YAML::add_domain_type).
[58] Fix | Delete
#
[59] Fix | Delete
# == Example
[60] Fix | Delete
#
[61] Fix | Delete
# A simple round-trip (load and dump) of an object.
[62] Fix | Delete
#
[63] Fix | Delete
# require "yaml"
[64] Fix | Delete
#
[65] Fix | Delete
# test_obj = ["dogs", "cats", "badgers"]
[66] Fix | Delete
#
[67] Fix | Delete
# yaml_obj = YAML::dump( test_obj )
[68] Fix | Delete
# # -> ---
[69] Fix | Delete
# - dogs
[70] Fix | Delete
# - cats
[71] Fix | Delete
# - badgers
[72] Fix | Delete
# ruby_obj = YAML::load( yaml_obj )
[73] Fix | Delete
# # => ["dogs", "cats", "badgers"]
[74] Fix | Delete
# ruby_obj == test_obj
[75] Fix | Delete
# # => true
[76] Fix | Delete
#
[77] Fix | Delete
# To register your custom types with the global resolver, use +add_domain_type+.
[78] Fix | Delete
#
[79] Fix | Delete
# YAML::add_domain_type( "your-site.com,2004", "widget" ) do |type, val|
[80] Fix | Delete
# Widget.new( val )
[81] Fix | Delete
# end
[82] Fix | Delete
#
[83] Fix | Delete
module YAML
[84] Fix | Delete
[85] Fix | Delete
Resolver = YAML::Syck::Resolver
[86] Fix | Delete
DefaultResolver = YAML::Syck::DefaultResolver
[87] Fix | Delete
DefaultResolver.use_types_at( @@tagged_classes )
[88] Fix | Delete
GenericResolver = YAML::Syck::GenericResolver
[89] Fix | Delete
Parser = YAML::Syck::Parser
[90] Fix | Delete
Emitter = YAML::Syck::Emitter
[91] Fix | Delete
[92] Fix | Delete
# Returns a new default parser
[93] Fix | Delete
def YAML.parser; Parser.new.set_resolver( YAML.resolver ); end
[94] Fix | Delete
# Returns a new generic parser
[95] Fix | Delete
def YAML.generic_parser; Parser.new.set_resolver( GenericResolver ); end
[96] Fix | Delete
# Returns the default resolver
[97] Fix | Delete
def YAML.resolver; DefaultResolver; end
[98] Fix | Delete
# Returns a new default emitter
[99] Fix | Delete
def YAML.emitter; Emitter.new.set_resolver( YAML.resolver ); end
[100] Fix | Delete
[101] Fix | Delete
#
[102] Fix | Delete
# Converts _obj_ to YAML and writes the YAML result to _io_.
[103] Fix | Delete
#
[104] Fix | Delete
# File.open( 'animals.yaml', 'w' ) do |out|
[105] Fix | Delete
# YAML.dump( ['badger', 'elephant', 'tiger'], out )
[106] Fix | Delete
# end
[107] Fix | Delete
#
[108] Fix | Delete
# If no _io_ is provided, a string containing the dumped YAML
[109] Fix | Delete
# is returned.
[110] Fix | Delete
#
[111] Fix | Delete
# YAML.dump( :locked )
[112] Fix | Delete
# #=> "--- :locked"
[113] Fix | Delete
#
[114] Fix | Delete
def YAML.dump( obj, io = nil )
[115] Fix | Delete
obj.to_yaml( io || io2 = StringIO.new )
[116] Fix | Delete
io || ( io2.rewind; io2.read )
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
#
[120] Fix | Delete
# Load a document from the current _io_ stream.
[121] Fix | Delete
#
[122] Fix | Delete
# File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) }
[123] Fix | Delete
# #=> ['badger', 'elephant', 'tiger']
[124] Fix | Delete
#
[125] Fix | Delete
# Can also load from a string.
[126] Fix | Delete
#
[127] Fix | Delete
# YAML.load( "--- :locked" )
[128] Fix | Delete
# #=> :locked
[129] Fix | Delete
#
[130] Fix | Delete
def YAML.load( io )
[131] Fix | Delete
yp = parser.load( io )
[132] Fix | Delete
end
[133] Fix | Delete
[134] Fix | Delete
#
[135] Fix | Delete
# Load a document from the file located at _filepath_.
[136] Fix | Delete
#
[137] Fix | Delete
# YAML.load_file( 'animals.yaml' )
[138] Fix | Delete
# #=> ['badger', 'elephant', 'tiger']
[139] Fix | Delete
#
[140] Fix | Delete
def YAML.load_file( filepath )
[141] Fix | Delete
File.open( filepath ) do |f|
[142] Fix | Delete
load( f )
[143] Fix | Delete
end
[144] Fix | Delete
end
[145] Fix | Delete
[146] Fix | Delete
#
[147] Fix | Delete
# Parse the first document from the current _io_ stream
[148] Fix | Delete
#
[149] Fix | Delete
# File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) }
[150] Fix | Delete
# #=> #<YAML::Syck::Node:0x82ccce0
[151] Fix | Delete
# @kind=:seq,
[152] Fix | Delete
# @value=
[153] Fix | Delete
# [#<YAML::Syck::Node:0x82ccd94
[154] Fix | Delete
# @kind=:scalar,
[155] Fix | Delete
# @type_id="str",
[156] Fix | Delete
# @value="badger">,
[157] Fix | Delete
# #<YAML::Syck::Node:0x82ccd58
[158] Fix | Delete
# @kind=:scalar,
[159] Fix | Delete
# @type_id="str",
[160] Fix | Delete
# @value="elephant">,
[161] Fix | Delete
# #<YAML::Syck::Node:0x82ccd1c
[162] Fix | Delete
# @kind=:scalar,
[163] Fix | Delete
# @type_id="str",
[164] Fix | Delete
# @value="tiger">]>
[165] Fix | Delete
#
[166] Fix | Delete
# Can also load from a string.
[167] Fix | Delete
#
[168] Fix | Delete
# YAML.parse( "--- :locked" )
[169] Fix | Delete
# #=> #<YAML::Syck::Node:0x82edddc
[170] Fix | Delete
# @type_id="tag:ruby.yaml.org,2002:sym",
[171] Fix | Delete
# @value=":locked", @kind=:scalar>
[172] Fix | Delete
#
[173] Fix | Delete
def YAML.parse( io )
[174] Fix | Delete
yp = generic_parser.load( io )
[175] Fix | Delete
end
[176] Fix | Delete
[177] Fix | Delete
#
[178] Fix | Delete
# Parse a document from the file located at _filepath_.
[179] Fix | Delete
#
[180] Fix | Delete
# YAML.parse_file( 'animals.yaml' )
[181] Fix | Delete
# #=> #<YAML::Syck::Node:0x82ccce0
[182] Fix | Delete
# @kind=:seq,
[183] Fix | Delete
# @value=
[184] Fix | Delete
# [#<YAML::Syck::Node:0x82ccd94
[185] Fix | Delete
# @kind=:scalar,
[186] Fix | Delete
# @type_id="str",
[187] Fix | Delete
# @value="badger">,
[188] Fix | Delete
# #<YAML::Syck::Node:0x82ccd58
[189] Fix | Delete
# @kind=:scalar,
[190] Fix | Delete
# @type_id="str",
[191] Fix | Delete
# @value="elephant">,
[192] Fix | Delete
# #<YAML::Syck::Node:0x82ccd1c
[193] Fix | Delete
# @kind=:scalar,
[194] Fix | Delete
# @type_id="str",
[195] Fix | Delete
# @value="tiger">]>
[196] Fix | Delete
#
[197] Fix | Delete
def YAML.parse_file( filepath )
[198] Fix | Delete
File.open( filepath ) do |f|
[199] Fix | Delete
parse( f )
[200] Fix | Delete
end
[201] Fix | Delete
end
[202] Fix | Delete
[203] Fix | Delete
#
[204] Fix | Delete
# Calls _block_ with each consecutive document in the YAML
[205] Fix | Delete
# stream contained in _io_.
[206] Fix | Delete
#
[207] Fix | Delete
# File.open( 'many-docs.yaml' ) do |yf|
[208] Fix | Delete
# YAML.each_document( yf ) do |ydoc|
[209] Fix | Delete
# ## ydoc contains the single object
[210] Fix | Delete
# ## from the YAML document
[211] Fix | Delete
# end
[212] Fix | Delete
# end
[213] Fix | Delete
#
[214] Fix | Delete
def YAML.each_document( io, &block )
[215] Fix | Delete
yp = parser.load_documents( io, &block )
[216] Fix | Delete
end
[217] Fix | Delete
[218] Fix | Delete
#
[219] Fix | Delete
# Calls _block_ with each consecutive document in the YAML
[220] Fix | Delete
# stream contained in _io_.
[221] Fix | Delete
#
[222] Fix | Delete
# File.open( 'many-docs.yaml' ) do |yf|
[223] Fix | Delete
# YAML.load_documents( yf ) do |ydoc|
[224] Fix | Delete
# ## ydoc contains the single object
[225] Fix | Delete
# ## from the YAML document
[226] Fix | Delete
# end
[227] Fix | Delete
# end
[228] Fix | Delete
#
[229] Fix | Delete
def YAML.load_documents( io, &doc_proc )
[230] Fix | Delete
YAML.each_document( io, &doc_proc )
[231] Fix | Delete
end
[232] Fix | Delete
[233] Fix | Delete
#
[234] Fix | Delete
# Calls _block_ with a tree of +YAML::BaseNodes+, one tree for
[235] Fix | Delete
# each consecutive document in the YAML stream contained in _io_.
[236] Fix | Delete
#
[237] Fix | Delete
# File.open( 'many-docs.yaml' ) do |yf|
[238] Fix | Delete
# YAML.each_node( yf ) do |ydoc|
[239] Fix | Delete
# ## ydoc contains a tree of nodes
[240] Fix | Delete
# ## from the YAML document
[241] Fix | Delete
# end
[242] Fix | Delete
# end
[243] Fix | Delete
#
[244] Fix | Delete
def YAML.each_node( io, &doc_proc )
[245] Fix | Delete
yp = generic_parser.load_documents( io, &doc_proc )
[246] Fix | Delete
end
[247] Fix | Delete
[248] Fix | Delete
#
[249] Fix | Delete
# Calls _block_ with a tree of +YAML::BaseNodes+, one tree for
[250] Fix | Delete
# each consecutive document in the YAML stream contained in _io_.
[251] Fix | Delete
#
[252] Fix | Delete
# File.open( 'many-docs.yaml' ) do |yf|
[253] Fix | Delete
# YAML.parse_documents( yf ) do |ydoc|
[254] Fix | Delete
# ## ydoc contains a tree of nodes
[255] Fix | Delete
# ## from the YAML document
[256] Fix | Delete
# end
[257] Fix | Delete
# end
[258] Fix | Delete
#
[259] Fix | Delete
def YAML.parse_documents( io, &doc_proc )
[260] Fix | Delete
YAML.each_node( io, &doc_proc )
[261] Fix | Delete
end
[262] Fix | Delete
[263] Fix | Delete
#
[264] Fix | Delete
# Loads all documents from the current _io_ stream,
[265] Fix | Delete
# returning a +YAML::Stream+ object containing all
[266] Fix | Delete
# loaded documents.
[267] Fix | Delete
#
[268] Fix | Delete
def YAML.load_stream( io )
[269] Fix | Delete
d = nil
[270] Fix | Delete
parser.load_documents( io ) do |doc|
[271] Fix | Delete
d = YAML::Stream.new if not d
[272] Fix | Delete
d.add( doc )
[273] Fix | Delete
end
[274] Fix | Delete
return d
[275] Fix | Delete
end
[276] Fix | Delete
[277] Fix | Delete
#
[278] Fix | Delete
# Returns a YAML stream containing each of the items in +objs+,
[279] Fix | Delete
# each having their own document.
[280] Fix | Delete
#
[281] Fix | Delete
# YAML.dump_stream( 0, [], {} )
[282] Fix | Delete
# #=> --- 0
[283] Fix | Delete
# --- []
[284] Fix | Delete
# --- {}
[285] Fix | Delete
#
[286] Fix | Delete
def YAML.dump_stream( *objs )
[287] Fix | Delete
d = YAML::Stream.new
[288] Fix | Delete
objs.each do |doc|
[289] Fix | Delete
d.add( doc )
[290] Fix | Delete
end
[291] Fix | Delete
d.emit
[292] Fix | Delete
end
[293] Fix | Delete
[294] Fix | Delete
#
[295] Fix | Delete
# Add a global handler for a YAML domain type.
[296] Fix | Delete
#
[297] Fix | Delete
def YAML.add_domain_type( domain, type_tag, &transfer_proc )
[298] Fix | Delete
resolver.add_type( "tag:#{ domain }:#{ type_tag }", transfer_proc )
[299] Fix | Delete
end
[300] Fix | Delete
[301] Fix | Delete
#
[302] Fix | Delete
# Add a transfer method for a builtin type
[303] Fix | Delete
#
[304] Fix | Delete
def YAML.add_builtin_type( type_tag, &transfer_proc )
[305] Fix | Delete
resolver.add_type( "tag:yaml.org,2002:#{ type_tag }", transfer_proc )
[306] Fix | Delete
end
[307] Fix | Delete
[308] Fix | Delete
#
[309] Fix | Delete
# Add a transfer method for a builtin type
[310] Fix | Delete
#
[311] Fix | Delete
def YAML.add_ruby_type( type_tag, &transfer_proc )
[312] Fix | Delete
resolver.add_type( "tag:ruby.yaml.org,2002:#{ type_tag }", transfer_proc )
[313] Fix | Delete
end
[314] Fix | Delete
[315] Fix | Delete
#
[316] Fix | Delete
# Add a private document type
[317] Fix | Delete
#
[318] Fix | Delete
def YAML.add_private_type( type_re, &transfer_proc )
[319] Fix | Delete
resolver.add_type( "x-private:" + type_re, transfer_proc )
[320] Fix | Delete
end
[321] Fix | Delete
[322] Fix | Delete
#
[323] Fix | Delete
# Detect typing of a string
[324] Fix | Delete
#
[325] Fix | Delete
def YAML.detect_implicit( val )
[326] Fix | Delete
resolver.detect_implicit( val )
[327] Fix | Delete
end
[328] Fix | Delete
[329] Fix | Delete
#
[330] Fix | Delete
# Convert a type_id to a taguri
[331] Fix | Delete
#
[332] Fix | Delete
def YAML.tagurize( val )
[333] Fix | Delete
resolver.tagurize( val )
[334] Fix | Delete
end
[335] Fix | Delete
[336] Fix | Delete
#
[337] Fix | Delete
# Apply a transfer method to a Ruby object
[338] Fix | Delete
#
[339] Fix | Delete
def YAML.transfer( type_id, obj )
[340] Fix | Delete
resolver.transfer( YAML.tagurize( type_id ), obj )
[341] Fix | Delete
end
[342] Fix | Delete
[343] Fix | Delete
#
[344] Fix | Delete
# Apply any implicit a node may qualify for
[345] Fix | Delete
#
[346] Fix | Delete
def YAML.try_implicit( obj )
[347] Fix | Delete
YAML.transfer( YAML.detect_implicit( obj ), obj )
[348] Fix | Delete
end
[349] Fix | Delete
[350] Fix | Delete
#
[351] Fix | Delete
# Method to extract colon-seperated type and class, returning
[352] Fix | Delete
# the type and the constant of the class
[353] Fix | Delete
#
[354] Fix | Delete
def YAML.read_type_class( type, obj_class )
[355] Fix | Delete
scheme, domain, type, tclass = type.split( ':', 4 )
[356] Fix | Delete
tclass.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tclass
[357] Fix | Delete
return [ type, obj_class ]
[358] Fix | Delete
end
[359] Fix | Delete
[360] Fix | Delete
#
[361] Fix | Delete
# Allocate blank object
[362] Fix | Delete
#
[363] Fix | Delete
def YAML.object_maker( obj_class, val )
[364] Fix | Delete
if Hash === val
[365] Fix | Delete
o = obj_class.allocate
[366] Fix | Delete
val.each_pair { |k,v|
[367] Fix | Delete
o.instance_variable_set("@#{k}", v)
[368] Fix | Delete
}
[369] Fix | Delete
o
[370] Fix | Delete
else
[371] Fix | Delete
raise YAML::Error, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
[372] Fix | Delete
end
[373] Fix | Delete
end
[374] Fix | Delete
[375] Fix | Delete
#
[376] Fix | Delete
# Allocate an Emitter if needed
[377] Fix | Delete
#
[378] Fix | Delete
def YAML.quick_emit( oid, opts = {}, &e )
[379] Fix | Delete
out =
[380] Fix | Delete
if opts.is_a? YAML::Emitter
[381] Fix | Delete
opts
[382] Fix | Delete
else
[383] Fix | Delete
emitter.reset( opts )
[384] Fix | Delete
end
[385] Fix | Delete
oid =
[386] Fix | Delete
case oid when Fixnum, NilClass; oid
[387] Fix | Delete
else oid = "#{oid.object_id}-#{oid.hash}"
[388] Fix | Delete
end
[389] Fix | Delete
out.emit( oid, &e )
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
end
[393] Fix | Delete
[394] Fix | Delete
require 'yaml/rubytypes'
[395] Fix | Delete
require 'yaml/types'
[396] Fix | Delete
[397] Fix | Delete
module Kernel
[398] Fix | Delete
#
[399] Fix | Delete
# ryan:: You know how Kernel.p is a really convenient way to dump ruby
[400] Fix | Delete
# structures? The only downside is that it's not as legible as
[401] Fix | Delete
# YAML.
[402] Fix | Delete
#
[403] Fix | Delete
# _why:: (listening)
[404] Fix | Delete
#
[405] Fix | Delete
# ryan:: I know you don't want to urinate all over your users' namespaces.
[406] Fix | Delete
# But, on the other hand, convenience of dumping for debugging is,
[407] Fix | Delete
# IMO, a big YAML use case.
[408] Fix | Delete
#
[409] Fix | Delete
# _why:: Go nuts! Have a pony parade!
[410] Fix | Delete
#
[411] Fix | Delete
# ryan:: Either way, I certainly will have a pony parade.
[412] Fix | Delete
#
[413] Fix | Delete
[414] Fix | Delete
# Prints any supplied _objects_ out in YAML. Intended as
[415] Fix | Delete
# a variation on +Kernel::p+.
[416] Fix | Delete
#
[417] Fix | Delete
# S = Struct.new(:name, :state)
[418] Fix | Delete
# s = S['dave', 'TX']
[419] Fix | Delete
# y s
[420] Fix | Delete
#
[421] Fix | Delete
# _produces:_
[422] Fix | Delete
#
[423] Fix | Delete
# --- !ruby/struct:S
[424] Fix | Delete
# name: dave
[425] Fix | Delete
# state: TX
[426] Fix | Delete
#
[427] Fix | Delete
def y( object, *objects )
[428] Fix | Delete
objects.unshift object
[429] Fix | Delete
puts( if objects.length == 1
[430] Fix | Delete
YAML::dump( *objects )
[431] Fix | Delete
else
[432] Fix | Delete
YAML::dump_stream( *objects )
[433] Fix | Delete
end )
[434] Fix | Delete
end
[435] Fix | Delete
private :y
[436] Fix | Delete
end
[437] Fix | Delete
[438] Fix | Delete
[439] Fix | Delete
[440] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function