Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: psych.rb
# frozen_string_literal: true
[0] Fix | Delete
require 'psych/versions'
[1] Fix | Delete
case RUBY_ENGINE
[2] Fix | Delete
when 'jruby'
[3] Fix | Delete
require 'psych_jars'
[4] Fix | Delete
if JRuby::Util.respond_to?(:load_ext)
[5] Fix | Delete
JRuby::Util.load_ext('org.jruby.ext.psych.PsychLibrary')
[6] Fix | Delete
else
[7] Fix | Delete
require 'java'; require 'jruby'
[8] Fix | Delete
org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
[9] Fix | Delete
end
[10] Fix | Delete
else
[11] Fix | Delete
require 'psych.so'
[12] Fix | Delete
end
[13] Fix | Delete
require 'psych/nodes'
[14] Fix | Delete
require 'psych/streaming'
[15] Fix | Delete
require 'psych/visitors'
[16] Fix | Delete
require 'psych/handler'
[17] Fix | Delete
require 'psych/tree_builder'
[18] Fix | Delete
require 'psych/parser'
[19] Fix | Delete
require 'psych/omap'
[20] Fix | Delete
require 'psych/set'
[21] Fix | Delete
require 'psych/coder'
[22] Fix | Delete
require 'psych/core_ext'
[23] Fix | Delete
require 'psych/stream'
[24] Fix | Delete
require 'psych/json/tree_builder'
[25] Fix | Delete
require 'psych/json/stream'
[26] Fix | Delete
require 'psych/handlers/document_stream'
[27] Fix | Delete
require 'psych/class_loader'
[28] Fix | Delete
[29] Fix | Delete
###
[30] Fix | Delete
# = Overview
[31] Fix | Delete
#
[32] Fix | Delete
# Psych is a YAML parser and emitter.
[33] Fix | Delete
# Psych leverages libyaml [Home page: https://pyyaml.org/wiki/LibYAML]
[34] Fix | Delete
# or [HG repo: https://bitbucket.org/xi/libyaml] for its YAML parsing
[35] Fix | Delete
# and emitting capabilities. In addition to wrapping libyaml, Psych also
[36] Fix | Delete
# knows how to serialize and de-serialize most Ruby objects to and from
[37] Fix | Delete
# the YAML format.
[38] Fix | Delete
#
[39] Fix | Delete
# = I NEED TO PARSE OR EMIT YAML RIGHT NOW!
[40] Fix | Delete
#
[41] Fix | Delete
# # Parse some YAML
[42] Fix | Delete
# Psych.load("--- foo") # => "foo"
[43] Fix | Delete
#
[44] Fix | Delete
# # Emit some YAML
[45] Fix | Delete
# Psych.dump("foo") # => "--- foo\n...\n"
[46] Fix | Delete
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
[47] Fix | Delete
#
[48] Fix | Delete
# Got more time on your hands? Keep on reading!
[49] Fix | Delete
#
[50] Fix | Delete
# == YAML Parsing
[51] Fix | Delete
#
[52] Fix | Delete
# Psych provides a range of interfaces for parsing a YAML document ranging from
[53] Fix | Delete
# low level to high level, depending on your parsing needs. At the lowest
[54] Fix | Delete
# level, is an event based parser. Mid level is access to the raw YAML AST,
[55] Fix | Delete
# and at the highest level is the ability to unmarshal YAML to Ruby objects.
[56] Fix | Delete
#
[57] Fix | Delete
# == YAML Emitting
[58] Fix | Delete
#
[59] Fix | Delete
# Psych provides a range of interfaces ranging from low to high level for
[60] Fix | Delete
# producing YAML documents. Very similar to the YAML parsing interfaces, Psych
[61] Fix | Delete
# provides at the lowest level, an event based system, mid-level is building
[62] Fix | Delete
# a YAML AST, and the highest level is converting a Ruby object straight to
[63] Fix | Delete
# a YAML document.
[64] Fix | Delete
#
[65] Fix | Delete
# == High-level API
[66] Fix | Delete
#
[67] Fix | Delete
# === Parsing
[68] Fix | Delete
#
[69] Fix | Delete
# The high level YAML parser provided by Psych simply takes YAML as input and
[70] Fix | Delete
# returns a Ruby data structure. For information on using the high level parser
[71] Fix | Delete
# see Psych.load
[72] Fix | Delete
#
[73] Fix | Delete
# ==== Reading from a string
[74] Fix | Delete
#
[75] Fix | Delete
# Psych.load("--- a") # => 'a'
[76] Fix | Delete
# Psych.load("---\n - a\n - b") # => ['a', 'b']
[77] Fix | Delete
#
[78] Fix | Delete
# ==== Reading from a file
[79] Fix | Delete
#
[80] Fix | Delete
# Psych.load_file("database.yml")
[81] Fix | Delete
#
[82] Fix | Delete
# ==== Exception handling
[83] Fix | Delete
#
[84] Fix | Delete
# begin
[85] Fix | Delete
# # The second argument changes only the exception contents
[86] Fix | Delete
# Psych.parse("--- `", "file.txt")
[87] Fix | Delete
# rescue Psych::SyntaxError => ex
[88] Fix | Delete
# ex.file # => 'file.txt'
[89] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[90] Fix | Delete
# end
[91] Fix | Delete
#
[92] Fix | Delete
# === Emitting
[93] Fix | Delete
#
[94] Fix | Delete
# The high level emitter has the easiest interface. Psych simply takes a Ruby
[95] Fix | Delete
# data structure and converts it to a YAML document. See Psych.dump for more
[96] Fix | Delete
# information on dumping a Ruby data structure.
[97] Fix | Delete
#
[98] Fix | Delete
# ==== Writing to a string
[99] Fix | Delete
#
[100] Fix | Delete
# # Dump an array, get back a YAML string
[101] Fix | Delete
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
[102] Fix | Delete
#
[103] Fix | Delete
# # Dump an array to an IO object
[104] Fix | Delete
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
[105] Fix | Delete
#
[106] Fix | Delete
# # Dump an array with indentation set
[107] Fix | Delete
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
[108] Fix | Delete
#
[109] Fix | Delete
# # Dump an array to an IO with indentation set
[110] Fix | Delete
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
[111] Fix | Delete
#
[112] Fix | Delete
# ==== Writing to a file
[113] Fix | Delete
#
[114] Fix | Delete
# Currently there is no direct API for dumping Ruby structure to file:
[115] Fix | Delete
#
[116] Fix | Delete
# File.open('database.yml', 'w') do |file|
[117] Fix | Delete
# file.write(Psych.dump(['a', 'b']))
[118] Fix | Delete
# end
[119] Fix | Delete
#
[120] Fix | Delete
# == Mid-level API
[121] Fix | Delete
#
[122] Fix | Delete
# === Parsing
[123] Fix | Delete
#
[124] Fix | Delete
# Psych provides access to an AST produced from parsing a YAML document. This
[125] Fix | Delete
# tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can
[126] Fix | Delete
# be examined and manipulated freely. Please see Psych::parse_stream,
[127] Fix | Delete
# Psych::Nodes, and Psych::Nodes::Node for more information on dealing with
[128] Fix | Delete
# YAML syntax trees.
[129] Fix | Delete
#
[130] Fix | Delete
# ==== Reading from a string
[131] Fix | Delete
#
[132] Fix | Delete
# # Returns Psych::Nodes::Stream
[133] Fix | Delete
# Psych.parse_stream("---\n - a\n - b")
[134] Fix | Delete
#
[135] Fix | Delete
# # Returns Psych::Nodes::Document
[136] Fix | Delete
# Psych.parse("---\n - a\n - b")
[137] Fix | Delete
#
[138] Fix | Delete
# ==== Reading from a file
[139] Fix | Delete
#
[140] Fix | Delete
# # Returns Psych::Nodes::Stream
[141] Fix | Delete
# Psych.parse_stream(File.read('database.yml'))
[142] Fix | Delete
#
[143] Fix | Delete
# # Returns Psych::Nodes::Document
[144] Fix | Delete
# Psych.parse_file('database.yml')
[145] Fix | Delete
#
[146] Fix | Delete
# ==== Exception handling
[147] Fix | Delete
#
[148] Fix | Delete
# begin
[149] Fix | Delete
# # The second argument changes only the exception contents
[150] Fix | Delete
# Psych.parse("--- `", "file.txt")
[151] Fix | Delete
# rescue Psych::SyntaxError => ex
[152] Fix | Delete
# ex.file # => 'file.txt'
[153] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[154] Fix | Delete
# end
[155] Fix | Delete
#
[156] Fix | Delete
# === Emitting
[157] Fix | Delete
#
[158] Fix | Delete
# At the mid level is building an AST. This AST is exactly the same as the AST
[159] Fix | Delete
# used when parsing a YAML document. Users can build an AST by hand and the
[160] Fix | Delete
# AST knows how to emit itself as a YAML document. See Psych::Nodes,
[161] Fix | Delete
# Psych::Nodes::Node, and Psych::TreeBuilder for more information on building
[162] Fix | Delete
# a YAML AST.
[163] Fix | Delete
#
[164] Fix | Delete
# ==== Writing to a string
[165] Fix | Delete
#
[166] Fix | Delete
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
[167] Fix | Delete
# stream = Psych.parse_stream("---\n - a\n - b")
[168] Fix | Delete
#
[169] Fix | Delete
# stream.to_yaml # => "---\n- a\n- b\n"
[170] Fix | Delete
#
[171] Fix | Delete
# ==== Writing to a file
[172] Fix | Delete
#
[173] Fix | Delete
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
[174] Fix | Delete
# stream = Psych.parse_stream(File.read('database.yml'))
[175] Fix | Delete
#
[176] Fix | Delete
# File.open('database.yml', 'w') do |file|
[177] Fix | Delete
# file.write(stream.to_yaml)
[178] Fix | Delete
# end
[179] Fix | Delete
#
[180] Fix | Delete
# == Low-level API
[181] Fix | Delete
#
[182] Fix | Delete
# === Parsing
[183] Fix | Delete
#
[184] Fix | Delete
# The lowest level parser should be used when the YAML input is already known,
[185] Fix | Delete
# and the developer does not want to pay the price of building an AST or
[186] Fix | Delete
# automatic detection and conversion to Ruby objects. See Psych::Parser for
[187] Fix | Delete
# more information on using the event based parser.
[188] Fix | Delete
#
[189] Fix | Delete
# ==== Reading to Psych::Nodes::Stream structure
[190] Fix | Delete
#
[191] Fix | Delete
# parser = Psych::Parser.new(TreeBuilder.new) # => #<Psych::Parser>
[192] Fix | Delete
# parser = Psych.parser # it's an alias for the above
[193] Fix | Delete
#
[194] Fix | Delete
# parser.parse("---\n - a\n - b") # => #<Psych::Parser>
[195] Fix | Delete
# parser.handler # => #<Psych::TreeBuilder>
[196] Fix | Delete
# parser.handler.root # => #<Psych::Nodes::Stream>
[197] Fix | Delete
#
[198] Fix | Delete
# ==== Receiving an events stream
[199] Fix | Delete
#
[200] Fix | Delete
# recorder = Psych::Handlers::Recorder.new
[201] Fix | Delete
# parser = Psych::Parser.new(recorder)
[202] Fix | Delete
#
[203] Fix | Delete
# parser.parse("---\n - a\n - b")
[204] Fix | Delete
# recorder.events # => [list of [event, args] lists]
[205] Fix | Delete
# # event is one of: Psych::Handler::EVENTS
[206] Fix | Delete
# # args are the arguments passed to the event
[207] Fix | Delete
#
[208] Fix | Delete
# === Emitting
[209] Fix | Delete
#
[210] Fix | Delete
# The lowest level emitter is an event based system. Events are sent to a
[211] Fix | Delete
# Psych::Emitter object. That object knows how to convert the events to a YAML
[212] Fix | Delete
# document. This interface should be used when document format is known in
[213] Fix | Delete
# advance or speed is a concern. See Psych::Emitter for more information.
[214] Fix | Delete
#
[215] Fix | Delete
# ==== Writing to a Ruby structure
[216] Fix | Delete
#
[217] Fix | Delete
# Psych.parser.parse("--- a") # => #<Psych::Parser>
[218] Fix | Delete
#
[219] Fix | Delete
# parser.handler.first # => #<Psych::Nodes::Stream>
[220] Fix | Delete
# parser.handler.first.to_ruby # => ["a"]
[221] Fix | Delete
#
[222] Fix | Delete
# parser.handler.root.first # => #<Psych::Nodes::Document>
[223] Fix | Delete
# parser.handler.root.first.to_ruby # => "a"
[224] Fix | Delete
#
[225] Fix | Delete
# # You can instantiate an Emitter manually
[226] Fix | Delete
# Psych::Visitors::ToRuby.new.accept(parser.handler.root.first)
[227] Fix | Delete
# # => "a"
[228] Fix | Delete
[229] Fix | Delete
module Psych
[230] Fix | Delete
# The version of libyaml Psych is using
[231] Fix | Delete
LIBYAML_VERSION = Psych.libyaml_version.join '.'
[232] Fix | Delete
# Deprecation guard
[233] Fix | Delete
NOT_GIVEN = Object.new
[234] Fix | Delete
private_constant :NOT_GIVEN
[235] Fix | Delete
[236] Fix | Delete
###
[237] Fix | Delete
# Load +yaml+ in to a Ruby data structure. If multiple documents are
[238] Fix | Delete
# provided, the object contained in the first document will be returned.
[239] Fix | Delete
# +filename+ will be used in the exception message if any exception
[240] Fix | Delete
# is raised while parsing. If +yaml+ is empty, it returns
[241] Fix | Delete
# the specified +fallback+ return value, which defaults to +false+.
[242] Fix | Delete
#
[243] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[244] Fix | Delete
#
[245] Fix | Delete
# Example:
[246] Fix | Delete
#
[247] Fix | Delete
# Psych.load("--- a") # => 'a'
[248] Fix | Delete
# Psych.load("---\n - a\n - b") # => ['a', 'b']
[249] Fix | Delete
#
[250] Fix | Delete
# begin
[251] Fix | Delete
# Psych.load("--- `", filename: "file.txt")
[252] Fix | Delete
# rescue Psych::SyntaxError => ex
[253] Fix | Delete
# ex.file # => 'file.txt'
[254] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[255] Fix | Delete
# end
[256] Fix | Delete
#
[257] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[258] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[259] Fix | Delete
#
[260] Fix | Delete
# Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
[261] Fix | Delete
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[262] Fix | Delete
#
[263] Fix | Delete
# Raises a TypeError when `yaml` parameter is NilClass
[264] Fix | Delete
#
[265] Fix | Delete
# NOTE: This method *should not* be used to parse untrusted documents, such as
[266] Fix | Delete
# YAML documents that are supplied via user input. Instead, please use the
[267] Fix | Delete
# safe_load method.
[268] Fix | Delete
#
[269] Fix | Delete
def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false
[270] Fix | Delete
if legacy_filename != NOT_GIVEN
[271] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load is deprecated. Use keyword argument like Psych.load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[272] Fix | Delete
filename = legacy_filename
[273] Fix | Delete
end
[274] Fix | Delete
[275] Fix | Delete
result = parse(yaml, filename: filename)
[276] Fix | Delete
return fallback unless result
[277] Fix | Delete
result = result.to_ruby if result
[278] Fix | Delete
symbolize_names!(result) if symbolize_names
[279] Fix | Delete
result
[280] Fix | Delete
end
[281] Fix | Delete
[282] Fix | Delete
###
[283] Fix | Delete
# Safely load the yaml string in +yaml+. By default, only the following
[284] Fix | Delete
# classes are allowed to be deserialized:
[285] Fix | Delete
#
[286] Fix | Delete
# * TrueClass
[287] Fix | Delete
# * FalseClass
[288] Fix | Delete
# * NilClass
[289] Fix | Delete
# * Numeric
[290] Fix | Delete
# * String
[291] Fix | Delete
# * Array
[292] Fix | Delete
# * Hash
[293] Fix | Delete
#
[294] Fix | Delete
# Recursive data structures are not allowed by default. Arbitrary classes
[295] Fix | Delete
# can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
[296] Fix | Delete
# additive. For example, to allow Date deserialization:
[297] Fix | Delete
#
[298] Fix | Delete
# Psych.safe_load(yaml, permitted_classes: [Date])
[299] Fix | Delete
#
[300] Fix | Delete
# Now the Date class can be loaded in addition to the classes listed above.
[301] Fix | Delete
#
[302] Fix | Delete
# Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
[303] Fix | Delete
# For example:
[304] Fix | Delete
#
[305] Fix | Delete
# x = []
[306] Fix | Delete
# x << x
[307] Fix | Delete
# yaml = Psych.dump x
[308] Fix | Delete
# Psych.safe_load yaml # => raises an exception
[309] Fix | Delete
# Psych.safe_load yaml, aliases: true # => loads the aliases
[310] Fix | Delete
#
[311] Fix | Delete
# A Psych::DisallowedClass exception will be raised if the yaml contains a
[312] Fix | Delete
# class that isn't in the +permitted_classes+ list.
[313] Fix | Delete
#
[314] Fix | Delete
# A Psych::BadAlias exception will be raised if the yaml contains aliases
[315] Fix | Delete
# but the +aliases+ keyword argument is set to false.
[316] Fix | Delete
#
[317] Fix | Delete
# +filename+ will be used in the exception message if any exception is raised
[318] Fix | Delete
# while parsing.
[319] Fix | Delete
#
[320] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[321] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[322] Fix | Delete
#
[323] Fix | Delete
# Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
[324] Fix | Delete
# Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[325] Fix | Delete
#
[326] Fix | Delete
def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false
[327] Fix | Delete
if legacy_permitted_classes != NOT_GIVEN
[328] Fix | Delete
warn_with_uplevel 'Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.', uplevel: 1 if $VERBOSE
[329] Fix | Delete
permitted_classes = legacy_permitted_classes
[330] Fix | Delete
end
[331] Fix | Delete
[332] Fix | Delete
if legacy_permitted_symbols != NOT_GIVEN
[333] Fix | Delete
warn_with_uplevel 'Passing permitted_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_symbols: ...) instead.', uplevel: 1 if $VERBOSE
[334] Fix | Delete
permitted_symbols = legacy_permitted_symbols
[335] Fix | Delete
end
[336] Fix | Delete
[337] Fix | Delete
if legacy_aliases != NOT_GIVEN
[338] Fix | Delete
warn_with_uplevel 'Passing aliases with the 4th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, aliases: ...) instead.', uplevel: 1 if $VERBOSE
[339] Fix | Delete
aliases = legacy_aliases
[340] Fix | Delete
end
[341] Fix | Delete
[342] Fix | Delete
if legacy_filename != NOT_GIVEN
[343] Fix | Delete
warn_with_uplevel 'Passing filename with the 5th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[344] Fix | Delete
filename = legacy_filename
[345] Fix | Delete
end
[346] Fix | Delete
[347] Fix | Delete
result = parse(yaml, filename: filename)
[348] Fix | Delete
return fallback unless result
[349] Fix | Delete
[350] Fix | Delete
class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
[351] Fix | Delete
permitted_symbols.map(&:to_s))
[352] Fix | Delete
scanner = ScalarScanner.new class_loader
[353] Fix | Delete
visitor = if aliases
[354] Fix | Delete
Visitors::ToRuby.new scanner, class_loader
[355] Fix | Delete
else
[356] Fix | Delete
Visitors::NoAliasRuby.new scanner, class_loader
[357] Fix | Delete
end
[358] Fix | Delete
result = visitor.accept result
[359] Fix | Delete
symbolize_names!(result) if symbolize_names
[360] Fix | Delete
result
[361] Fix | Delete
end
[362] Fix | Delete
[363] Fix | Delete
###
[364] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
[365] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[366] Fix | Delete
# raised.
[367] Fix | Delete
#
[368] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[369] Fix | Delete
#
[370] Fix | Delete
# Example:
[371] Fix | Delete
#
[372] Fix | Delete
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
[373] Fix | Delete
#
[374] Fix | Delete
# begin
[375] Fix | Delete
# Psych.parse("--- `", filename: "file.txt")
[376] Fix | Delete
# rescue Psych::SyntaxError => ex
[377] Fix | Delete
# ex.file # => 'file.txt'
[378] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[379] Fix | Delete
# end
[380] Fix | Delete
#
[381] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[382] Fix | Delete
def self.parse yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: NOT_GIVEN
[383] Fix | Delete
if legacy_filename != NOT_GIVEN
[384] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse is deprecated. Use keyword argument like Psych.parse(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[385] Fix | Delete
filename = legacy_filename
[386] Fix | Delete
end
[387] Fix | Delete
[388] Fix | Delete
parse_stream(yaml, filename: filename) do |node|
[389] Fix | Delete
return node
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
if fallback != NOT_GIVEN
[393] Fix | Delete
warn_with_uplevel 'Passing the `fallback` keyword argument of Psych.parse is deprecated.', uplevel: 1 if $VERBOSE
[394] Fix | Delete
fallback
[395] Fix | Delete
else
[396] Fix | Delete
false
[397] Fix | Delete
end
[398] Fix | Delete
end
[399] Fix | Delete
[400] Fix | Delete
###
[401] Fix | Delete
# Parse a file at +filename+. Returns the Psych::Nodes::Document.
[402] Fix | Delete
#
[403] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[404] Fix | Delete
def self.parse_file filename, fallback: false
[405] Fix | Delete
result = File.open filename, 'r:bom|utf-8' do |f|
[406] Fix | Delete
parse f, filename: filename
[407] Fix | Delete
end
[408] Fix | Delete
result || fallback
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
###
[412] Fix | Delete
# Returns a default parser
[413] Fix | Delete
def self.parser
[414] Fix | Delete
Psych::Parser.new(TreeBuilder.new)
[415] Fix | Delete
end
[416] Fix | Delete
[417] Fix | Delete
###
[418] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Stream.
[419] Fix | Delete
# This method can handle multiple YAML documents contained in +yaml+.
[420] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[421] Fix | Delete
# raised.
[422] Fix | Delete
#
[423] Fix | Delete
# If a block is given, a Psych::Nodes::Document node will be yielded to the
[424] Fix | Delete
# block as it's being parsed.
[425] Fix | Delete
#
[426] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[427] Fix | Delete
#
[428] Fix | Delete
# Example:
[429] Fix | Delete
#
[430] Fix | Delete
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
[431] Fix | Delete
#
[432] Fix | Delete
# Psych.parse_stream("--- a\n--- b") do |node|
[433] Fix | Delete
# node # => #<Psych::Nodes::Document:0x00>
[434] Fix | Delete
# end
[435] Fix | Delete
#
[436] Fix | Delete
# begin
[437] Fix | Delete
# Psych.parse_stream("--- `", filename: "file.txt")
[438] Fix | Delete
# rescue Psych::SyntaxError => ex
[439] Fix | Delete
# ex.file # => 'file.txt'
[440] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[441] Fix | Delete
# end
[442] Fix | Delete
#
[443] Fix | Delete
# Raises a TypeError when NilClass is passed.
[444] Fix | Delete
#
[445] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[446] Fix | Delete
def self.parse_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, &block
[447] Fix | Delete
if legacy_filename != NOT_GIVEN
[448] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse_stream is deprecated. Use keyword argument like Psych.parse_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[449] Fix | Delete
filename = legacy_filename
[450] Fix | Delete
end
[451] Fix | Delete
[452] Fix | Delete
if block_given?
[453] Fix | Delete
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
[454] Fix | Delete
parser.parse yaml, filename
[455] Fix | Delete
else
[456] Fix | Delete
parser = self.parser
[457] Fix | Delete
parser.parse yaml, filename
[458] Fix | Delete
parser.handler.root
[459] Fix | Delete
end
[460] Fix | Delete
end
[461] Fix | Delete
[462] Fix | Delete
###
[463] Fix | Delete
# call-seq:
[464] Fix | Delete
# Psych.dump(o) -> string of yaml
[465] Fix | Delete
# Psych.dump(o, options) -> string of yaml
[466] Fix | Delete
# Psych.dump(o, io) -> io object passed in
[467] Fix | Delete
# Psych.dump(o, io, options) -> io object passed in
[468] Fix | Delete
#
[469] Fix | Delete
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
[470] Fix | Delete
# to control the output format. If an IO object is passed in, the YAML will
[471] Fix | Delete
# be dumped to that IO object.
[472] Fix | Delete
#
[473] Fix | Delete
# Currently supported options are:
[474] Fix | Delete
#
[475] Fix | Delete
# [<tt>:indentation</tt>] Number of space characters used to indent.
[476] Fix | Delete
# Acceptable value should be in <tt>0..9</tt> range,
[477] Fix | Delete
# otherwise option is ignored.
[478] Fix | Delete
#
[479] Fix | Delete
# Default: <tt>2</tt>.
[480] Fix | Delete
# [<tt>:line_width</tt>] Max character to wrap line at.
[481] Fix | Delete
#
[482] Fix | Delete
# Default: <tt>0</tt> (meaning "wrap at 81").
[483] Fix | Delete
# [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
[484] Fix | Delete
# strictly formal).
[485] Fix | Delete
#
[486] Fix | Delete
# Default: <tt>false</tt>.
[487] Fix | Delete
# [<tt>:header</tt>] Write <tt>%YAML [version]</tt> at the beginning of document.
[488] Fix | Delete
#
[489] Fix | Delete
# Default: <tt>false</tt>.
[490] Fix | Delete
#
[491] Fix | Delete
# Example:
[492] Fix | Delete
#
[493] Fix | Delete
# # Dump an array, get back a YAML string
[494] Fix | Delete
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
[495] Fix | Delete
#
[496] Fix | Delete
# # Dump an array to an IO object
[497] Fix | Delete
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
[498] Fix | Delete
#
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function