Class: RXFHelper

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

Overview

Read XML File Helper

Class Method Summary collapse

Class Method Details

.absolute_url(page_url, item_location) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/rxfhelper.rb', line 561

def self.absolute_url(page_url, item_location)

  case item_location

    when /^\//
      URL.join page_url[/https?:\/\/[^\/]+/], item_location

    when /^http/
      item_location

    else
      File.join page_url[/.*\//], item_location
  end
end

.call(s) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rxfhelper.rb', line 150

def self.call(s)

  if s =~ /=/ then

    uri, val = s.split(/=/)
    self.set uri, val

  else

    self.get s

  end

end

.chdir(x) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rxfhelper.rb', line 204

def self.chdir(x)

  # We can use @fs within chdir() to flag the current file system.
  # Allowing us to use relative paths with FileX operations instead
  # of explicitly stating the path each time. e.g. touch 'foo.txt'
  #

  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then

    @fs = :local
    FileUtils.chdir x

  elsif x[/^dfs:\/\//]

    host = x[/(?<=dfs:\/\/)[^\/]+/]
    @fs = 'dfs://' + host
    DfsFile.chdir x

  end

end

.chmod(permissions, s) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rxfhelper.rb', line 165

def self.chmod(permissions, s)

  return unless permissions.is_a? Integer
  return unless s.is_a? String

  if s[/^dfs:\/\//] or @fs[0..2] == 'dfs' then
    DfsFile.chmod permissions, s
  else
    FileUtils.chmod permissions, s
  end

end

.cp(s1, s2, debug: false) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rxfhelper.rb', line 178

def self.cp(s1, s2, debug: false)

  puts 'inside RXFHelper.cp' if debug

  found = [s1,s2].grep /^\w+:\/\//
  puts 'found: ' + found.inspect if debug

  if found.any? then

    case found.first[/^\w+(?=:\/\/)/]

    when 'dfs'
      DfsFile.cp(s1, s2)
    when 'ftp'
      MyMediaFTP.cp s1, s2
    else

    end

  else

    FileUtils.cp s1, s2

  end
end

.get(x) ⇒ Object

Raises:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rxfhelper.rb', line 226

def self.get(x)

  raise RXFHelperException, 'nil found, expected a string' if x.nil?

  if x[/^rse:\/\//] then

    RSC.new.get x

  elsif x[/^reg:\/\//] then

    r = DRbRegClient.new.get(x)
    r.is_a?(Rexle::Element::Value) ? r.to_s : r

  else
    [x, :unknown]
  end

end

.glob(s) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/rxfhelper.rb', line 245

def self.glob(s)

  if s[/^dfs:\/\//] then
    DfsFile.glob(s)
  else
    Dir.glob(s)
  end

end

.ls(x = '*') ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rxfhelper.rb', line 255

def self.ls(x='*')

  return Dir[x] if File.exists?(File.dirname(x))

  case x[/^\w+(?=:\/\/)/]
  when 'file'
    Dir[x]
  when 'dfs'
    DfsFile.ls x
  when 'ftp'
    MyMediaFTP.ls x
  else

  end

end

.mkdir(x) ⇒ Object



272
273
274
275
276
277
278
279
280
# File 'lib/rxfhelper.rb', line 272

def self.mkdir(x)

  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then
    FileUtils.mkdir x
  elsif x[/^dfs:\/\//]
    DfsFile.mkdir x
  end

end

.mkdir_p(x) ⇒ Object



282
283
284
285
286
287
288
289
290
# File 'lib/rxfhelper.rb', line 282

def self.mkdir_p(x)

  if x[/^dfs:\/\//] or @fs[0..2] == 'dfs' then
    DfsFile.mkdir_p x
  else
    FileUtils.mkdir_p x
  end

end

.mv(s1, s2) ⇒ Object



292
293
294
# File 'lib/rxfhelper.rb', line 292

def self.mv(s1, s2)
  DfsFile.mv(s1, s2)
end

.objectize(contents) ⇒ Object

used by self.read



298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/rxfhelper.rb', line 298

def self.objectize(contents)

  doctype = contents.lines.first[/(?<=^<\?)\w+/]
  reg = RemoteDwsRegistry.new domain: 'reg.lookup', port: '9292'
  r = reg.get_key 'hkey_gems/doctype/' + doctype

  return contents unless r

  require r.text('require')

  obj = Object.const_get(r.text('class')).new
  obj.import contents
  obj
end

.post(uri, x = nil) ⇒ Object

Raises:



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/rxfhelper.rb', line 576

def self.post(uri, x=nil)

  raise RXFHelperException, 'nil found, expected a string' if uri.nil?

  if uri[/^rse:\/\//] then
    RSC.new.post uri, x

  elsif uri[/^reg:\/\//]

    DRbRegClient.new.set(uri, x)
  else
    [uri, :unknown]
  end

end

.pwdObject



313
314
315
316
317
# File 'lib/rxfhelper.rb', line 313

def self.pwd()

  DfsFile.pwd

end

.read(x, h = {}) ⇒ Object

Raises:



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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/rxfhelper.rb', line 319

def self.read(x, h={})

  opt = {debug: false, auto: false}.merge(h)

  debug = opt[:debug]

  puts 'x: ' + x.inspect if opt[:debug]
  raise RXFHelperException, 'nil found, expected a string' if x.nil?

  if x.class.to_s =~ /Rexle$/ then

    [x.xml, :rexle]

  elsif x.strip[/^<(\?xml|[^\?])/] then

    [x, :xml]

  elsif x.lines.length == 1 then

    puts 'x.lines == 1'.info if debug

    if x[/^https?:\/\//] then

      puts 'before GPDRequest'.info if debug

      r = if opt[:username] and opt[:password] then
        GPDRequest.new(opt[:username], opt[:password]).get(x)
      else
        response = RestClient.get(x)
      end

      case r.code
      when '404'
        raise(RXFHelperException, "404 %s not found" % x)
      when '401'
        raise(RXFHelperException, "401 %s unauthorized access" % x)
      end

      obj = opt[:auto] ? objectize(r.body) :   r.body

      [obj, :url]

    elsif  x[/^dfs:\/\//] then

      r = DfsFile.read(x)
      [opt[:auto] ? objectize(r) : r, :dfs]

    elsif  x[/^ftp:\/\//] then

      [MyMediaFTP.read(x), :ftp]

    elsif x[/^rse:\/\//] then

       [RSC.new.get(x), :rse]

    elsif x[/^reg:\/\//] then

      r = DRbRegClient.new.get(x)
      [r.is_a?(Rexle::Element::Value) ? r.to_s : r, :reg]

    elsif x[/^file:\/\//] or File.exists?(x) then

      puts 'RXFHelper.read before File.read' if opt[:debug]
      contents = File.read(File.expand_path(x.sub(%r{^file://}, '')))

      puts 'contents2: ' + contents.inspect if opt[:debug]

      puts 'opt: ' + opt.inspect if opt[:debug]

      obj = opt[:auto] ? objectize(contents) :   contents

      [obj, :file]

    elsif x =~ /\s/
      [x, :text]
    elsif DfsFile.exists?(x)
      [DfsFile.read(x), :dfs]
    else
      [x, :unknown]
    end

  else

    [x, :unknown]
  end
end

.rm(filename) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/rxfhelper.rb', line 406

def self.rm(filename)

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.rm filename
  when 'ftp'
    MyMediaFTP.rm filename
  else

    if File.basename(filename) =~ /\*/ then

      Dir.glob(filename).each do |file|

        begin
          FileUtils.rm file
        rescue
          puts ('RXFHelper#rm: ' + file + ' is a Directory').warning
        end

      end

    else
      FileUtils.rm filename
    end

  end

end

.rm_r(filename, force: false) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/rxfhelper.rb', line 435

def self.rm_r(filename, force: false)

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.rm_r filename, force: force
  #when 'ftp'
  #  MyMediaFTP.rm filename
  else

    if File.basename(filename) =~ /\*/ then

      Dir.glob(filename).each do |file|

        begin
          FileUtils.rm_r file, force: force
        rescue
          puts ('RXFHelper#rm: ' + file + ' is a Directory').warning
        end

      end

    else
      FileUtils.rm_r filename, force: force
    end

  end

end

.ru(path = '.') ⇒ Object

recently_updated



466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/rxfhelper.rb', line 466

def self.ru(path='.')

  case path[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.ru path

  else

    DirToXML.new(path, recursive: false, verbose: false).latest

  end

end

.ru_r(path = '.') ⇒ Object

recently updated recursively check directories



482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/rxfhelper.rb', line 482

def self.ru_r(path='.')

  case path[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.ru_r path

  else

    DirToXML.new(path, recursive: true, verbose: false).latest

  end

end

.set(uri, x = nil) ⇒ Object

Raises:



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/rxfhelper.rb', line 592

def self.set(uri, x=nil)

  raise RXFHelperException, 'nil found, expected a string' if uri.nil?
  puts 'uri: ' + uri.inspect

  if uri[/^rse:\/\//] then
    RSC.new.post uri, x

  elsif uri[/^reg:\/\//]

    DRbRegClient.new.set(uri, x)
  else
    [uri, :unknown]
  end

end

.touch(filename, mtime: Time.now) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/rxfhelper.rb', line 496

def self.touch(filename, mtime: Time.now)

  if @fs[0..2] == 'dfs' then
    return DfsFile.touch(@fs + pwd + '/' + filename, mtime: mtime)
  end

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.touch filename, mtime: mtime
  #when 'ftp'
  #  MyMediaFTP.touch filename
  else
    FileUtils.touch filename, mtime: mtime
  end

end

.write(location, s = nil) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/rxfhelper.rb', line 513

def self.write(location, s=nil)

  case location
  when /^dfs:\/\//

    DfsFile.write location, s

  when  /^ftp:\/\// then

    MyMediaFTP.write location, s

  when /^rse:\/\//

    RSC.new.post(location, s)

  when /^reg:\/\//

    DRbRegClient.new.set(location, s)

  else

    if DfsFile.exists?(File.dirname(location)) then
      DfsFile.write location, s
    else
      File.write(location, s)
    end

  end

end

.writeable?(source) ⇒ Boolean

Returns:

  • (Boolean)


544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/rxfhelper.rb', line 544

def self.writeable?(source)

  return false if source.lines.length > 1

  if not source =~ /:\/\// then

    return true if File.exists? source

  else

    return true if source =~ /^dfs:/

  end

  return false
end

.zip(filename, a) ⇒ Object



609
610
611
# File 'lib/rxfhelper.rb', line 609

def self.zip(filename, a)
  DfsFile.zip(filename, a)
end