Class: Ruber::PluginSpecificationReader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruber/plugin_specification_reader.rb

Defined Under Namespace

Classes: Option

Constant Summary

LICENSES =

A list of valid licences

[:unknown, :gpl, :gpl2, :lgpl, :lgpl2, :bsd, :artistic, :qpl, :qpl1, :gpl3, :lgpl3]

Instance Method Summary (collapse)

Constructor Details

- (PluginSpecificationReader) initialize(info)

A new instance of PluginSpecificationReader



83
84
85
# File 'lib/ruber/plugin_specification_reader.rb', line 83

def initialize info
  @plugin_info = info
end

Instance Method Details

- (Object) get_maybe_array(hash, key, conversion = nil, required = false) (private)



140
141
142
143
144
# File 'lib/ruber/plugin_specification_reader.rb', line 140

def get_maybe_array hash, key, conversion = nil, required = false
  res = get_value( hash, key, [], required).to_array
  res = res.map{|i| i.send conversion} if conversion
  res
end

- (Object) get_value(hash, key, default, required = false) (private)



130
131
132
133
134
135
136
137
138
# File 'lib/ruber/plugin_specification_reader.rb', line 130

def get_value hash, key, default, required = false
  hash.fetch(key) do |k|
    if required
      hash.fetch(key.to_s){raise PluginSpecification::PSFError, 
                          "The required '#{key}' entry is missing from the PDF"}
    else hash.fetch(key.to_s, default)
    end
  end
end

- (Boolean) has_key?(hash, key) (private)

Returns:

  • (Boolean)


126
127
128
# File 'lib/ruber/plugin_specification_reader.rb', line 126

def has_key? hash, key
  hash.has_key?(key) or hash.has_key?(key.to_sym)
end

- (Object) pixmap_file(pixmap) (private)

Finds the absolute file for the pixmap file pixmap. In particular, if a file called pixmap exists in the plugin directory, the full path of that file is returned. If such a file doesn’t exist, then KDE::IconLoader is used to find the path. If KDE::IconLoader also fails, an empty string is returned.

If the application hasn’t been created yet, this method always returns the absolute path of the file as if it were in the plugin directory (this happens because KDE::IconLoader can’t be used if the application hasn’t been created)



355
356
357
358
359
360
361
362
363
# File 'lib/ruber/plugin_specification_reader.rb', line 355

def pixmap_file pixmap
  if pixmap
    pix_file = File.join( @plugin_info.directory || Dir.pwd, pixmap)
    if File.exist?( pix_file ) or !KDE::Application.instance then pix_file
    else KDE::IconLoader.pixmap_path(pixmap) || ''
    end
  else ''
  end
end

- (Object) process_pdf(hash)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruber/plugin_specification_reader.rb', line 87

def process_pdf hash
  @plugin_info.name = read_name hash
  @plugin_info.about = read_about hash
  @plugin_info.version = read_version hash
  @plugin_info.required = read_required hash
  @plugin_info.required.each do |f| 
    file = File.join @plugin_info.directory, f
    if file.end_with?('.rb') then load file
    else require file
    end
  end
  @plugin_info.class_obj = read_class hash
  @plugin_info.features = read_features hash
  @plugin_info.deps = read_deps hash
  @plugin_info.runtime_deps = read_runtime_deps hash
  @plugin_info.ui_file = read_ui_file hash
  @plugin_info.tool_widgets = read_tool_widgets hash
  @plugin_info.config_widgets = read_config_widgets hash
  @plugin_info.config_options = read_config_options hash
  @plugin_info.project_options = read_project_options hash
  @plugin_info.project_widgets = read_project_widgets hash
  @plugin_info.extensions = read_extensions hash
  @plugin_info.actions = read_actions hash
  @plugin_info
end

- (Object) process_pdf_intro(hash, component = false)



113
114
115
116
117
118
119
120
121
122
# File 'lib/ruber/plugin_specification_reader.rb', line 113

def process_pdf_intro hash, component = false
  @plugin_info.name = read_name hash, component
  @plugin_info.about = read_about hash
  @plugin_info.version = read_version hash
  @plugin_info.required = read_required hash
  @plugin_info.features = read_features hash
  @plugin_info.deps = read_deps hash
  @plugin_info.runtime_deps = read_runtime_deps hash
  @plugin_info
end

- (Object) read_about(hash) (private)



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/ruber/plugin_specification_reader.rb', line 425

def read_about hash
  res = {}
  hash = get_value hash, :about, {}
  res[:human_name] = read_human_name hash
  res[:authors] = read_authors hash
  res[:license] = read_license hash
  res[:bug_address] = read_bug_address hash
  res[:copyright] = read_copyright hash
  res[:homepage] = read_homepage hash
  res[:description] = read_description hash
  res[:icon] = read_icon hash
  # Sometimes, an exception is raised because sometimes, when loading plugins,
  # I get an exception where OpenStruct complains because of a nil key. It
  # happens randomly, however. Let's see whether this removes the error and
  # makes clearer why it's happening
  if res.has_key? nil
    deleted_value = res.delete nil
  msg = <<-EOS 
This is information for Ruber developers only. Unless you aren't one of them, you can close and ignore this message box.

One of the entries of the about hash in the PSF was nil. The corresponding value was: #{deleted_value.inspect}"
    EOS
    KDE::MessageBox.information nil, deleted_value.inspect
  end
  begin OpenStruct.new res
  rescue Exception
    puts "The following exception occurred while reading the About data for #{@plugin_info.plugin_name}.\nThe contents of the hash was: #{res.inspect}"
    raise
  end
end

- (Object) read_action(name, hash) (private)



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ruber/plugin_specification_reader.rb', line 319

def read_action name, hash
  res = {}
  res[:name] = name
  unless res[:name]
    raise PluginSpecification::PSFError, "The required 'name' entry is missing from the PDF" 
  end
  res[:text] = get_value hash, :text, ''
  short = get_value hash, :shortcut, nil
  res[:shortcut] = short ? KDE::Shortcut.new(short) : nil
  res[:help] = get_value hash, :help, ''
  res[:icon] = pixmap_file get_value(hash, :icon, nil)
  cls = get_value(hash, :class, nil)
  std_action = get_value hash, :standard_action, nil
  if !cls and std_action
    res[:standard_action] = std_action.to_sym
  else res[:action_class] = constant(cls || 'KDE::Action')
  end
#       res[:delayed] = get_value hash, :delayed, false
  res[:receiver] = get_value hash, :receiver, 'self'
  res[:signal] = get_value hash, :signal, 'triggered(bool)'
  res[:slot] = get_value hash, :slot, nil
  res[:states] = get_value hash, :states, []
  res[:state] = get_value hash, :state, nil if res[:states].empty?
  OpenStruct.new res
end

- (Object) read_actions(hash) (private)



309
310
311
312
313
314
315
316
317
# File 'lib/ruber/plugin_specification_reader.rb', line 309

def read_actions hash
  res = {}
  hash = get_value(hash, :actions, {})
  hash.each_pair do |name, data|
    name = name.to_s
    res[name] = read_action name.to_s, data
  end
  res
end

- (Object) read_authors(hash) (private)



370
371
372
373
374
375
376
377
378
# File 'lib/ruber/plugin_specification_reader.rb', line 370

def read_authors hash
  res = get_value hash, :authors, []
  res = if res.is_a? Array and (res.empty? or res[0].is_a? Array) then res
  elsif res.is_a? Array then [res]
  else raise PluginSpecification::PSFError, 'The "authors" entry in the PDF should be an array'
  end
  res.each{|a| a << '' if a.size == 1}
  res
end

- (Object) read_bug_address(hash) (private)



392
393
394
# File 'lib/ruber/plugin_specification_reader.rb', line 392

def read_bug_address hash
  get_value hash, :bug_address, ''
end

- (Object) read_class(hash, component = false) (private)



158
159
160
161
# File 'lib/ruber/plugin_specification_reader.rb', line 158

def read_class hash, component = false
  res = get_value(hash, :class, 'Ruber::Plugin')
  res ? constant(res.to_s) : nil
end

- (Object) read_config_options(hash) (private)



236
237
238
239
240
241
242
243
244
# File 'lib/ruber/plugin_specification_reader.rb', line 236

def read_config_options hash
  hash = get_value(hash, :config_options, {})
  hash.inject({}) do |res, i|
    g, h = i
    g = g.to_sym
    h.each_pair{ |n, o| res[[g, n.to_sym]] = read_option g, n.to_sym, o}
    res
  end
end

- (Object) read_config_widgets(hash) (private)



229
230
231
232
233
# File 'lib/ruber/plugin_specification_reader.rb', line 229

def read_config_widgets hash
  res = get_value(hash, :config_widgets, [])
  res = res.to_array
  res.map{|h| read_widget h, [:caption]}
end


396
397
398
# File 'lib/ruber/plugin_specification_reader.rb', line 396

def read_copyright hash
  get_value hash, :copyright, ''
end

- (Object) read_deps(hash) (private)



177
178
179
# File 'lib/ruber/plugin_specification_reader.rb', line 177

def read_deps hash
  get_maybe_array hash, :deps, :to_sym
end

- (Object) read_description(hash) (private)



153
154
155
# File 'lib/ruber/plugin_specification_reader.rb', line 153

def read_description hash
  get_value(hash, :description, '').to_s
end

- (Object) read_extension(name, data) (private)



298
299
300
301
302
303
304
305
306
307
# File 'lib/ruber/plugin_specification_reader.rb', line 298

def read_extension name, data
  if data.is_a? Array
    data.inject([]){|res, i| res << read_extension(name, i)}
  else
    res = {:name => name}
    res[:class_obj] = eval get_value(data, :class, nil, true)
    res.merge! read_rules(data)
    OpenStruct.new res
  end
end

- (Object) read_extensions(hash) (private)



287
288
289
290
291
292
293
294
295
296
# File 'lib/ruber/plugin_specification_reader.rb', line 287

def read_extensions hash
  hash = get_value(hash, :extensions, {})
  hash.inject({}) do |res, i|
    name, h= i[0].to_sym, i[1]
    ext = read_extension name, h
#         ext.scope = Array(get_value(h, :scope, [:global, :document])).map{|i| i.to_sym}
    res[name] = ext
    res
  end
end

- (Object) read_features(hash) (private)



169
170
171
172
173
174
# File 'lib/ruber/plugin_specification_reader.rb', line 169

def read_features hash
  res = get_maybe_array hash, :features, :to_sym
  name = get_value(hash, :name, nil)
  res.unshift name.to_sym if name
  res
end

- (Object) read_homepage(hash) (private)



400
401
402
403
404
405
406
# File 'lib/ruber/plugin_specification_reader.rb', line 400

def read_homepage hash
  res = get_value hash, :homepage, ''
  unless res.empty? or res.match(%r{^http://})
    res = 'http://'+res
  end
  res
end

- (Object) read_human_name(hash) (private)



365
366
367
368
# File 'lib/ruber/plugin_specification_reader.rb', line 365

def read_human_name hash
  res = get_value hash, :human_name, nil
  res || @plugin_info.name.to_s.sub('_', ' ').capitalize
end

- (Object) read_icon(hash) (private)



408
409
410
411
# File 'lib/ruber/plugin_specification_reader.rb', line 408

def read_icon hash
  res = get_value(hash, :icon, nil)
  res ? pixmap_file(res) : ''
end

- (Object) read_license(hash) (private)



380
381
382
383
384
385
386
# File 'lib/ruber/plugin_specification_reader.rb', line 380

def read_license hash
  res = get_value hash, :license, :unknown
  if LICENSES.include? res.to_sym then res.to_sym
  elsif res.is_a? String then res
  else raise PluginSpecification::PSFError, "Invalid licese type :#{res}" 
  end
end

- (Object) read_name(hash, component = false) (private)



147
148
149
150
# File 'lib/ruber/plugin_specification_reader.rb', line 147

def read_name hash, component = false
  res = get_value(hash, :name, nil, !component)
  res ? res.to_sym : res
end

- (Object) read_option(group, name, hash) (private)



263
264
265
266
267
268
269
270
271
272
# File 'lib/ruber/plugin_specification_reader.rb', line 263

def read_option group, name, hash
  res = {:name => name.to_sym, :group => group.to_sym}
  default = get_value(hash, :default, nil)
  res[:relative_path] = get_value(hash, :relative_path, false)
  res[:eval_default] = get_value(hash, :eval_default, true)
  res[:default] = get_value(hash, :default, '')
  res[:order] = get_value(hash, :order, nil)
  d res if res.keys.any?{|k| k.nil?}
  Option.new res
end

- (Object) read_project_options(hash) (private)



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ruber/plugin_specification_reader.rb', line 247

def read_project_options hash
  hash = get_value(hash, :project_options, {})
  hash.inject({}) do |res, i|
    g, h = i
    g = g.to_sym
    h.each_pair do |n, o| 
      op = read_option g, n.to_sym, o
      rules = read_rules o
      op = Option.new op.instance_variable_get(:@table).merge(rules)
      op.type = get_value(o, :type, :global).to_sym
      res[[g, n.to_sym]] = op
    end
    res
  end
end

- (Object) read_project_widgets(hash) (private)



275
276
277
278
279
280
281
282
283
284
# File 'lib/ruber/plugin_specification_reader.rb', line 275

def read_project_widgets hash
  res = get_value(hash, :project_widgets, []).to_array
  res = res.map do |h| 
    w = read_widget h, [:caption]
    rules = read_rules h
    w = Option.new w.instance_variable_get(:@table).merge(rules)
    w
  end
  res
end

- (Object) read_required(hash) (private)



164
165
166
# File 'lib/ruber/plugin_specification_reader.rb', line 164

def read_required hash
  get_maybe_array hash, :require, :to_s
end

- (Object) read_rules(hash) (private)



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/ruber/plugin_specification_reader.rb', line 413

def read_rules hash
  res = {}
  scope = Array(get_value hash, :scope, [:global]).map &:to_sym
  scope = [:global, :document] if scope == [:all]
  res[:scope] = scope
  place = Array(get_value hash, :place, [:local]).map &:to_sym
  res[:place] = (place.include?(:all) ? [:local, :remote] : place)
  res[:mimetype] = Array(get_value(hash, :mimetype, []))
  res[:file_extension] = Array(get_value(hash, :file_extension, []))
  res
end

- (Object) read_runtime_deps(hash) (private)



182
183
184
# File 'lib/ruber/plugin_specification_reader.rb', line 182

def read_runtime_deps hash
  get_maybe_array hash, :runtime_deps, :to_sym
end

- (Object) read_tool_widgets(hash) (private)



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ruber/plugin_specification_reader.rb', line 216

def read_tool_widgets hash
  res = get_value(hash, :tool_widgets, [])
  res.to_array.map do |a|
    o = read_widget a, [:pixmap, :caption]
    o.position = get_value(a, :position, :bottom).to_sym
    o.name = get_value(a, :name, o.caption).to_s
    o.var_name = get_value(a, :var_name, 'widget')
    o.var_name = o.var_name.to_s if o.var_name
    o
  end
end

- (Object) read_ui_file(hash) (private)



187
188
189
# File 'lib/ruber/plugin_specification_reader.rb', line 187

def read_ui_file hash
  get_value( hash, :ui_file, '').to_s
end

- (Object) read_version(hash) (private)



388
389
390
# File 'lib/ruber/plugin_specification_reader.rb', line 388

def read_version hash
  get_value hash, :version, '0.0.0'
end

- (Object) read_widget(hash, required = []) (private)



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ruber/plugin_specification_reader.rb', line 192

def read_widget hash, required = []
  res = {}
  res[:caption] = get_value(hash, :caption, nil)
  pixmap = get_value(hash, :pixmap, @plugin_info.about.icon)
  if required.include? :pixmap and pixmap.empty?
    raise PluginSpecification::PSFError, "The :pixmap entry must be present in the widget description"
  end
  res[:pixmap] = pixmap_file pixmap
  cls = get_value(hash, :class, nil)
  res[:class_obj] = cls ? constant(cls) : nil
  res[:code] = get_value(hash, :code, nil)
  res[:required] = get_maybe_array hash, :required
  raise PluginSpecification::PSFError, "A widget description can't contain both the :class and "\
      "the :code entries" if res[:class_obj] and res[:code]
  raise PluginSpecification::PSFError, "Either the :class or the :code entry must be present in "\
      "the widget description" unless res[:class_obj] or res[:code]
  if required.include? :caption and !res[:caption]
    raise PluginSpecification::PSFError, "The :caption entry must be present in the widget description"
  elsif !res[:caption] then res[:caption] = ''
  end
  OpenStruct.new(res)
end