puppet 0.25.0 → 0.25.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puppet might be problematic. Click here for more details.

Files changed (138) hide show
  1. data/CHANGELOG +4772 -19114
  2. data/README +1 -1
  3. data/Rakefile +7 -25
  4. data/{sbin → bin}/puppetca +0 -0
  5. data/{sbin → bin}/puppetd +0 -0
  6. data/{sbin → bin}/puppetmasterd +0 -0
  7. data/{sbin → bin}/puppetqd +0 -0
  8. data/{sbin → bin}/puppetrun +0 -0
  9. data/conf/osx/createpackage.sh +1 -1
  10. data/conf/redhat/client.init +6 -3
  11. data/conf/redhat/puppet.spec +26 -14
  12. data/conf/redhat/server.init +3 -2
  13. data/ext/vim/syntax/puppet.vim +4 -1
  14. data/install.rb +25 -7
  15. data/lib/puppet.rb +1 -1
  16. data/lib/puppet/agent.rb +2 -2
  17. data/lib/puppet/application/puppet.rb +3 -3
  18. data/lib/puppet/application/puppetd.rb +0 -7
  19. data/lib/puppet/application/puppetdoc.rb +1 -0
  20. data/lib/puppet/application/puppetmasterd.rb +2 -2
  21. data/lib/puppet/configurer.rb +6 -1
  22. data/lib/puppet/configurer/fact_handler.rb +2 -2
  23. data/lib/puppet/defaults.rb +2 -2
  24. data/lib/puppet/external/nagios/base.rb +4 -3
  25. data/lib/puppet/external/pson/common.rb +367 -0
  26. data/lib/puppet/external/pson/pure.rb +77 -0
  27. data/lib/puppet/external/pson/pure/generator.rb +429 -0
  28. data/lib/puppet/external/pson/pure/parser.rb +269 -0
  29. data/lib/puppet/external/pson/version.rb +8 -0
  30. data/lib/puppet/feature/pson.rb +6 -0
  31. data/lib/puppet/feature/rails.rb +1 -5
  32. data/lib/puppet/file_serving/configuration.rb +2 -2
  33. data/lib/puppet/indirector/certificate/rest.rb +6 -0
  34. data/lib/puppet/indirector/facts/facter.rb +1 -1
  35. data/lib/puppet/indirector/ldap.rb +1 -1
  36. data/lib/puppet/indirector/queue.rb +3 -3
  37. data/lib/puppet/indirector/rest.rb +1 -1
  38. data/lib/puppet/network/authstore.rb +34 -53
  39. data/lib/puppet/network/formats.rb +59 -9
  40. data/lib/puppet/network/http/mongrel/rest.rb +10 -9
  41. data/lib/puppet/network/http/webrick.rb +8 -1
  42. data/lib/puppet/network/http/webrick/rest.rb +2 -5
  43. data/lib/puppet/network/http_server/webrick.rb +1 -4
  44. data/lib/puppet/parser/ast/leaf.rb +1 -3
  45. data/lib/puppet/parser/collector.rb +14 -8
  46. data/lib/puppet/parser/compiler.rb +7 -0
  47. data/lib/puppet/parser/functions/fqdn_rand.rb +4 -10
  48. data/lib/puppet/parser/functions/regsubst.rb +44 -30
  49. data/lib/puppet/parser/functions/require.rb +18 -3
  50. data/lib/puppet/parser/functions/versioncmp.rb +1 -1
  51. data/lib/puppet/parser/interpreter.rb +1 -1
  52. data/lib/puppet/parser/lexer.rb +29 -31
  53. data/lib/puppet/parser/loaded_code.rb +4 -0
  54. data/lib/puppet/parser/parser_support.rb +5 -2
  55. data/lib/puppet/parser/resource.rb +31 -6
  56. data/lib/puppet/property.rb +3 -2
  57. data/lib/puppet/provider/macauthorization/macauthorization.rb +14 -14
  58. data/lib/puppet/provider/package/dpkg.rb +1 -1
  59. data/lib/puppet/provider/package/portage.rb +15 -5
  60. data/lib/puppet/provider/package/rug.rb +1 -1
  61. data/lib/puppet/provider/package/up2date.rb +1 -1
  62. data/lib/puppet/provider/package/urpmi.rb +1 -1
  63. data/lib/puppet/provider/service/daemontools.rb +7 -10
  64. data/lib/puppet/provider/service/runit.rb +7 -17
  65. data/lib/puppet/provider/ssh_authorized_key/parsed.rb +7 -47
  66. data/lib/puppet/provider/zone/solaris.rb +12 -3
  67. data/lib/puppet/relationship.rb +12 -12
  68. data/lib/puppet/reports/rrdgraph.rb +1 -1
  69. data/lib/puppet/reports/store.rb +2 -2
  70. data/lib/puppet/reports/tagmail.rb +5 -16
  71. data/lib/puppet/resource.rb +15 -20
  72. data/lib/puppet/resource/catalog.rb +40 -29
  73. data/lib/puppet/ssl/certificate_revocation_list.rb +0 -2
  74. data/lib/puppet/ssl/host.rb +2 -3
  75. data/lib/puppet/sslcertificates/ca.rb +0 -5
  76. data/lib/puppet/type/cron.rb +1 -1
  77. data/lib/puppet/type/file/owner.rb +7 -4
  78. data/lib/puppet/type/resources.rb +17 -17
  79. data/lib/puppet/type/yumrepo.rb +10 -3
  80. data/lib/puppet/util.rb +6 -11
  81. data/lib/puppet/util/inifile.rb +8 -0
  82. data/lib/puppet/util/log.rb +2 -2
  83. data/lib/puppet/util/monkey_patches.rb +0 -43
  84. data/lib/puppet/util/{json.rb → pson.rb} +6 -6
  85. data/lib/puppet/util/rdoc.rb +5 -3
  86. data/lib/puppet/util/selinux.rb +12 -6
  87. data/lib/puppet/util/settings.rb +25 -16
  88. data/lib/puppet/util/settings/file_setting.rb +4 -2
  89. data/spec/integration/application/puppet.rb +4 -4
  90. data/spec/integration/defaults.rb +2 -2
  91. data/spec/integration/indirector/catalog/queue.rb +5 -5
  92. data/spec/integration/indirector/certificate/rest.rb +3 -1
  93. data/spec/integration/network/formats.rb +36 -36
  94. data/spec/integration/parser/functions/require.rb +5 -3
  95. data/spec/integration/provider/mailalias/aliases.rb +4 -4
  96. data/spec/integration/resource/catalog.rb +4 -4
  97. data/spec/unit/application/puppet.rb +16 -15
  98. data/spec/unit/application/puppetd.rb +1 -1
  99. data/spec/unit/application/puppetdoc.rb +6 -0
  100. data/spec/unit/application/puppetmasterd.rb +6 -6
  101. data/spec/unit/configurer/fact_handler.rb +3 -3
  102. data/spec/unit/file_serving/configuration.rb +16 -2
  103. data/spec/unit/indirector/certificate/rest.rb +34 -0
  104. data/spec/unit/indirector/queue.rb +15 -15
  105. data/spec/unit/indirector/rest.rb +31 -9
  106. data/spec/unit/network/authstore.rb +105 -26
  107. data/spec/unit/network/formats.rb +124 -39
  108. data/spec/unit/parser/ast/leaf.rb +15 -0
  109. data/spec/unit/parser/collector.rb +20 -9
  110. data/spec/unit/parser/compiler.rb +19 -0
  111. data/spec/unit/parser/functions/fqdn_rand.rb +62 -0
  112. data/spec/unit/parser/functions/regsubst.rb +80 -0
  113. data/spec/unit/parser/functions/require.rb +19 -7
  114. data/spec/unit/parser/interpreter.rb +2 -2
  115. data/spec/unit/parser/lexer.rb +32 -7
  116. data/spec/unit/parser/loaded_code.rb +18 -1
  117. data/spec/unit/parser/parser.rb +10 -2
  118. data/spec/unit/parser/resource.rb +53 -2
  119. data/spec/unit/parser/scope.rb +1 -1
  120. data/spec/unit/property.rb +14 -4
  121. data/spec/unit/provider/package/dpkg.rb +7 -0
  122. data/spec/unit/provider/service/daemontools.rb +19 -2
  123. data/spec/unit/provider/service/redhat.rb +2 -0
  124. data/spec/unit/provider/service/runit.rb +15 -4
  125. data/spec/unit/provider/ssh_authorized_key/parsed.rb +32 -55
  126. data/spec/unit/relationship.rb +21 -46
  127. data/spec/unit/resource.rb +30 -39
  128. data/spec/unit/resource/catalog.rb +66 -51
  129. data/spec/unit/ssl/certificate_revocation_list.rb +0 -12
  130. data/spec/unit/type/cron.rb +33 -0
  131. data/spec/unit/type/file/owner.rb +10 -4
  132. data/spec/unit/util/json.rb +9 -9
  133. data/spec/unit/util/log.rb +36 -0
  134. data/spec/unit/util/settings.rb +6 -0
  135. data/test/data/providers/ssh_authorized_key/parsed/authorized_keys2 +1 -0
  136. data/test/lib/puppettest/support/utils.rb +8 -16
  137. metadata +36 -13
  138. data/lib/puppet/feature/json.rb +0 -2
@@ -0,0 +1,367 @@
1
+ require 'puppet/external/pson/version'
2
+
3
+ module PSON
4
+ class << self
5
+ # If _object_ is string-like parse the string and return the parsed result
6
+ # as a Ruby data structure. Otherwise generate a PSON text from the Ruby
7
+ # data structure object and return it.
8
+ #
9
+ # The _opts_ argument is passed through to generate/parse respectively, see
10
+ # generate and parse for their documentation.
11
+ def [](object, opts = {})
12
+ if object.respond_to? :to_str
13
+ PSON.parse(object.to_str, opts => {})
14
+ else
15
+ PSON.generate(object, opts => {})
16
+ end
17
+ end
18
+
19
+ # Returns the PSON parser class, that is used by PSON. This might be either
20
+ # PSON::Ext::Parser or PSON::Pure::Parser.
21
+ attr_reader :parser
22
+
23
+ # Set the PSON parser class _parser_ to be used by PSON.
24
+ def parser=(parser) # :nodoc:
25
+ @parser = parser
26
+ remove_const :Parser if const_defined? :Parser
27
+ const_set :Parser, parser
28
+ end
29
+
30
+ def registered_document_types
31
+ @registered_document_types ||= {}
32
+ end
33
+
34
+ # Register a class-constant for deserializaion.
35
+ def register_document_type(name,klass)
36
+ registered_document_types[name.to_s] = klass
37
+ end
38
+
39
+ # Return the constant located at _path_.
40
+ # Anything may be registered as a path by calling register_path, above.
41
+ # Otherwise, the format of _path_ has to be either ::A::B::C or A::B::C.
42
+ # In either of these cases A has to be defined in Object (e.g. the path
43
+ # must be an absolute namespace path. If the constant doesn't exist at
44
+ # the given path, an ArgumentError is raised.
45
+ def deep_const_get(path) # :nodoc:
46
+ path = path.to_s
47
+ registered_document_types[path] || path.split(/::/).inject(Object) do |p, c|
48
+ case
49
+ when c.empty? then p
50
+ when p.const_defined?(c) then p.const_get(c)
51
+ else raise ArgumentError, "can't find const #{path}"
52
+ end
53
+ end
54
+ end
55
+
56
+ # Set the module _generator_ to be used by PSON.
57
+ def generator=(generator) # :nodoc:
58
+ @generator = generator
59
+ generator_methods = generator::GeneratorMethods
60
+ for const in generator_methods.constants
61
+ klass = deep_const_get(const)
62
+ modul = generator_methods.const_get(const)
63
+ klass.class_eval do
64
+ instance_methods(false).each do |m|
65
+ m.to_s == 'to_pson' and remove_method m
66
+ end
67
+ include modul
68
+ end
69
+ end
70
+ self.state = generator::State
71
+ const_set :State, self.state
72
+ end
73
+
74
+ # Returns the PSON generator modul, that is used by PSON. This might be
75
+ # either PSON::Ext::Generator or PSON::Pure::Generator.
76
+ attr_reader :generator
77
+
78
+ # Returns the PSON generator state class, that is used by PSON. This might
79
+ # be either PSON::Ext::Generator::State or PSON::Pure::Generator::State.
80
+ attr_accessor :state
81
+
82
+ # This is create identifier, that is used to decide, if the _pson_create_
83
+ # hook of a class should be called. It defaults to 'document_type'.
84
+ attr_accessor :create_id
85
+ end
86
+ self.create_id = 'document_type'
87
+
88
+ NaN = (-1.0) ** 0.5
89
+
90
+ Infinity = 1.0/0
91
+
92
+ MinusInfinity = -Infinity
93
+
94
+ # The base exception for PSON errors.
95
+ class PSONError < StandardError; end
96
+
97
+ # This exception is raised, if a parser error occurs.
98
+ class ParserError < PSONError; end
99
+
100
+ # This exception is raised, if the nesting of parsed datastructures is too
101
+ # deep.
102
+ class NestingError < ParserError; end
103
+
104
+ # This exception is raised, if a generator or unparser error occurs.
105
+ class GeneratorError < PSONError; end
106
+ # For backwards compatibility
107
+ UnparserError = GeneratorError
108
+
109
+ # If a circular data structure is encountered while unparsing
110
+ # this exception is raised.
111
+ class CircularDatastructure < GeneratorError; end
112
+
113
+ # This exception is raised, if the required unicode support is missing on the
114
+ # system. Usually this means, that the iconv library is not installed.
115
+ class MissingUnicodeSupport < PSONError; end
116
+
117
+ module_function
118
+
119
+ # Parse the PSON string _source_ into a Ruby data structure and return it.
120
+ #
121
+ # _opts_ can have the following
122
+ # keys:
123
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
124
+ # structures. Disable depth checking with :max_nesting => false, it defaults
125
+ # to 19.
126
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
127
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
128
+ # to false.
129
+ # * *create_additions*: If set to false, the Parser doesn't create
130
+ # additions even if a matchin class and create_id was found. This option
131
+ # defaults to true.
132
+ def parse(source, opts = {})
133
+ PSON.parser.new(source, opts).parse
134
+ end
135
+
136
+ # Parse the PSON string _source_ into a Ruby data structure and return it.
137
+ # The bang version of the parse method, defaults to the more dangerous values
138
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
139
+ #
140
+ # _opts_ can have the following keys:
141
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
142
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
143
+ # methods defaults to not doing max depth checking: This can be dangerous,
144
+ # if someone wants to fill up your stack.
145
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
146
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
147
+ # to true.
148
+ # * *create_additions*: If set to false, the Parser doesn't create
149
+ # additions even if a matchin class and create_id was found. This option
150
+ # defaults to true.
151
+ def parse!(source, opts = {})
152
+ opts = {
153
+ :max_nesting => false,
154
+ :allow_nan => true
155
+ }.update(opts)
156
+ PSON.parser.new(source, opts).parse
157
+ end
158
+
159
+ # Unparse the Ruby data structure _obj_ into a single line PSON string and
160
+ # return it. _state_ is
161
+ # * a PSON::State object,
162
+ # * or a Hash like object (responding to to_hash),
163
+ # * an object convertible into a hash by a to_h method,
164
+ # that is used as or to configure a State object.
165
+ #
166
+ # It defaults to a state object, that creates the shortest possible PSON text
167
+ # in one line, checks for circular data structures and doesn't allow NaN,
168
+ # Infinity, and -Infinity.
169
+ #
170
+ # A _state_ hash can have the following keys:
171
+ # * *indent*: a string used to indent levels (default: ''),
172
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
173
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
174
+ # * *object_nl*: a string that is put at the end of a PSON object (default: ''),
175
+ # * *array_nl*: a string that is put at the end of a PSON array (default: ''),
176
+ # * *check_circular*: true if checking for circular data structures
177
+ # should be done (the default), false otherwise.
178
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
179
+ # generated, otherwise an exception is thrown, if these values are
180
+ # encountered. This options defaults to false.
181
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
182
+ # structures from which PSON is to be generated. Disable depth checking
183
+ # with :max_nesting => false, it defaults to 19.
184
+ #
185
+ # See also the fast_generate for the fastest creation method with the least
186
+ # amount of sanity checks, and the pretty_generate method for some
187
+ # defaults for a pretty output.
188
+ def generate(obj, state = nil)
189
+ if state
190
+ state = State.from_state(state)
191
+ else
192
+ state = State.new
193
+ end
194
+ obj.to_pson(state)
195
+ end
196
+
197
+ # :stopdoc:
198
+ # I want to deprecate these later, so I'll first be silent about them, and
199
+ # later delete them.
200
+ alias unparse generate
201
+ module_function :unparse
202
+ # :startdoc:
203
+
204
+ # Unparse the Ruby data structure _obj_ into a single line PSON string and
205
+ # return it. This method disables the checks for circles in Ruby objects, and
206
+ # also generates NaN, Infinity, and, -Infinity float values.
207
+ #
208
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
209
+ # _obj_ argument, because this will cause PSON to go into an infinite loop.
210
+ def fast_generate(obj)
211
+ obj.to_pson(nil)
212
+ end
213
+
214
+ # :stopdoc:
215
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
216
+ alias fast_unparse fast_generate
217
+ module_function :fast_unparse
218
+ # :startdoc:
219
+
220
+ # Unparse the Ruby data structure _obj_ into a PSON string and return it. The
221
+ # returned string is a prettier form of the string returned by #unparse.
222
+ #
223
+ # The _opts_ argument can be used to configure the generator, see the
224
+ # generate method for a more detailed explanation.
225
+ def pretty_generate(obj, opts = nil)
226
+ state = PSON.state.new(
227
+ :indent => ' ',
228
+ :space => ' ',
229
+ :object_nl => "\n",
230
+ :array_nl => "\n",
231
+ :check_circular => true
232
+ )
233
+ if opts
234
+ if opts.respond_to? :to_hash
235
+ opts = opts.to_hash
236
+ elsif opts.respond_to? :to_h
237
+ opts = opts.to_h
238
+ else
239
+ raise TypeError, "can't convert #{opts.class} into Hash"
240
+ end
241
+ state.configure(opts)
242
+ end
243
+ obj.to_pson(state)
244
+ end
245
+
246
+ # :stopdoc:
247
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
248
+ alias pretty_unparse pretty_generate
249
+ module_function :pretty_unparse
250
+ # :startdoc:
251
+
252
+ # Load a ruby data structure from a PSON _source_ and return it. A source can
253
+ # either be a string-like object, an IO like object, or an object responding
254
+ # to the read method. If _proc_ was given, it will be called with any nested
255
+ # Ruby object as an argument recursively in depth first order.
256
+ #
257
+ # This method is part of the implementation of the load/dump interface of
258
+ # Marshal and YAML.
259
+ def load(source, proc = nil)
260
+ if source.respond_to? :to_str
261
+ source = source.to_str
262
+ elsif source.respond_to? :to_io
263
+ source = source.to_io.read
264
+ else
265
+ source = source.read
266
+ end
267
+ result = parse(source, :max_nesting => false, :allow_nan => true)
268
+ recurse_proc(result, &proc) if proc
269
+ result
270
+ end
271
+
272
+ def recurse_proc(result, &proc)
273
+ case result
274
+ when Array
275
+ result.each { |x| recurse_proc x, &proc }
276
+ proc.call result
277
+ when Hash
278
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
279
+ proc.call result
280
+ else
281
+ proc.call result
282
+ end
283
+ end
284
+ private :recurse_proc
285
+ module_function :recurse_proc
286
+
287
+ alias restore load
288
+ module_function :restore
289
+
290
+ # Dumps _obj_ as a PSON string, i.e. calls generate on the object and returns
291
+ # the result.
292
+ #
293
+ # If anIO (an IO like object or an object that responds to the write method)
294
+ # was given, the resulting PSON is written to it.
295
+ #
296
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
297
+ # exception is raised. This argument is similar (but not exactly the
298
+ # same!) to the _limit_ argument in Marshal.dump.
299
+ #
300
+ # This method is part of the implementation of the load/dump interface of
301
+ # Marshal and YAML.
302
+ def dump(obj, anIO = nil, limit = nil)
303
+ if anIO and limit.nil?
304
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
305
+ unless anIO.respond_to?(:write)
306
+ limit = anIO
307
+ anIO = nil
308
+ end
309
+ end
310
+ limit ||= 0
311
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
312
+ if anIO
313
+ anIO.write result
314
+ anIO
315
+ else
316
+ result
317
+ end
318
+ rescue PSON::NestingError
319
+ raise ArgumentError, "exceed depth limit"
320
+ end
321
+ end
322
+
323
+ module ::Kernel
324
+ private
325
+
326
+ # Outputs _objs_ to STDOUT as PSON strings in the shortest form, that is in
327
+ # one line.
328
+ def j(*objs)
329
+ objs.each do |obj|
330
+ puts PSON::generate(obj, :allow_nan => true, :max_nesting => false)
331
+ end
332
+ nil
333
+ end
334
+
335
+ # Ouputs _objs_ to STDOUT as PSON strings in a pretty format, with
336
+ # indentation and over many lines.
337
+ def jj(*objs)
338
+ objs.each do |obj|
339
+ puts PSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
340
+ end
341
+ nil
342
+ end
343
+
344
+ # If _object_ is string-like parse the string and return the parsed result as
345
+ # a Ruby data structure. Otherwise generate a PSON text from the Ruby data
346
+ # structure object and return it.
347
+ #
348
+ # The _opts_ argument is passed through to generate/parse respectively, see
349
+ # generate and parse for their documentation.
350
+ def PSON(object, opts = {})
351
+ if object.respond_to? :to_str
352
+ PSON.parse(object.to_str, opts)
353
+ else
354
+ PSON.generate(object, opts)
355
+ end
356
+ end
357
+ end
358
+
359
+ class ::Class
360
+ # Returns true, if this class can be used to create an instance
361
+ # from a serialised PSON string. The class has to implement a class
362
+ # method _pson_create_ that expects a hash as first parameter, which includes
363
+ # the required data.
364
+ def pson_creatable?
365
+ respond_to?(:pson_create)
366
+ end
367
+ end
@@ -0,0 +1,77 @@
1
+ require 'puppet/external/pson/common'
2
+ require 'puppet/external/pson/pure/parser'
3
+ require 'puppet/external/pson/pure/generator'
4
+
5
+ module PSON
6
+ begin
7
+ require 'iconv'
8
+ # An iconv instance to convert from UTF8 to UTF16 Big Endian.
9
+ UTF16toUTF8 = Iconv.new('utf-8', 'utf-16be') # :nodoc:
10
+ # An iconv instance to convert from UTF16 Big Endian to UTF8.
11
+ UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8') # :nodoc:
12
+ UTF8toUTF16.iconv('no bom')
13
+ rescue LoadError
14
+ raise MissingUnicodeSupport,
15
+ "iconv couldn't be loaded, which is required for UTF-8/UTF-16 conversions"
16
+ rescue Errno::EINVAL, Iconv::InvalidEncoding
17
+ # Iconv doesn't support big endian utf-16. Let's try to hack this manually
18
+ # into the converters.
19
+ begin
20
+ old_verbose, $VERBSOSE = $VERBOSE, nil
21
+ # An iconv instance to convert from UTF8 to UTF16 Big Endian.
22
+ UTF16toUTF8 = Iconv.new('utf-8', 'utf-16') # :nodoc:
23
+ # An iconv instance to convert from UTF16 Big Endian to UTF8.
24
+ UTF8toUTF16 = Iconv.new('utf-16', 'utf-8') # :nodoc:
25
+ UTF8toUTF16.iconv('no bom')
26
+ if UTF8toUTF16.iconv("\xe2\x82\xac") == "\xac\x20"
27
+ swapper = Class.new do
28
+ def initialize(iconv) # :nodoc:
29
+ @iconv = iconv
30
+ end
31
+
32
+ def iconv(string) # :nodoc:
33
+ result = @iconv.iconv(string)
34
+ PSON.swap!(result)
35
+ end
36
+ end
37
+ UTF8toUTF16 = swapper.new(UTF8toUTF16) # :nodoc:
38
+ end
39
+ if UTF16toUTF8.iconv("\xac\x20") == "\xe2\x82\xac"
40
+ swapper = Class.new do
41
+ def initialize(iconv) # :nodoc:
42
+ @iconv = iconv
43
+ end
44
+
45
+ def iconv(string) # :nodoc:
46
+ string = PSON.swap!(string.dup)
47
+ @iconv.iconv(string)
48
+ end
49
+ end
50
+ UTF16toUTF8 = swapper.new(UTF16toUTF8) # :nodoc:
51
+ end
52
+ rescue Errno::EINVAL, Iconv::InvalidEncoding
53
+ raise MissingUnicodeSupport, "iconv doesn't seem to support UTF-8/UTF-16 conversions"
54
+ ensure
55
+ $VERBOSE = old_verbose
56
+ end
57
+ end
58
+
59
+ # Swap consecutive bytes of _string_ in place.
60
+ def self.swap!(string) # :nodoc:
61
+ 0.upto(string.size / 2) do |i|
62
+ break unless string[2 * i + 1]
63
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
64
+ end
65
+ string
66
+ end
67
+
68
+ # This module holds all the modules/classes that implement PSON's
69
+ # functionality in pure ruby.
70
+ module Pure
71
+ $DEBUG and warn "Using pure library for PSON."
72
+ PSON.parser = Parser
73
+ PSON.generator = Generator
74
+ end
75
+
76
+ PSON_LOADED = true
77
+ end
@@ -0,0 +1,429 @@
1
+ module PSON
2
+ MAP = {
3
+ "\x0" => '\u0000',
4
+ "\x1" => '\u0001',
5
+ "\x2" => '\u0002',
6
+ "\x3" => '\u0003',
7
+ "\x4" => '\u0004',
8
+ "\x5" => '\u0005',
9
+ "\x6" => '\u0006',
10
+ "\x7" => '\u0007',
11
+ "\b" => '\b',
12
+ "\t" => '\t',
13
+ "\n" => '\n',
14
+ "\xb" => '\u000b',
15
+ "\f" => '\f',
16
+ "\r" => '\r',
17
+ "\xe" => '\u000e',
18
+ "\xf" => '\u000f',
19
+ "\x10" => '\u0010',
20
+ "\x11" => '\u0011',
21
+ "\x12" => '\u0012',
22
+ "\x13" => '\u0013',
23
+ "\x14" => '\u0014',
24
+ "\x15" => '\u0015',
25
+ "\x16" => '\u0016',
26
+ "\x17" => '\u0017',
27
+ "\x18" => '\u0018',
28
+ "\x19" => '\u0019',
29
+ "\x1a" => '\u001a',
30
+ "\x1b" => '\u001b',
31
+ "\x1c" => '\u001c',
32
+ "\x1d" => '\u001d',
33
+ "\x1e" => '\u001e',
34
+ "\x1f" => '\u001f',
35
+ '"' => '\"',
36
+ '\\' => '\\\\',
37
+ } # :nodoc:
38
+
39
+ # Convert a UTF8 encoded Ruby string _string_ to a PSON string, encoded with
40
+ # UTF16 big endian characters as \u????, and return it.
41
+ if String.method_defined?(:force_encoding)
42
+ def utf8_to_pson(string) # :nodoc:
43
+ string = string.dup
44
+ string << '' # XXX workaround: avoid buffer sharing
45
+ string.force_encoding(Encoding::ASCII_8BIT)
46
+ string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
47
+ string.gsub!(/(
48
+ (?:
49
+ [\xc2-\xdf][\x80-\xbf] |
50
+ [\xe0-\xef][\x80-\xbf]{2} |
51
+ [\xf0-\xf4][\x80-\xbf]{3}
52
+ )+ |
53
+ [\x80-\xc1\xf5-\xff] # invalid
54
+ )/nx) { |c|
55
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
56
+ s = PSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
57
+ s.gsub!(/.{4}/n, '\\\\u\&')
58
+ }
59
+ string.force_encoding(Encoding::UTF_8)
60
+ string
61
+ rescue Iconv::Failure => e
62
+ raise GeneratorError, "Caught #{e.class}: #{e}"
63
+ end
64
+ else
65
+ def utf8_to_pson(string) # :nodoc:
66
+ string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
67
+ string.gsub!(/(
68
+ (?:
69
+ [\xc2-\xdf][\x80-\xbf] |
70
+ [\xe0-\xef][\x80-\xbf]{2} |
71
+ [\xf0-\xf4][\x80-\xbf]{3}
72
+ )+ |
73
+ [\x80-\xc1\xf5-\xff] # invalid
74
+ )/nx) { |c|
75
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
76
+ s = PSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
77
+ s.gsub!(/.{4}/n, '\\\\u\&')
78
+ }
79
+ string
80
+ rescue Iconv::Failure => e
81
+ raise GeneratorError, "Caught #{e.class}: #{e}"
82
+ end
83
+ end
84
+ module_function :utf8_to_pson
85
+
86
+ module Pure
87
+ module Generator
88
+ # This class is used to create State instances, that are use to hold data
89
+ # while generating a PSON text from a a Ruby data structure.
90
+ class State
91
+ # Creates a State object from _opts_, which ought to be Hash to create
92
+ # a new State instance configured by _opts_, something else to create
93
+ # an unconfigured instance. If _opts_ is a State object, it is just
94
+ # returned.
95
+ def self.from_state(opts)
96
+ case opts
97
+ when self
98
+ opts
99
+ when Hash
100
+ new(opts)
101
+ else
102
+ new
103
+ end
104
+ end
105
+
106
+ # Instantiates a new State object, configured by _opts_.
107
+ #
108
+ # _opts_ can have the following keys:
109
+ #
110
+ # * *indent*: a string used to indent levels (default: ''),
111
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
112
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
113
+ # * *object_nl*: a string that is put at the end of a PSON object (default: ''),
114
+ # * *array_nl*: a string that is put at the end of a PSON array (default: ''),
115
+ # * *check_circular*: true if checking for circular data structures
116
+ # should be done (the default), false otherwise.
117
+ # * *check_circular*: true if checking for circular data structures
118
+ # should be done, false (the default) otherwise.
119
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
120
+ # generated, otherwise an exception is thrown, if these values are
121
+ # encountered. This options defaults to false.
122
+ def initialize(opts = {})
123
+ @seen = {}
124
+ @indent = ''
125
+ @space = ''
126
+ @space_before = ''
127
+ @object_nl = ''
128
+ @array_nl = ''
129
+ @check_circular = true
130
+ @allow_nan = false
131
+ configure opts
132
+ end
133
+
134
+ # This string is used to indent levels in the PSON text.
135
+ attr_accessor :indent
136
+
137
+ # This string is used to insert a space between the tokens in a PSON
138
+ # string.
139
+ attr_accessor :space
140
+
141
+ # This string is used to insert a space before the ':' in PSON objects.
142
+ attr_accessor :space_before
143
+
144
+ # This string is put at the end of a line that holds a PSON object (or
145
+ # Hash).
146
+ attr_accessor :object_nl
147
+
148
+ # This string is put at the end of a line that holds a PSON array.
149
+ attr_accessor :array_nl
150
+
151
+ # This integer returns the maximum level of data structure nesting in
152
+ # the generated PSON, max_nesting = 0 if no maximum is checked.
153
+ attr_accessor :max_nesting
154
+
155
+ def check_max_nesting(depth) # :nodoc:
156
+ return if @max_nesting.zero?
157
+ current_nesting = depth + 1
158
+ current_nesting > @max_nesting and
159
+ raise NestingError, "nesting of #{current_nesting} is too deep"
160
+ end
161
+
162
+ # Returns true, if circular data structures should be checked,
163
+ # otherwise returns false.
164
+ def check_circular?
165
+ @check_circular
166
+ end
167
+
168
+ # Returns true if NaN, Infinity, and -Infinity should be considered as
169
+ # valid PSON and output.
170
+ def allow_nan?
171
+ @allow_nan
172
+ end
173
+
174
+ # Returns _true_, if _object_ was already seen during this generating
175
+ # run.
176
+ def seen?(object)
177
+ @seen.key?(object.__id__)
178
+ end
179
+
180
+ # Remember _object_, to find out if it was already encountered (if a
181
+ # cyclic data structure is if a cyclic data structure is rendered).
182
+ def remember(object)
183
+ @seen[object.__id__] = true
184
+ end
185
+
186
+ # Forget _object_ for this generating run.
187
+ def forget(object)
188
+ @seen.delete object.__id__
189
+ end
190
+
191
+ # Configure this State instance with the Hash _opts_, and return
192
+ # itself.
193
+ def configure(opts)
194
+ @indent = opts[:indent] if opts.key?(:indent)
195
+ @space = opts[:space] if opts.key?(:space)
196
+ @space_before = opts[:space_before] if opts.key?(:space_before)
197
+ @object_nl = opts[:object_nl] if opts.key?(:object_nl)
198
+ @array_nl = opts[:array_nl] if opts.key?(:array_nl)
199
+ @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
200
+ @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
201
+ if !opts.key?(:max_nesting) # defaults to 19
202
+ @max_nesting = 19
203
+ elsif opts[:max_nesting]
204
+ @max_nesting = opts[:max_nesting]
205
+ else
206
+ @max_nesting = 0
207
+ end
208
+ self
209
+ end
210
+
211
+ # Returns the configuration instance variables as a hash, that can be
212
+ # passed to the configure method.
213
+ def to_h
214
+ result = {}
215
+ for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
216
+ result[iv.intern] = instance_variable_get("@#{iv}")
217
+ end
218
+ result
219
+ end
220
+ end
221
+
222
+ module GeneratorMethods
223
+ module Object
224
+ # Converts this object to a string (calling #to_s), converts
225
+ # it to a PSON string, and returns the result. This is a fallback, if no
226
+ # special method #to_pson was defined for some object.
227
+ def to_pson(*) to_s.to_pson end
228
+ end
229
+
230
+ module Hash
231
+ # Returns a PSON string containing a PSON object, that is unparsed from
232
+ # this Hash instance.
233
+ # _state_ is a PSON::State object, that can also be used to configure the
234
+ # produced PSON string output further.
235
+ # _depth_ is used to find out nesting depth, to indent accordingly.
236
+ def to_pson(state = nil, depth = 0, *)
237
+ if state
238
+ state = PSON.state.from_state(state)
239
+ state.check_max_nesting(depth)
240
+ pson_check_circular(state) { pson_transform(state, depth) }
241
+ else
242
+ pson_transform(state, depth)
243
+ end
244
+ end
245
+
246
+ private
247
+
248
+ def pson_check_circular(state)
249
+ if state and state.check_circular?
250
+ state.seen?(self) and raise PSON::CircularDatastructure,
251
+ "circular data structures not supported!"
252
+ state.remember self
253
+ end
254
+ yield
255
+ ensure
256
+ state and state.forget self
257
+ end
258
+
259
+ def pson_shift(state, depth)
260
+ state and not state.object_nl.empty? or return ''
261
+ state.indent * depth
262
+ end
263
+
264
+ def pson_transform(state, depth)
265
+ delim = ','
266
+ if state
267
+ delim << state.object_nl
268
+ result = '{'
269
+ result << state.object_nl
270
+ result << map { |key,value|
271
+ s = pson_shift(state, depth + 1)
272
+ s << key.to_s.to_pson(state, depth + 1)
273
+ s << state.space_before
274
+ s << ':'
275
+ s << state.space
276
+ s << value.to_pson(state, depth + 1)
277
+ }.join(delim)
278
+ result << state.object_nl
279
+ result << pson_shift(state, depth)
280
+ result << '}'
281
+ else
282
+ result = '{'
283
+ result << map { |key,value|
284
+ key.to_s.to_pson << ':' << value.to_pson
285
+ }.join(delim)
286
+ result << '}'
287
+ end
288
+ result
289
+ end
290
+ end
291
+
292
+ module Array
293
+ # Returns a PSON string containing a PSON array, that is unparsed from
294
+ # this Array instance.
295
+ # _state_ is a PSON::State object, that can also be used to configure the
296
+ # produced PSON string output further.
297
+ # _depth_ is used to find out nesting depth, to indent accordingly.
298
+ def to_pson(state = nil, depth = 0, *)
299
+ if state
300
+ state = PSON.state.from_state(state)
301
+ state.check_max_nesting(depth)
302
+ pson_check_circular(state) { pson_transform(state, depth) }
303
+ else
304
+ pson_transform(state, depth)
305
+ end
306
+ end
307
+
308
+ private
309
+
310
+ def pson_check_circular(state)
311
+ if state and state.check_circular?
312
+ state.seen?(self) and raise PSON::CircularDatastructure,
313
+ "circular data structures not supported!"
314
+ state.remember self
315
+ end
316
+ yield
317
+ ensure
318
+ state and state.forget self
319
+ end
320
+
321
+ def pson_shift(state, depth)
322
+ state and not state.array_nl.empty? or return ''
323
+ state.indent * depth
324
+ end
325
+
326
+ def pson_transform(state, depth)
327
+ delim = ','
328
+ if state
329
+ delim << state.array_nl
330
+ result = '['
331
+ result << state.array_nl
332
+ result << map { |value|
333
+ pson_shift(state, depth + 1) << value.to_pson(state, depth + 1)
334
+ }.join(delim)
335
+ result << state.array_nl
336
+ result << pson_shift(state, depth)
337
+ result << ']'
338
+ else
339
+ '[' << map { |value| value.to_pson }.join(delim) << ']'
340
+ end
341
+ end
342
+ end
343
+
344
+ module Integer
345
+ # Returns a PSON string representation for this Integer number.
346
+ def to_pson(*) to_s end
347
+ end
348
+
349
+ module Float
350
+ # Returns a PSON string representation for this Float number.
351
+ def to_pson(state = nil, *)
352
+ case
353
+ when infinite?
354
+ if !state || state.allow_nan?
355
+ to_s
356
+ else
357
+ raise GeneratorError, "#{self} not allowed in PSON"
358
+ end
359
+ when nan?
360
+ if !state || state.allow_nan?
361
+ to_s
362
+ else
363
+ raise GeneratorError, "#{self} not allowed in PSON"
364
+ end
365
+ else
366
+ to_s
367
+ end
368
+ end
369
+ end
370
+
371
+ module String
372
+ # This string should be encoded with UTF-8 A call to this method
373
+ # returns a PSON string encoded with UTF16 big endian characters as
374
+ # \u????.
375
+ def to_pson(*)
376
+ '"' << PSON.utf8_to_pson(self) << '"'
377
+ end
378
+
379
+ # Module that holds the extinding methods if, the String module is
380
+ # included.
381
+ module Extend
382
+ # Raw Strings are PSON Objects (the raw bytes are stored in an array for the
383
+ # key "raw"). The Ruby String can be created by this module method.
384
+ def pson_create(o)
385
+ o['raw'].pack('C*')
386
+ end
387
+ end
388
+
389
+ # Extends _modul_ with the String::Extend module.
390
+ def self.included(modul)
391
+ modul.extend Extend
392
+ end
393
+
394
+ # This method creates a raw object hash, that can be nested into
395
+ # other data structures and will be unparsed as a raw string. This
396
+ # method should be used, if you want to convert raw strings to PSON
397
+ # instead of UTF-8 strings, e. g. binary data.
398
+ def to_pson_raw_object
399
+ {
400
+ PSON.create_id => self.class.name,
401
+ 'raw' => self.unpack('C*'),
402
+ }
403
+ end
404
+
405
+ # This method creates a PSON text from the result of
406
+ # a call to to_pson_raw_object of this String.
407
+ def to_pson_raw(*args)
408
+ to_pson_raw_object.to_pson(*args)
409
+ end
410
+ end
411
+
412
+ module TrueClass
413
+ # Returns a PSON string for true: 'true'.
414
+ def to_pson(*) 'true' end
415
+ end
416
+
417
+ module FalseClass
418
+ # Returns a PSON string for false: 'false'.
419
+ def to_pson(*) 'false' end
420
+ end
421
+
422
+ module NilClass
423
+ # Returns a PSON string for nil: 'null'.
424
+ def to_pson(*) 'null' end
425
+ end
426
+ end
427
+ end
428
+ end
429
+ end