octocatalog-diff 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +7 -0
  2. data/.version +1 -0
  3. data/LICENSE +20 -0
  4. data/README.md +82 -0
  5. data/bin/octocatalog-diff +75 -0
  6. data/doc/advanced-bootstrap.md +33 -0
  7. data/doc/advanced-cache-dir.md +24 -0
  8. data/doc/advanced-catalog-only.md +37 -0
  9. data/doc/advanced-ci.md +13 -0
  10. data/doc/advanced-dynamic-ignores.md +123 -0
  11. data/doc/advanced-future-parser.md +11 -0
  12. data/doc/advanced-ignores.md +224 -0
  13. data/doc/advanced-output-formats.md +96 -0
  14. data/doc/advanced-output-hacks.md +45 -0
  15. data/doc/advanced-override-facts.md +67 -0
  16. data/doc/advanced-pe-enc.md +52 -0
  17. data/doc/advanced-puppet-master.md +50 -0
  18. data/doc/advanced-puppet-versions.md +9 -0
  19. data/doc/advanced-storeconfigs.md +72 -0
  20. data/doc/advanced-using-without-git.md +15 -0
  21. data/doc/advanced.md +43 -0
  22. data/doc/basic.md +70 -0
  23. data/doc/configuration-enc.md +69 -0
  24. data/doc/configuration-hiera.md +103 -0
  25. data/doc/configuration-puppetdb.md +49 -0
  26. data/doc/configuration.md +51 -0
  27. data/doc/dev/README.md +1 -0
  28. data/doc/dev/coverage.md +34 -0
  29. data/doc/dev/how-to-add-options.md +83 -0
  30. data/doc/dev/integration-tests.md +63 -0
  31. data/doc/dev/releasing.md +19 -0
  32. data/doc/installation.md +49 -0
  33. data/doc/limitations.md +34 -0
  34. data/doc/optionsref.md +947 -0
  35. data/doc/requirements.md +16 -0
  36. data/doc/roadmap.md +26 -0
  37. data/doc/similar.md +17 -0
  38. data/doc/troubleshooting.md +54 -0
  39. data/lib/octocatalog-diff.rb +12 -0
  40. data/lib/octocatalog-diff/bootstrap.rb +53 -0
  41. data/lib/octocatalog-diff/catalog-diff/cli.rb +205 -0
  42. data/lib/octocatalog-diff/catalog-diff/cli/catalogs.rb +240 -0
  43. data/lib/octocatalog-diff/catalog-diff/cli/diffs.rb +145 -0
  44. data/lib/octocatalog-diff/catalog-diff/cli/helpers/fact_override.rb +99 -0
  45. data/lib/octocatalog-diff/catalog-diff/cli/options.rb +173 -0
  46. data/lib/octocatalog-diff/catalog-diff/cli/options/basedir.rb +14 -0
  47. data/lib/octocatalog-diff/catalog-diff/cli/options/bootstrap_environment.rb +18 -0
  48. data/lib/octocatalog-diff/catalog-diff/cli/options/bootstrap_script.rb +14 -0
  49. data/lib/octocatalog-diff/catalog-diff/cli/options/bootstrap_then_exit.rb +12 -0
  50. data/lib/octocatalog-diff/catalog-diff/cli/options/bootstrapped_dirs.rb +18 -0
  51. data/lib/octocatalog-diff/catalog-diff/cli/options/cached_master_dir.rb +21 -0
  52. data/lib/octocatalog-diff/catalog-diff/cli/options/catalog_only.rb +14 -0
  53. data/lib/octocatalog-diff/catalog-diff/cli/options/color.rb +13 -0
  54. data/lib/octocatalog-diff/catalog-diff/cli/options/compare_file_text.rb +15 -0
  55. data/lib/octocatalog-diff/catalog-diff/cli/options/debug.rb +12 -0
  56. data/lib/octocatalog-diff/catalog-diff/cli/options/display_datatype_changes.rb +16 -0
  57. data/lib/octocatalog-diff/catalog-diff/cli/options/display_detail_add.rb +12 -0
  58. data/lib/octocatalog-diff/catalog-diff/cli/options/display_source_file_line.rb +12 -0
  59. data/lib/octocatalog-diff/catalog-diff/cli/options/enc.rb +31 -0
  60. data/lib/octocatalog-diff/catalog-diff/cli/options/existing_catalogs.rb +25 -0
  61. data/lib/octocatalog-diff/catalog-diff/cli/options/fact_file.rb +23 -0
  62. data/lib/octocatalog-diff/catalog-diff/cli/options/fact_override.rb +19 -0
  63. data/lib/octocatalog-diff/catalog-diff/cli/options/facts_terminus.rb +16 -0
  64. data/lib/octocatalog-diff/catalog-diff/cli/options/from_puppetdb.rb +13 -0
  65. data/lib/octocatalog-diff/catalog-diff/cli/options/header.rb +24 -0
  66. data/lib/octocatalog-diff/catalog-diff/cli/options/hiera_config.rb +18 -0
  67. data/lib/octocatalog-diff/catalog-diff/cli/options/hiera_path_strip.rb +12 -0
  68. data/lib/octocatalog-diff/catalog-diff/cli/options/hostname.rb +13 -0
  69. data/lib/octocatalog-diff/catalog-diff/cli/options/ignore.rb +24 -0
  70. data/lib/octocatalog-diff/catalog-diff/cli/options/ignore_attr.rb +16 -0
  71. data/lib/octocatalog-diff/catalog-diff/cli/options/ignore_tags.rb +23 -0
  72. data/lib/octocatalog-diff/catalog-diff/cli/options/include_tags.rb +12 -0
  73. data/lib/octocatalog-diff/catalog-diff/cli/options/master_cache_branch.rb +12 -0
  74. data/lib/octocatalog-diff/catalog-diff/cli/options/output_file.rb +15 -0
  75. data/lib/octocatalog-diff/catalog-diff/cli/options/output_format.rb +15 -0
  76. data/lib/octocatalog-diff/catalog-diff/cli/options/parallel.rb +12 -0
  77. data/lib/octocatalog-diff/catalog-diff/cli/options/parser.rb +48 -0
  78. data/lib/octocatalog-diff/catalog-diff/cli/options/pass_env_vars.rb +19 -0
  79. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_ssl_ca.rb +15 -0
  80. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_ssl_client_cert.rb +14 -0
  81. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_ssl_client_key.rb +14 -0
  82. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_token.rb +15 -0
  83. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_token_file.rb +17 -0
  84. data/lib/octocatalog-diff/catalog-diff/cli/options/pe_enc_url.rb +19 -0
  85. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_binary.rb +16 -0
  86. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_master.rb +16 -0
  87. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_master_api_version.rb +20 -0
  88. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_master_ssl_ca.rb +19 -0
  89. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_master_ssl_client_cert.rb +19 -0
  90. data/lib/octocatalog-diff/catalog-diff/cli/options/puppet_master_ssl_client_key.rb +19 -0
  91. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_ssl_ca.rb +15 -0
  92. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_ssl_client_cert.rb +14 -0
  93. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_ssl_client_key.rb +14 -0
  94. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_ssl_client_password.rb +14 -0
  95. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_ssl_client_password_file.rb +13 -0
  96. data/lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_url.rb +18 -0
  97. data/lib/octocatalog-diff/catalog-diff/cli/options/quiet.rb +12 -0
  98. data/lib/octocatalog-diff/catalog-diff/cli/options/retry_failed_catalog.rb +13 -0
  99. data/lib/octocatalog-diff/catalog-diff/cli/options/safe_to_delete_cached_master_dir.rb +15 -0
  100. data/lib/octocatalog-diff/catalog-diff/cli/options/storeconfigs.rb +12 -0
  101. data/lib/octocatalog-diff/catalog-diff/cli/options/suppress_absent_file_details.rb +14 -0
  102. data/lib/octocatalog-diff/catalog-diff/cli/options/to_from_branch.rb +16 -0
  103. data/lib/octocatalog-diff/catalog-diff/cli/printer.rb +52 -0
  104. data/lib/octocatalog-diff/catalog-diff/differ.rb +615 -0
  105. data/lib/octocatalog-diff/catalog-diff/display.rb +125 -0
  106. data/lib/octocatalog-diff/catalog-diff/display/json.rb +25 -0
  107. data/lib/octocatalog-diff/catalog-diff/display/text.rb +452 -0
  108. data/lib/octocatalog-diff/catalog-util/bootstrap.rb +145 -0
  109. data/lib/octocatalog-diff/catalog-util/builddir.rb +289 -0
  110. data/lib/octocatalog-diff/catalog-util/cached_master_directory.rb +169 -0
  111. data/lib/octocatalog-diff/catalog-util/command.rb +96 -0
  112. data/lib/octocatalog-diff/catalog-util/enc.rb +77 -0
  113. data/lib/octocatalog-diff/catalog-util/enc/noop.rb +22 -0
  114. data/lib/octocatalog-diff/catalog-util/enc/pe.rb +99 -0
  115. data/lib/octocatalog-diff/catalog-util/enc/pe/v1.rb +61 -0
  116. data/lib/octocatalog-diff/catalog-util/enc/script.rb +88 -0
  117. data/lib/octocatalog-diff/catalog-util/facts.rb +89 -0
  118. data/lib/octocatalog-diff/catalog-util/fileresources.rb +83 -0
  119. data/lib/octocatalog-diff/catalog-util/git.rb +65 -0
  120. data/lib/octocatalog-diff/catalog.rb +209 -0
  121. data/lib/octocatalog-diff/catalog/computed.rb +205 -0
  122. data/lib/octocatalog-diff/catalog/json.rb +30 -0
  123. data/lib/octocatalog-diff/catalog/noop.rb +19 -0
  124. data/lib/octocatalog-diff/catalog/puppetdb.rb +82 -0
  125. data/lib/octocatalog-diff/catalog/puppetmaster.rb +121 -0
  126. data/lib/octocatalog-diff/external/pson/LICENSE +17 -0
  127. data/lib/octocatalog-diff/external/pson/README.md +20 -0
  128. data/lib/octocatalog-diff/external/pson/common.rb +370 -0
  129. data/lib/octocatalog-diff/external/pson/pure.rb +15 -0
  130. data/lib/octocatalog-diff/external/pson/pure/generator.rb +395 -0
  131. data/lib/octocatalog-diff/external/pson/pure/parser.rb +307 -0
  132. data/lib/octocatalog-diff/external/pson/version.rb +8 -0
  133. data/lib/octocatalog-diff/facts.rb +125 -0
  134. data/lib/octocatalog-diff/facts/json.rb +20 -0
  135. data/lib/octocatalog-diff/facts/puppetdb.rb +59 -0
  136. data/lib/octocatalog-diff/facts/yaml.rb +29 -0
  137. data/lib/octocatalog-diff/puppetdb.rb +163 -0
  138. data/lib/octocatalog-diff/util/colored.rb +20 -0
  139. data/lib/octocatalog-diff/util/httparty.rb +158 -0
  140. data/lib/octocatalog-diff/util/parallel.rb +170 -0
  141. data/lib/octocatalog-diff/util/puppetversion.rb +24 -0
  142. data/lib/octocatalog-diff/version.rb +7 -0
  143. metadata +386 -0
@@ -0,0 +1,15 @@
1
+ require_relative 'common'
2
+ require_relative 'pure/parser'
3
+ require_relative 'pure/generator'
4
+
5
+ module PSON
6
+ # This module holds all the modules/classes that implement PSON's
7
+ # functionality in pure ruby.
8
+ module Pure
9
+ $DEBUG and warn "Using pure library for PSON."
10
+ PSON.parser = Parser
11
+ PSON.generator = Generator
12
+ end
13
+
14
+ PSON_LOADED = true
15
+ end
@@ -0,0 +1,395 @@
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
48
+ rescue => e
49
+ raise GeneratorError, "Caught #{e.class}: #{e}", e.backtrace
50
+ end
51
+ else
52
+ def utf8_to_pson(string) # :nodoc:
53
+ string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
54
+ end
55
+ end
56
+ module_function :utf8_to_pson
57
+
58
+ module Pure
59
+ module Generator
60
+ # This class is used to create State instances, that are use to hold data
61
+ # while generating a PSON text from a Ruby data structure.
62
+ class State
63
+ # Creates a State object from _opts_, which ought to be Hash to create
64
+ # a new State instance configured by _opts_, something else to create
65
+ # an unconfigured instance. If _opts_ is a State object, it is just
66
+ # returned.
67
+ def self.from_state(opts)
68
+ case opts
69
+ when self
70
+ opts
71
+ when Hash
72
+ new(opts)
73
+ else
74
+ new
75
+ end
76
+ end
77
+
78
+ # Instantiates a new State object, configured by _opts_.
79
+ #
80
+ # _opts_ can have the following keys:
81
+ #
82
+ # * *indent*: a string used to indent levels (default: ''),
83
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
84
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
85
+ # * *object_nl*: a string that is put at the end of a PSON object (default: ''),
86
+ # * *array_nl*: a string that is put at the end of a PSON array (default: ''),
87
+ # * *check_circular*: true if checking for circular data structures
88
+ # should be done (the default), false otherwise.
89
+ # * *check_circular*: true if checking for circular data structures
90
+ # should be done, false (the default) otherwise.
91
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
92
+ # generated, otherwise an exception is thrown, if these values are
93
+ # encountered. This options defaults to false.
94
+ def initialize(opts = {})
95
+ @seen = {}
96
+ @indent = ''
97
+ @space = ''
98
+ @space_before = ''
99
+ @object_nl = ''
100
+ @array_nl = ''
101
+ @check_circular = true
102
+ @allow_nan = false
103
+ configure opts
104
+ end
105
+
106
+ # This string is used to indent levels in the PSON text.
107
+ attr_accessor :indent
108
+
109
+ # This string is used to insert a space between the tokens in a PSON
110
+ # string.
111
+ attr_accessor :space
112
+
113
+ # This string is used to insert a space before the ':' in PSON objects.
114
+ attr_accessor :space_before
115
+
116
+ # This string is put at the end of a line that holds a PSON object (or
117
+ # Hash).
118
+ attr_accessor :object_nl
119
+
120
+ # This string is put at the end of a line that holds a PSON array.
121
+ attr_accessor :array_nl
122
+
123
+ # This integer returns the maximum level of data structure nesting in
124
+ # the generated PSON, max_nesting = 0 if no maximum is checked.
125
+ attr_accessor :max_nesting
126
+
127
+ def check_max_nesting(depth) # :nodoc:
128
+ return if @max_nesting.zero?
129
+ current_nesting = depth + 1
130
+ current_nesting > @max_nesting and
131
+ raise NestingError, "nesting of #{current_nesting} is too deep"
132
+ end
133
+
134
+ # Returns true, if circular data structures should be checked,
135
+ # otherwise returns false.
136
+ def check_circular?
137
+ @check_circular
138
+ end
139
+
140
+ # Returns true if NaN, Infinity, and -Infinity should be considered as
141
+ # valid PSON and output.
142
+ def allow_nan?
143
+ @allow_nan
144
+ end
145
+
146
+ # Returns _true_, if _object_ was already seen during this generating
147
+ # run.
148
+ def seen?(object)
149
+ @seen.key?(object.__id__)
150
+ end
151
+
152
+ # Remember _object_, to find out if it was already encountered (if a
153
+ # cyclic data structure is if a cyclic data structure is rendered).
154
+ def remember(object)
155
+ @seen[object.__id__] = true
156
+ end
157
+
158
+ # Forget _object_ for this generating run.
159
+ def forget(object)
160
+ @seen.delete object.__id__
161
+ end
162
+
163
+ # Configure this State instance with the Hash _opts_, and return
164
+ # itself.
165
+ def configure(opts)
166
+ @indent = opts[:indent] if opts.key?(:indent)
167
+ @space = opts[:space] if opts.key?(:space)
168
+ @space_before = opts[:space_before] if opts.key?(:space_before)
169
+ @object_nl = opts[:object_nl] if opts.key?(:object_nl)
170
+ @array_nl = opts[:array_nl] if opts.key?(:array_nl)
171
+ @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
172
+ @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
173
+ if !opts.key?(:max_nesting) # defaults to 19
174
+ @max_nesting = 19
175
+ elsif opts[:max_nesting]
176
+ @max_nesting = opts[:max_nesting]
177
+ else
178
+ @max_nesting = 0
179
+ end
180
+ self
181
+ end
182
+
183
+ # Returns the configuration instance variables as a hash, that can be
184
+ # passed to the configure method.
185
+ def to_h
186
+ result = {}
187
+ for iv in %w{indent space space_before object_nl array_nl check_circular allow_nan max_nesting}
188
+ result[iv.intern] = instance_variable_get("@#{iv}")
189
+ end
190
+ result
191
+ end
192
+ end
193
+
194
+ module GeneratorMethods
195
+ module Object
196
+ # Converts this object to a string (calling #to_s), converts
197
+ # it to a PSON string, and returns the result. This is a fallback, if no
198
+ # special method #to_pson was defined for some object.
199
+ def to_pson(*) to_s.to_pson end
200
+ end
201
+
202
+ module Hash
203
+ # Returns a PSON string containing a PSON object, that is unparsed from
204
+ # this Hash instance.
205
+ # _state_ is a PSON::State object, that can also be used to configure the
206
+ # produced PSON string output further.
207
+ # _depth_ is used to find out nesting depth, to indent accordingly.
208
+ def to_pson(state = nil, depth = 0, *)
209
+ if state
210
+ state = PSON.state.from_state(state)
211
+ state.check_max_nesting(depth)
212
+ pson_check_circular(state) { pson_transform(state, depth) }
213
+ else
214
+ pson_transform(state, depth)
215
+ end
216
+ end
217
+
218
+ private
219
+
220
+ def pson_check_circular(state)
221
+ if state and state.check_circular?
222
+ state.seen?(self) and raise PSON::CircularDatastructure,
223
+ "circular data structures not supported!"
224
+ state.remember self
225
+ end
226
+ yield
227
+ ensure
228
+ state and state.forget self
229
+ end
230
+
231
+ def pson_shift(state, depth)
232
+ state and not state.object_nl.empty? or return ''
233
+ state.indent * depth
234
+ end
235
+
236
+ def pson_transform(state, depth)
237
+ delim = ','
238
+ if state
239
+ delim << state.object_nl
240
+ result = '{'
241
+ result << state.object_nl
242
+ result << map { |key,value|
243
+ s = pson_shift(state, depth + 1)
244
+ s << key.to_s.to_pson(state, depth + 1)
245
+ s << state.space_before
246
+ s << ':'
247
+ s << state.space
248
+ s << value.to_pson(state, depth + 1)
249
+ }.join(delim)
250
+ result << state.object_nl
251
+ result << pson_shift(state, depth)
252
+ result << '}'
253
+ else
254
+ result = '{'
255
+ result << map { |key,value|
256
+ key.to_s.to_pson << ':' << value.to_pson
257
+ }.join(delim)
258
+ result << '}'
259
+ end
260
+ result
261
+ end
262
+ end
263
+
264
+ module Array
265
+ # Returns a PSON string containing a PSON array, that is unparsed from
266
+ # this Array instance.
267
+ # _state_ is a PSON::State object, that can also be used to configure the
268
+ # produced PSON string output further.
269
+ # _depth_ is used to find out nesting depth, to indent accordingly.
270
+ def to_pson(state = nil, depth = 0, *)
271
+ if state
272
+ state = PSON.state.from_state(state)
273
+ state.check_max_nesting(depth)
274
+ pson_check_circular(state) { pson_transform(state, depth) }
275
+ else
276
+ pson_transform(state, depth)
277
+ end
278
+ end
279
+
280
+ private
281
+
282
+ def pson_check_circular(state)
283
+ if state and state.check_circular?
284
+ state.seen?(self) and raise PSON::CircularDatastructure,
285
+ "circular data structures not supported!"
286
+ state.remember self
287
+ end
288
+ yield
289
+ ensure
290
+ state and state.forget self
291
+ end
292
+
293
+ def pson_shift(state, depth)
294
+ state and not state.array_nl.empty? or return ''
295
+ state.indent * depth
296
+ end
297
+
298
+ def pson_transform(state, depth)
299
+ delim = ','
300
+ if state
301
+ delim << state.array_nl
302
+ result = '['
303
+ result << state.array_nl
304
+ result << map { |value|
305
+ pson_shift(state, depth + 1) << value.to_pson(state, depth + 1)
306
+ }.join(delim)
307
+ result << state.array_nl
308
+ result << pson_shift(state, depth)
309
+ result << ']'
310
+ else
311
+ '[' << map { |value| value.to_pson }.join(delim) << ']'
312
+ end
313
+ end
314
+ end
315
+
316
+ module Integer
317
+ # Returns a PSON string representation for this Integer number.
318
+ def to_pson(*) to_s end
319
+ end
320
+
321
+ module Float
322
+ # Returns a PSON string representation for this Float number.
323
+ def to_pson(state = nil, *)
324
+ if infinite? || nan?
325
+ if !state || state.allow_nan?
326
+ to_s
327
+ else
328
+ raise GeneratorError, "#{self} not allowed in PSON"
329
+ end
330
+ else
331
+ to_s
332
+ end
333
+ end
334
+ end
335
+
336
+ module String
337
+ # This string should be encoded with UTF-8 A call to this method
338
+ # returns a PSON string encoded with UTF16 big endian characters as
339
+ # \u????.
340
+ def to_pson(*)
341
+ '"' << PSON.utf8_to_pson(self) << '"'
342
+ end
343
+
344
+ # Module that holds the extinding methods if, the String module is
345
+ # included.
346
+ module Extend
347
+ # Raw Strings are PSON Objects (the raw bytes are stored in an array for the
348
+ # key "raw"). The Ruby String can be created by this module method.
349
+ def pson_create(o)
350
+ o['raw'].pack('C*')
351
+ end
352
+ end
353
+
354
+ # Extends _modul_ with the String::Extend module.
355
+ def self.included(modul)
356
+ modul.extend Extend
357
+ end
358
+
359
+ # This method creates a raw object hash, that can be nested into
360
+ # other data structures and will be unparsed as a raw string. This
361
+ # method should be used, if you want to convert raw strings to PSON
362
+ # instead of UTF-8 strings, e.g. binary data.
363
+ def to_pson_raw_object
364
+ # create_id will be ignored during deserialization
365
+ {
366
+ PSON.create_id => self.class.name,
367
+ 'raw' => self.unpack('C*'),
368
+ }
369
+ end
370
+
371
+ # This method creates a PSON text from the result of
372
+ # a call to to_pson_raw_object of this String.
373
+ def to_pson_raw(*args)
374
+ to_pson_raw_object.to_pson(*args)
375
+ end
376
+ end
377
+
378
+ module TrueClass
379
+ # Returns a PSON string for true: 'true'.
380
+ def to_pson(*) 'true' end
381
+ end
382
+
383
+ module FalseClass
384
+ # Returns a PSON string for false: 'false'.
385
+ def to_pson(*) 'false' end
386
+ end
387
+
388
+ module NilClass
389
+ # Returns a PSON string for nil: 'null'.
390
+ def to_pson(*) 'null' end
391
+ end
392
+ end
393
+ end
394
+ end
395
+ end
@@ -0,0 +1,307 @@
1
+ require 'strscan'
2
+
3
+ module PSON
4
+ module Pure
5
+ # This class implements the PSON parser that is used to parse a PSON string
6
+ # into a Ruby data structure.
7
+ class Parser < StringScanner
8
+ STRING = /" ((?:[^\x0-\x1f"\\] |
9
+ # escaped special characters:
10
+ \\["\\\/bfnrt] |
11
+ \\u[0-9a-fA-F]{4} |
12
+ # match all but escaped special characters:
13
+ \\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*)
14
+ "/nx
15
+ INTEGER = /(-?0|-?[1-9]\d*)/
16
+ FLOAT = /(-?
17
+ (?:0|[1-9]\d*)
18
+ (?:
19
+ \.\d+(?i:e[+-]?\d+) |
20
+ \.\d+ |
21
+ (?i:e[+-]?\d+)
22
+ )
23
+ )/x
24
+ NAN = /NaN/
25
+ INFINITY = /Infinity/
26
+ MINUS_INFINITY = /-Infinity/
27
+ OBJECT_OPEN = /\{/
28
+ OBJECT_CLOSE = /\}/
29
+ ARRAY_OPEN = /\[/
30
+ ARRAY_CLOSE = /\]/
31
+ PAIR_DELIMITER = /:/
32
+ COLLECTION_DELIMITER = /,/
33
+ TRUE = /true/
34
+ FALSE = /false/
35
+ NULL = /null/
36
+ IGNORE = %r(
37
+ (?:
38
+ //[^\n\r]*[\n\r]| # line comments
39
+ /\* # c-style comments
40
+ (?:
41
+ [^*/]| # normal chars
42
+ /[^*]| # slashes that do not start a nested comment
43
+ \*[^/]| # asterisks that do not end this comment
44
+ /(?=\*/) # single slash before this comment's end
45
+ )*
46
+ \*/ # the End of this comment
47
+ |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
48
+ )+
49
+ )mx
50
+
51
+ UNPARSED = Object.new
52
+
53
+ # Creates a new PSON::Pure::Parser instance for the string _source_.
54
+ #
55
+ # It will be configured by the _opts_ hash. _opts_ can have the following
56
+ # keys:
57
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
58
+ # structures. Disable depth checking with :max_nesting => false|nil|0,
59
+ # it defaults to 19.
60
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
+ # to false.
63
+ # * *object_class*: Defaults to Hash
64
+ # * *array_class*: Defaults to Array
65
+ def initialize(source, opts = {})
66
+ source = convert_encoding source
67
+ super source
68
+ if !opts.key?(:max_nesting) # defaults to 19
69
+ @max_nesting = 19
70
+ elsif opts[:max_nesting]
71
+ @max_nesting = opts[:max_nesting]
72
+ else
73
+ @max_nesting = 0
74
+ end
75
+ @allow_nan = !!opts[:allow_nan]
76
+ @object_class = opts[:object_class] || Hash
77
+ @array_class = opts[:array_class] || Array
78
+ end
79
+
80
+ alias source string
81
+
82
+ # Parses the current PSON string _source_ and returns the complete data
83
+ # structure as a result.
84
+ def parse
85
+ reset
86
+ obj = nil
87
+ until eos?
88
+ case
89
+ when scan(OBJECT_OPEN)
90
+ obj and raise ParserError, "source '#{peek(20)}' not in PSON!"
91
+ @current_nesting = 1
92
+ obj = parse_object
93
+ when scan(ARRAY_OPEN)
94
+ obj and raise ParserError, "source '#{peek(20)}' not in PSON!"
95
+ @current_nesting = 1
96
+ obj = parse_array
97
+ when skip(IGNORE)
98
+ ;
99
+ else
100
+ raise ParserError, "source '#{peek(20)}' not in PSON!"
101
+ end
102
+ end
103
+ obj or raise ParserError, "source did not contain any PSON!"
104
+ obj
105
+ end
106
+
107
+ private
108
+
109
+ def convert_encoding(source)
110
+ if source.respond_to?(:to_str)
111
+ source = source.to_str
112
+ else
113
+ raise TypeError, "#{source.inspect} is not like a string"
114
+ end
115
+ if supports_encodings?(source)
116
+ if source.encoding == ::Encoding::ASCII_8BIT
117
+ b = source[0, 4].bytes.to_a
118
+ source =
119
+ case
120
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
121
+ source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
122
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
123
+ source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
124
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
125
+ source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
126
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
127
+ source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
128
+ else
129
+ source.dup
130
+ end
131
+ else
132
+ source = source.encode(::Encoding::UTF_8)
133
+ end
134
+ source.force_encoding(::Encoding::ASCII_8BIT)
135
+ else
136
+ b = source
137
+ source =
138
+ case
139
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
140
+ PSON.encode('utf-8', 'utf-32be', b)
141
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
142
+ PSON.encode('utf-8', 'utf-16be', b)
143
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
144
+ PSON.encode('utf-8', 'utf-32le', b)
145
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
146
+ PSON.encode('utf-8', 'utf-16le', b)
147
+ else
148
+ b
149
+ end
150
+ end
151
+ source
152
+ end
153
+
154
+ def supports_encodings?(string)
155
+ # Some modules, such as REXML on 1.8.7 (see #22804) can actually create
156
+ # a top-level Encoding constant when they are misused. Therefore
157
+ # checking for just that constant is not enough, so we'll be a bit more
158
+ # robust about if we can actually support encoding transformations.
159
+ string.respond_to?(:encoding) && defined?(::Encoding)
160
+ end
161
+
162
+ # Unescape characters in strings.
163
+ UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
164
+
165
+ UNESCAPE_MAP.update(
166
+ {
167
+ ?" => '"',
168
+ ?\\ => '\\',
169
+ ?/ => '/',
170
+ ?b => "\b",
171
+ ?f => "\f",
172
+ ?n => "\n",
173
+ ?r => "\r",
174
+ ?t => "\t",
175
+ ?u => nil,
176
+
177
+ })
178
+
179
+ def parse_string
180
+ if scan(STRING)
181
+ return '' if self[1].empty?
182
+ string = self[1].gsub(%r{(?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff])}n) do |c|
183
+ if u = UNESCAPE_MAP[$&[1]]
184
+ u
185
+ else # \uXXXX
186
+ bytes = ''
187
+ i = 0
188
+ while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
189
+ bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
190
+ i += 1
191
+ end
192
+ PSON.encode('utf-8', 'utf-16be', bytes)
193
+ end
194
+ end
195
+ string.force_encoding(Encoding::UTF_8) if string.respond_to?(:force_encoding)
196
+ string
197
+ else
198
+ UNPARSED
199
+ end
200
+ rescue => e
201
+ raise GeneratorError, "Caught #{e.class}: #{e}", e.backtrace
202
+ end
203
+
204
+ def parse_value
205
+ case
206
+ when scan(FLOAT)
207
+ Float(self[1])
208
+ when scan(INTEGER)
209
+ Integer(self[1])
210
+ when scan(TRUE)
211
+ true
212
+ when scan(FALSE)
213
+ false
214
+ when scan(NULL)
215
+ nil
216
+ when (string = parse_string) != UNPARSED
217
+ string
218
+ when scan(ARRAY_OPEN)
219
+ @current_nesting += 1
220
+ ary = parse_array
221
+ @current_nesting -= 1
222
+ ary
223
+ when scan(OBJECT_OPEN)
224
+ @current_nesting += 1
225
+ obj = parse_object
226
+ @current_nesting -= 1
227
+ obj
228
+ when @allow_nan && scan(NAN)
229
+ NaN
230
+ when @allow_nan && scan(INFINITY)
231
+ Infinity
232
+ when @allow_nan && scan(MINUS_INFINITY)
233
+ MinusInfinity
234
+ else
235
+ UNPARSED
236
+ end
237
+ end
238
+
239
+ def parse_array
240
+ raise NestingError, "nesting of #@current_nesting is too deep" if
241
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
242
+ result = @array_class.new
243
+ delim = false
244
+ until eos?
245
+ case
246
+ when (value = parse_value) != UNPARSED
247
+ delim = false
248
+ result << value
249
+ skip(IGNORE)
250
+ if scan(COLLECTION_DELIMITER)
251
+ delim = true
252
+ elsif match?(ARRAY_CLOSE)
253
+ ;
254
+ else
255
+ raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
256
+ end
257
+ when scan(ARRAY_CLOSE)
258
+ raise ParserError, "expected next element in array at '#{peek(20)}'!" if delim
259
+ break
260
+ when skip(IGNORE)
261
+ ;
262
+ else
263
+ raise ParserError, "unexpected token in array at '#{peek(20)}'!"
264
+ end
265
+ end
266
+ result
267
+ end
268
+
269
+ def parse_object
270
+ raise NestingError, "nesting of #@current_nesting is too deep" if
271
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
272
+ result = @object_class.new
273
+ delim = false
274
+ until eos?
275
+ case
276
+ when (string = parse_string) != UNPARSED
277
+ skip(IGNORE)
278
+ raise ParserError, "expected ':' in object at '#{peek(20)}'!" unless scan(PAIR_DELIMITER)
279
+ skip(IGNORE)
280
+ unless (value = parse_value).equal? UNPARSED
281
+ result[string] = value
282
+ delim = false
283
+ skip(IGNORE)
284
+ if scan(COLLECTION_DELIMITER)
285
+ delim = true
286
+ elsif match?(OBJECT_CLOSE)
287
+ ;
288
+ else
289
+ raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
290
+ end
291
+ else
292
+ raise ParserError, "expected value in object at '#{peek(20)}'!"
293
+ end
294
+ when scan(OBJECT_CLOSE)
295
+ raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!" if delim
296
+ break
297
+ when skip(IGNORE)
298
+ ;
299
+ else
300
+ raise ParserError, "unexpected token in object at '#{peek(20)}'!"
301
+ end
302
+ end
303
+ result
304
+ end
305
+ end
306
+ end
307
+ end