yip 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES ADDED
@@ -0,0 +1,13 @@
1
+ = Version 0.8
2
+
3
+ Initial release
4
+
5
+ = Version 0.8.1
6
+
7
+ Bugfix release.
8
+
9
+ = Version 0.8.2
10
+
11
+ Repackaging, extra documentation & examples
12
+
13
+ Renamed YAML::load_and_interpolate to YAML::interpolate
data/README ADDED
@@ -0,0 +1,58 @@
1
+ INSTALLATION
2
+
3
+ sudo ruby install.rb
4
+
5
+ LICENSE
6
+
7
+ (c) 2003-2004 Bruce Williams <wbruce@gmail.com>
8
+
9
+ Free software; modify and redistribute it under
10
+ the same terms as Ruby itself
11
+
12
+ ABOUT
13
+
14
+ yip is a library which adds interpolation to whytheluckystiff's
15
+ (http://whytheluckystiff.net) YAML implementation, Syck (included with Ruby 1.8).
16
+
17
+ This library is designed primarily for use in configuration
18
+ files, but may be useful elsewhere.
19
+
20
+ HOW TO USE IT
21
+
22
+ yip uses YPATHs to find nodes referenced for interpolation. See
23
+ http://yaml.freepan.org/index.cgi?YpathBrainstorm for more
24
+ information on YPATH.
25
+
26
+ Here's a small example:
27
+
28
+ ---
29
+ author: Bruce Williams
30
+ message: %author% wrote this.
31
+
32
+ => { 'author' => 'Bruce Williams', 'message' => 'Bruce Williams
33
+ wrote this.' }
34
+
35
+ Simple interpolation is done using %YPATH% notation, as is done
36
+ above. You can also use a sprintf variant:
37
+
38
+ ---
39
+ author: Bruce Williams
40
+ version: 0.8
41
+ message: >
42
+ %author% says, "Here's the version with two extra zeros %(version)1.3f"
43
+
44
+ The sprintf notation is the standard notation %1.3f, but the
45
+ YPATH is included in parenthesis after the '%'. This may look
46
+ eerily familiar to people with Python experience.
47
+
48
+ Note: In the event a YPATH resolves to a complex datatype, the data is
49
+ interpolated as YAML (without the document separator).
50
+
51
+ Take a look at the examples/ subdirectory for additional examples.
52
+
53
+ TODO
54
+
55
+ YAML::INTERPOLATION_PATTERN
56
+ I'd like to spruce up the sprintf-matching part of the Regexp
57
+ before releasing v1.0; at the moment it's a hack.
58
+
@@ -0,0 +1,6 @@
1
+ ---
2
+ # An example configuration file using yip
3
+ arch: i686
4
+ optimize: 3
5
+ cflags: &cf -O%optimize% -mcpu=%arch% -funroll-loops -pipe
6
+ cxxflags: *cf
@@ -0,0 +1,15 @@
1
+ ---
2
+ project: yip; YAML/YPATH Interpolation
3
+ author:
4
+ name: Bruce Williams
5
+ email:
6
+ - wbruce@gmail.com
7
+ - bruce@codedbliss.com
8
+ version: 0.8.2
9
+ description: |
10
+ %project% v%version% by %author/name%
11
+ Feel free to contact the author at one of the following
12
+ e-mail addresses for support:
13
+ %(author/email/0)20s primary
14
+ %(author/email/1)20s secondary
15
+
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+
5
+ # yip; YAML + YPATH Interpolation
6
+ #
7
+ # (c) 2003-2004 Bruce Williams <wbruce@gmail.com>
8
+ # Free software; modify and redistribute it under
9
+ # the same terms as Ruby itself
10
+ #
11
+
12
+ module YAML
13
+
14
+ INTERPOLATION_PATTERN = /(%\((.+?)\)([^[:alpha:]]*[[:alpha:]]+)\b|%(.+?)%)/
15
+
16
+ # For circular YPATH references
17
+ class RecursiveInterpolationError < Exception; end
18
+
19
+ # For the attempted interpolation of nodes that don't exist
20
+ class MissingInterpolationError < Exception; end
21
+
22
+ # Interpolation method
23
+ # * Works similar to YAML::load
24
+ #
25
+ # Interpolation of values occurs within all YAML strings.
26
+ # Interpolation is done using one of two formats:
27
+ #
28
+ # %<YPATH ADDRESS>%
29
+ # %(<YPATH ADDRESS>)<Kernel::printf FORMAT STRING>
30
+ #
31
+ # Examples:
32
+ #
33
+ # %general/server description%
34
+ # %(general/server description)10s
35
+ #
36
+ # For more examples, see examples/
37
+ #
38
+ # Note: YPATH addresses that resolve to collections
39
+ # interpolate as YAML (sans document separator)
40
+ #
41
+ # Resources: http://yaml.freepan.org/index.cgi?YpathBrainstorm
42
+ def YAML::interpolate( data )
43
+ # get the node tree
44
+ nodes = YAML::parse( data )
45
+ # give the DefaultLoader access to the node tree
46
+ YAML::Syck::DefaultLoader.instance_eval{ @nodes = nodes }
47
+ # do the deed
48
+ nodes.transform
49
+ rescue SystemStackError
50
+ raise YAML::RecursiveInterpolationError::new( "Circular YPATH references found; could not interpolate" )
51
+ end
52
+
53
+
54
+ module Syck
55
+
56
+ class Loader
57
+
58
+ alias_method :old_transfer, :transfer
59
+
60
+ # A redefinition of Syck::Loader#transfer to allow
61
+ # for interpolation of nodes
62
+ def transfer( yaml_type, value )
63
+ new_value = value.dup
64
+ # If a node tree has been given (second pass), and it's a string, interpolate
65
+ if @nodes and yaml_type =~ /str$/
66
+ value.scan(YAML::INTERPOLATION_PATTERN) do |field,sprintf_ypath,sprintf_format,plain_ypath|
67
+ ypath = sprintf_ypath || plain_ypath
68
+ format = "%#{sprintf_format || 's'}"
69
+ results = @nodes.select!( ypath )
70
+ replace = /#{Regexp::escape field}/
71
+ case results[0]
72
+ when nil
73
+ raise YAML::MissingInterpolationError::new( "Could not find any nodes matching YPATH '#{ypath}'" )
74
+ when String
75
+ new_value.gsub!( replace, format % results[0].to_s )
76
+ when Numeric
77
+ new_value.gsub!( replace, format % results[0].to_s )
78
+ else
79
+ new_value.gsub!( replace, format % results[0].to_yaml.sub(/^---\s*/, "\n") )
80
+ end
81
+ end
82
+ end
83
+ old_transfer( yaml_type, new_value )
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ if $0 == __FILE__ and File::exists?( ARGV[0].to_s )
93
+ data = File::read ARGV[0]
94
+ puts YAML::interpolate( data ).to_yaml
95
+ end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'yaml'
3
+
4
+ $: << File::join(File::dirname(__FILE__),'../lib')
5
+ require 'yip'
6
+
7
+ class TestYIP < Test::Unit::TestCase
8
+
9
+ YML_SERVER = YAML::interpolate(<<EOY)
10
+ ---
11
+ server:
12
+ port: 2020
13
+ host: localhost
14
+ motd: the host is %server/host%
15
+ full_motd: the server is %server%
16
+ port5: %(server/port)05d
17
+ bad_port5: %(server/port)05d%
18
+ EOY
19
+
20
+ def test_plain
21
+ assert_equal(
22
+ YAML::interpolate("---\na: ok\nb: %a%"),
23
+ {'a'=>'ok','b'=>'ok'} )
24
+ assert_equal( YML_SERVER['motd'], "the host is localhost" )
25
+ end
26
+
27
+ def test_sprintf
28
+ assert_equal( YML_SERVER['port5'], '02020',
29
+ "sprintf-style behavior (w/ digit interpolation) failed")
30
+ assert_not_equal( YML_SERVER['bad_port5'], '02020',
31
+ "sprintf-style w/ trailing '%' behavior failed" )
32
+ end
33
+
34
+ def test_sequence
35
+ assert_equal(YML_SERVER['full_motd'], "the server is \nport: 2020\nhost: localhost",
36
+ "Interpolation of sequences behavior failed")
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.1
3
+ specification_version: 1
4
+ name: yip
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.8.2
7
+ date: 2004-10-02
8
+ summary: Adds interpolation to YAML; primarily for use in configuration files
9
+ require_paths:
10
+ - lib
11
+ author: Bruce Williams
12
+ email: wbruce@gmail.com
13
+ homepage:
14
+ rubyforge_project:
15
+ description:
16
+ autorequire: yip
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.8.0
26
+ version:
27
+ platform: ruby
28
+ files:
29
+ - lib/yip.rb
30
+ - examples/example1.yml
31
+ - examples/example2.yml
32
+ - examples/examples
33
+ - tests/test_yip.rb
34
+ - README
35
+ - CHANGES
36
+ test_files:
37
+ - tests/test_yip.rb
38
+ rdoc_options: []
39
+ extra_rdoc_files:
40
+ - README
41
+ - CHANGES
42
+ executables: []
43
+ extensions: []
44
+ requirements: []
45
+ dependencies: []