ghazel-aws-s3 0.6.4 → 0.6.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ #
2
+ # Copyright (c) 2006 Michael Koziarski
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the "Software"), to deal in the
6
+ # Software without restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8
+ # Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18
+ # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+
21
+ require 'rubygems'
22
+ require 'xml/libxml'
23
+
24
+ class FasterXmlSimple
25
+ Version = '0.5.0'
26
+ class << self
27
+ # Take an string containing XML, and returns a hash representing that
28
+ # XML document. For example:
29
+ #
30
+ # FasterXmlSimple.xml_in("<root><something>1</something></root>")
31
+ # {"root"=>{"something"=>{"__content__"=>"1"}}}
32
+ #
33
+ # Faster XML Simple is designed to be a drop in replacement for the xml_in
34
+ # functionality of http://xml-simple.rubyforge.org
35
+ #
36
+ # The following options are supported:
37
+ #
38
+ # * <tt>contentkey</tt>: The key to use for the content of text elements,
39
+ # defaults to '\_\_content__'
40
+ # * <tt>forcearray</tt>: The list of elements which should always be returned
41
+ # as arrays. Under normal circumstances single element arrays are inlined.
42
+ # * <tt>suppressempty</tt>: The value to return for empty elements, pass +true+
43
+ # to remove empty elements entirely.
44
+ # * <tt>keeproot</tt>: By default the hash returned has a single key with the
45
+ # name of the root element. If the name of the root element isn't
46
+ # interesting to you, pass +false+.
47
+ # * <tt>forcecontent</tt>: By default a text element with no attributes, will
48
+ # be collapsed to just a string instead of a hash with a single key.
49
+ # Pass +true+ to prevent this.
50
+ #
51
+ #
52
+ def xml_in(string, options={})
53
+ new(string, options).out
54
+ end
55
+ end
56
+
57
+ def initialize(string, options) #:nodoc:
58
+ @doc = parse(string)
59
+ @options = default_options.merge options
60
+ end
61
+
62
+ def out #:nodoc:
63
+ if @options['keeproot']
64
+ {@doc.root.name => collapse(@doc.root)}
65
+ else
66
+ collapse(@doc.root)
67
+ end
68
+ end
69
+
70
+ private
71
+ def default_options
72
+ {'contentkey' => '__content__', 'forcearray' => [], 'keeproot'=>true}
73
+ end
74
+
75
+ def collapse(element)
76
+ result = hash_of_attributes(element)
77
+ if text_node? element
78
+ text = collapse_text(element)
79
+ result[content_key] = text if text =~ /\S/
80
+ elsif element.children?
81
+ element.inject(result) do |hash, child|
82
+ unless child.text?
83
+ child_result = collapse(child)
84
+ (hash[child.name] ||= []) << child_result
85
+ end
86
+ hash
87
+ end
88
+ end
89
+ if result.empty?
90
+ return empty_element
91
+ end
92
+ # Compact them to ensure it complies with the user's requests
93
+ inline_single_element_arrays(result)
94
+ remove_empty_elements(result) if suppress_empty?
95
+ if content_only?(result) && !force_content?
96
+ result[content_key]
97
+ else
98
+ result
99
+ end
100
+ end
101
+
102
+ def content_only?(result)
103
+ result.keys == [content_key]
104
+ end
105
+
106
+ def content_key
107
+ @options['contentkey']
108
+ end
109
+
110
+ def force_array?(key_name)
111
+ Array(@options['forcearray']).include?(key_name)
112
+ end
113
+
114
+ def inline_single_element_arrays(result)
115
+ result.each do |key, value|
116
+ if value.size == 1 && value.is_a?(Array) && !force_array?(key)
117
+ result[key] = value.first
118
+ end
119
+ end
120
+ end
121
+
122
+ def remove_empty_elements(result)
123
+ result.each do |key, value|
124
+ if value == empty_element
125
+ result.delete key
126
+ end
127
+ end
128
+ end
129
+
130
+ def suppress_empty?
131
+ @options['suppressempty'] == true
132
+ end
133
+
134
+ def empty_element
135
+ if !@options.has_key? 'suppressempty'
136
+ {}
137
+ else
138
+ @options['suppressempty']
139
+ end
140
+ end
141
+
142
+ # removes the content if it's nothing but blanks, prevents
143
+ # the hash being polluted with lots of content like "\n\t\t\t"
144
+ def suppress_empty_content(result)
145
+ result.delete content_key if result[content_key] !~ /\S/
146
+ end
147
+
148
+ def force_content?
149
+ @options['forcecontent']
150
+ end
151
+
152
+ # a text node is one with 1 or more child nodes which are
153
+ # text nodes, and no non-text children, there's no sensible
154
+ # way to support nodes which are text and markup like:
155
+ # <p>Something <b>Bold</b> </p>
156
+ def text_node?(element)
157
+ !element.text? && element.all? {|c| c.text?}
158
+ end
159
+
160
+ # takes a text node, and collapses it into a string
161
+ def collapse_text(element)
162
+ element.map {|c| c.content } * ''
163
+ end
164
+
165
+ def hash_of_attributes(element)
166
+ result = {}
167
+ element.each_attr do |attribute|
168
+ name = attribute.name
169
+ name = [attribute.ns, attribute.name].join(':') if attribute.ns?
170
+ result[name] = attribute.value
171
+ end
172
+ result
173
+ end
174
+
175
+ def parse(string)
176
+ if string == ''
177
+ string = ' '
178
+ end
179
+ XML::Parser.string(string).parse
180
+ end
181
+ end
182
+
183
+ # Don't overwrite XmlSimple.xml_in since FasterXmlSimple's implementation isn't
184
+ # 100% compatible.
185
+ #
186
+ # class XmlSimple # :nodoc:
187
+ # def self.xml_in(*args)
188
+ # FasterXmlSimple.xml_in *args
189
+ # end
190
+ # end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RegressionTest < FasterXSTest
4
+ def test_content_nil_regressions
5
+ expected = {"asdf"=>{"jklsemicolon"=>{}}}
6
+ assert_equal expected, FasterXmlSimple.xml_in("<asdf><jklsemicolon /></asdf>")
7
+ assert_equal expected, FasterXmlSimple.xml_in("<asdf><jklsemicolon /></asdf>", 'forcearray'=>['asdf'])
8
+ end
9
+
10
+ def test_s3_regression
11
+ str = File.read("test/fixtures/test-7.xml")
12
+ assert_nil FasterXmlSimple.xml_in(str)["AccessControlPolicy"]["AccessControlList"]["__content__"]
13
+ end
14
+
15
+ def test_xml_simple_transparency
16
+ assert_equal XmlSimple.xml_in("<asdf />"), FasterXmlSimple.xml_in("<asdf />")
17
+ end
18
+
19
+ def test_suppress_empty_variations
20
+ str = "<asdf><fdsa /></asdf>"
21
+
22
+ assert_equal Hash.new, FasterXmlSimple.xml_in(str)["asdf"]["fdsa"]
23
+ assert_nil FasterXmlSimple.xml_in(str, 'suppressempty'=>nil)["asdf"]["fdsa"]
24
+ assert_equal '', FasterXmlSimple.xml_in(str, 'suppressempty'=>'')["asdf"]["fdsa"]
25
+ assert !FasterXmlSimple.xml_in(str, 'suppressempty'=>true)["asdf"].has_key?("fdsa")
26
+ end
27
+
28
+ def test_empty_string_doesnt_crash
29
+ assert_raise(XML::Parser::ParseError) do
30
+ silence_stderr do
31
+ FasterXmlSimple.xml_in('')
32
+ end
33
+ end
34
+ end
35
+
36
+ def test_keeproot_false
37
+ str = "<asdf><fdsa>1</fdsa></asdf>"
38
+ expected = {"fdsa"=>"1"}
39
+ assert_equal expected, FasterXmlSimple.xml_in(str, 'keeproot'=>false)
40
+ end
41
+
42
+ def test_keeproot_false_with_force_content
43
+ str = "<asdf><fdsa>1</fdsa></asdf>"
44
+ expected = {"fdsa"=>{"__content__"=>"1"}}
45
+ assert_equal expected, FasterXmlSimple.xml_in(str, 'keeproot'=>false, 'forcecontent'=>true)
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+
2
+ require 'test/unit'
3
+ require 'faster_xml_simple'
4
+
5
+ class FasterXSTest < Test::Unit::TestCase
6
+ def default_test
7
+ end
8
+
9
+ def silence_stderr
10
+ str = STDERR.dup
11
+ STDERR.reopen("/dev/null")
12
+ STDERR.sync=true
13
+ yield
14
+ ensure
15
+ STDERR.reopen(str)
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'yaml'
3
+
4
+ class XmlSimpleComparisonTest < FasterXSTest
5
+
6
+ # Define test methods
7
+
8
+ Dir["test/fixtures/test-*.xml"].each do |file_name|
9
+ xml_file_name = file_name
10
+ method_name = File.basename(file_name, ".xml").gsub('-', '_')
11
+ yml_file_name = file_name.gsub('xml', 'yml')
12
+ rails_yml_file_name = file_name.gsub('xml', 'rails.yml')
13
+ class_eval <<-EOV, __FILE__, __LINE__
14
+ def #{method_name}
15
+ assert_equal YAML.load(File.read('#{yml_file_name}')),
16
+ FasterXmlSimple.xml_in(File.read('#{xml_file_name}'), default_options )
17
+ end
18
+
19
+ def #{method_name}_rails
20
+ assert_equal YAML.load(File.read('#{rails_yml_file_name}')),
21
+ FasterXmlSimple.xml_in(File.read('#{xml_file_name}'), rails_options)
22
+ end
23
+ EOV
24
+ end
25
+
26
+ def default_options
27
+ {
28
+ 'keeproot' => true,
29
+ 'contentkey' => '__content__',
30
+ 'forcecontent' => true,
31
+ 'suppressempty' => nil,
32
+ 'forcearray' => ['something-else']
33
+ }
34
+ end
35
+
36
+ def rails_options
37
+ {
38
+ 'forcearray' => false,
39
+ 'forcecontent' => true,
40
+ 'keeproot' => true,
41
+ 'contentkey' => '__content__'
42
+ }
43
+ end
44
+
45
+
46
+ end
@@ -0,0 +1,211 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rdoc/rdoc'
4
+
5
+ module RDoc
6
+ class CodeInfo
7
+ class << self
8
+ def parse(wildcard_pattern = nil)
9
+ @info_for_corpus = parse_files(wildcard_pattern)
10
+ end
11
+
12
+ def for(constant)
13
+ new(constant).info
14
+ end
15
+
16
+ def info_for_corpus
17
+ raise RuntimeError, "You must first generate a corpus to search by using RDoc::CodeInfo.parse" unless @info_for_corpus
18
+ @info_for_corpus
19
+ end
20
+
21
+ def parsed_files
22
+ info_for_corpus.map {|info| info.file_absolute_name}
23
+ end
24
+
25
+ def files_to_parse
26
+ @files_to_parse ||= Rake::FileList.new
27
+ end
28
+
29
+ private
30
+ def parse_files(pattern)
31
+ files = pattern ? Rake::FileList[pattern] : files_to_parse
32
+ options = Options.instance
33
+ options.parse(files << '-q', RDoc::GENERATORS)
34
+ rdoc.send(:parse_files, options)
35
+ end
36
+
37
+ def rdoc
38
+ TopLevel.reset
39
+ rdoc = RDoc.new
40
+ stats = Stats.new
41
+ # We don't want any output so we'll override the print method
42
+ stats.instance_eval { def print; nil end }
43
+ rdoc.instance_variable_set(:@stats, stats)
44
+ rdoc
45
+ end
46
+ end
47
+
48
+ attr_reader :info
49
+ def initialize(location)
50
+ @location = CodeLocation.new(location)
51
+ find_constant
52
+ find_method if @location.has_method?
53
+ end
54
+
55
+ private
56
+ attr_reader :location
57
+ attr_writer :info
58
+ def find_constant
59
+ parts = location.namespace_parts
60
+ self.class.info_for_corpus.each do |file_info|
61
+ @info = parts.inject(file_info) do |result, const_part|
62
+ (result.find_module_named(const_part) || result.find_class_named(const_part)) || break
63
+ end
64
+ return if info
65
+ end
66
+ end
67
+
68
+ def find_method
69
+ return unless info
70
+ self.info = info.method_list.detect do |method_info|
71
+ next unless method_info.name == location.method_name
72
+ if location.class_method?
73
+ method_info.singleton
74
+ elsif location.instance_method?
75
+ !method_info.singleton
76
+ else
77
+ true
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ class CodeLocation
84
+ attr_reader :location
85
+
86
+ def initialize(location)
87
+ @location = location
88
+ end
89
+
90
+ def parts
91
+ location.split(/::|\.|#/)
92
+ end
93
+
94
+ def namespace_parts
95
+ has_method? ? parts[0...-1] : parts
96
+ end
97
+
98
+ def has_method?
99
+ ('a'..'z').include?(parts.last[0, 1])
100
+ end
101
+
102
+ def instance_method?
103
+ !location['#'].nil?
104
+ end
105
+
106
+ def class_method?
107
+ has_method? && !location[/#|\./]
108
+ end
109
+
110
+ def method_name
111
+ parts.last if has_method?
112
+ end
113
+ end
114
+ end
115
+
116
+ if __FILE__ == $0
117
+ require 'test/unit'
118
+ class CodeInfoTest < Test::Unit::TestCase
119
+ def setup
120
+ RDoc::CodeInfo.parse(__FILE__)
121
+ end
122
+
123
+ def test_constant_lookup
124
+ assert RDoc::CodeInfo.for('RDoc')
125
+
126
+ info = RDoc::CodeInfo.for('RDoc::CodeInfo')
127
+ assert_equal 'CodeInfo', info.name
128
+ end
129
+
130
+ def test_method_lookup
131
+ {'RDoc::CodeInfo.parse' => true,
132
+ 'RDoc::CodeInfo::parse' => true,
133
+ 'RDoc::CodeInfo#parse' => false,
134
+ 'RDoc::CodeInfo.find_method' => true,
135
+ 'RDoc::CodeInfo::find_method' => false,
136
+ 'RDoc::CodeInfo#find_method' => true,
137
+ 'RDoc::CodeInfo#no_such_method' => false,
138
+ 'RDoc::NoSuchConst#foo' => false}.each do |location, result_of_lookup|
139
+ assert_equal result_of_lookup, !RDoc::CodeInfo.for(location).nil?
140
+ end
141
+ end
142
+ end
143
+
144
+ class CodeLocationTest < Test::Unit::TestCase
145
+ def test_parts
146
+ {'Foo' => %w(Foo),
147
+ 'Foo::Bar' => %w(Foo Bar),
148
+ 'Foo::Bar#baz' => %w(Foo Bar baz),
149
+ 'Foo::Bar.baz' => %w(Foo Bar baz),
150
+ 'Foo::Bar::baz' => %w(Foo Bar baz),
151
+ 'Foo::Bar::Baz' => %w(Foo Bar Baz)}.each do |location, parts|
152
+ assert_equal parts, RDoc::CodeLocation.new(location).parts
153
+ end
154
+ end
155
+
156
+ def test_namespace_parts
157
+ {'Foo' => %w(Foo),
158
+ 'Foo::Bar' => %w(Foo Bar),
159
+ 'Foo::Bar#baz' => %w(Foo Bar),
160
+ 'Foo::Bar.baz' => %w(Foo Bar),
161
+ 'Foo::Bar::baz' => %w(Foo Bar),
162
+ 'Foo::Bar::Baz' => %w(Foo Bar Baz)}.each do |location, namespace_parts|
163
+ assert_equal namespace_parts, RDoc::CodeLocation.new(location).namespace_parts
164
+ end
165
+ end
166
+
167
+ def test_has_method?
168
+ {'Foo' => false,
169
+ 'Foo::Bar' => false,
170
+ 'Foo::Bar#baz' => true,
171
+ 'Foo::Bar.baz' => true,
172
+ 'Foo::Bar::baz' => true,
173
+ 'Foo::Bar::Baz' => false}.each do |location, has_method_result|
174
+ assert_equal has_method_result, RDoc::CodeLocation.new(location).has_method?
175
+ end
176
+ end
177
+
178
+ def test_instance_method?
179
+ {'Foo' => false,
180
+ 'Foo::Bar' => false,
181
+ 'Foo::Bar#baz' => true,
182
+ 'Foo::Bar.baz' => false,
183
+ 'Foo::Bar::baz' => false,
184
+ 'Foo::Bar::Baz' => false}.each do |location, is_instance_method|
185
+ assert_equal is_instance_method, RDoc::CodeLocation.new(location).instance_method?
186
+ end
187
+ end
188
+
189
+ def test_class_method?
190
+ {'Foo' => false,
191
+ 'Foo::Bar' => false,
192
+ 'Foo::Bar#baz' => false,
193
+ 'Foo::Bar.baz' => false,
194
+ 'Foo::Bar::baz' => true,
195
+ 'Foo::Bar::Baz' => false}.each do |location, is_class_method|
196
+ assert_equal is_class_method, RDoc::CodeLocation.new(location).class_method?
197
+ end
198
+ end
199
+
200
+ def test_method_name
201
+ {'Foo' => nil,
202
+ 'Foo::Bar' => nil,
203
+ 'Foo::Bar#baz' => 'baz',
204
+ 'Foo::Bar.baz' => 'baz',
205
+ 'Foo::Bar::baz' => 'baz',
206
+ 'Foo::Bar::Baz' => nil}.each do |location, method_name|
207
+ assert_equal method_name, RDoc::CodeLocation.new(location).method_name
208
+ end
209
+ end
210
+ end
211
+ end