Class: NumRu::GDir

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/gdir.rb,
lib/numru/htdir.rb

Defined Under Namespace

Classes: HtDir

Constant Summary collapse

@@top =

default

'/'
@@top_pat =
Regexp.new('^'+@@top)
@@text_pattern =
[/\.txt$/,/^\w*$/]
@@cwd =

current working directory

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GDir

Returns a new instance of GDir.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/numru/gdir.rb', line 388

def initialize(path)
  http = false
  if /^http/ =~ path
	http = true
	abspath = path.sub(/\/+$/,'')
	@path = @abspath = ( abspath + '/' )
  elsif /^http/ =~ @@top
	http = true
	abspath = @@top + path.sub(/\/+$/,'')
	abspath = File.expand_path( '/'+abspath ).sub(/^\//,'').sub(/^http:\//,'http://')  # to clean up the path
	@path = @abspath = ( abspath + '/' )
  else
	path = '/' + path if /^\// !~ path      # to always start with '/'
	path = File.expand_path( path )         # to clean up the path
	abspath = @@top.sub(/^(\/.*)\/$/,'\1')+path.sub(/\/+$/,'')
	@path = abspath.sub(@@top_pat,'') + '/'
	@abspath = abspath + '/'
  end
  ftype = __ftype(abspath)
  case ftype
  when "directory"
	if http
	  @dir = HtDir.new(abspath)
	else
	  @dir = Dir.new(abspath)
	end
	@file = nil
  when "file"
	if (file_class = GPhys::IO::file2file_class( abspath ))
	  # non-nil means GPhys knows it.
	  @dir = nil
	  @file = file_class.open( abspath )
	else
	  raise path+" are not dealt with GPhys. " +
     "Use GPhys::IO::add_(nc|gr)_pattern if it should be."
	end
  else
	raise ArgumentError, abspath+": Unsupported file type "+ftype
  end
end

Class Method Details

.add_text_pattern(*regexps) ⇒ Object



497
498
499
500
501
502
503
# File 'lib/numru/gdir.rb', line 497

def add_text_pattern(*regexps)
	regexps.each{ |regexp|
	  raise TypeError,"Regexp expected" unless Regexp===regexp
	  @@text_pattern.push(regexp)
	}
	nil
end

.cd(path) ⇒ Object Also known as: chdir, cwd=



469
470
471
472
473
474
475
476
477
478
# File 'lib/numru/gdir.rb', line 469

def cd(path)
	if /^\// =~ path
	  # absolute path
	  @@cwd = GDir.new( path )
	else
	  # relative path
	  set_cwd
	  @@cwd = GDir.new( @@cwd.path + path )
	end
end

.cwdObject

<< private //



460
461
462
463
# File 'lib/numru/gdir.rb', line 460

def cwd
	set_cwd
	@@cwd
end

.pwdObject



465
466
467
# File 'lib/numru/gdir.rb', line 465

def pwd
	cwd.path
end

.set_cwdObject

// private >>



454
455
456
# File 'lib/numru/gdir.rb', line 454

def set_cwd
	@@cwd = GDir.new('/') if !@@cwd
end

.set_text_pattern(*regexps) ⇒ Object



505
506
507
508
509
510
511
# File 'lib/numru/gdir.rb', line 505

def GDir.set_text_pattern(*regexps)
	regexps.each{ |regexp|
	  raise TypeError,"Regexp expected" unless Regexp===regexp
	}
	@@text_pattern = regexps
	nil
end

.topObject



449
450
451
# File 'lib/numru/gdir.rb', line 449

def top
	@@top
end

.top=(top) ⇒ Object Also known as: set_top



434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/numru/gdir.rb', line 434

def top=(top)
	if /^http:\/\// =~ top
	  top = top.sub(/\/+$/,'')
	else
	  top = File.expand_path( top )   # to absolute path
	end
  ### top.untaint
  @@top_pat =  Regexp.new('^'+top)
	@@top = top + '/'
	@@cwd = GDir.new('/') if @@cwd    # reset to the top if set
	@@top
end

Instance Method Details

#[](path) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/numru/gdir.rb', line 533

def [](path)
  begin
	begin
	  dir(path)
	rescue
	  begin
 data(path)
	  rescue
 text(path)
	  end
	end
  rescue
	pt = path || ''
	raise @path+pt+': no such data, directory or text (or is outside the tree)'
  end
end

#atimeObject



893
894
895
# File 'lib/numru/gdir.rb', line 893

def atime
  File.ctime(@abspath.sub(/\/$/,''))
end

#closeObject

INSTANCE METHODS ######



517
518
519
520
# File 'lib/numru/gdir.rb', line 517

def close
  @dir.close if @dir
  @file.close if @file
end

#ctimeObject



890
891
892
# File 'lib/numru/gdir.rb', line 890

def ctime
  File.ctime(@abspath.sub(/\/$/,''))
end

#data(path) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/numru/gdir.rb', line 563

def data(path)
  dirname = File.dirname(path)
  if dirname != '.'
	self.dir(dirname).data( File.basename(path) )
  else
	# path is for the current directory.
	if @dir
	  raise "data #{path} is not found"   # cannot be directly under a Dir
	else
	  GPhys::IO::open(@file,path)
	end
  end
end

#dir(path) ⇒ Object Also known as: cd



550
551
552
553
554
555
556
557
558
559
# File 'lib/numru/gdir.rb', line 550

def dir(path)
  case path
  when /^\//
	# absolute path
	GDir.new(path)
  else
	# relative path
	GDir.new(@path+path)
  end
end

#each_dataObject



830
831
832
833
834
835
836
837
# File 'lib/numru/gdir.rb', line 830

def each_data
  if @file
	GPhys::IO::var_names(@file).each do |vnm|
	  gphys = GPhys::IO::open(@file,vnm)
	  yield(gphys)
	end
  end
end

#each_dirObject



818
819
820
821
822
823
824
825
826
827
828
# File 'lib/numru/gdir.rb', line 818

def each_dir
  if @dir
	@dir.rewind
	@dir.each do |fnm|
	  if fnm != '.' && fnm != '..' && __can_be_a_gdir(@abspath+fnm)
 yield( self.dir(fnm) )
	  end
	end
	self
  end
end

#find_data(name = nil, long_name = nil, units = nil) ⇒ Object



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/numru/gdir.rb', line 637

def find_data(name=nil,long_name=nil,units=nil)
#      GC.start
  # name (Regexp (or String; partial match))
  # long_name (Regexp (or String; partial match))
  # units (Units or String)

  if !(name || long_name || units)
	raise ArgumentError,
	  "You must specify at least one of name, long_name, and units"
  end

  name = Regexp.new( name ) if name.is_a?(String)
  long_name = Regexp.new( long_name ) if long_name.is_a?(String)
  units = Units.new( units ) if units.is_a?(String)

  result = []

  if @file
	#p @file.path
	GPhys::IO::var_names(@file).each do |nm|
	  if !name || name =~ nm
 # name inspection passed
 gph = GPhys::IO::open(@file,nm) if long_name || units
 if !long_name || long_name =~ gph.long_name
   # long_name inspection passed
   if !units || units == gph.units
		# units inspection passed
		result.push( [path, nm] )
   end
 end
	  end

	end
  end

  if @dir
	dnames = []
	@dir.rewind
	@dir.each{|fnm|
	  if fnm != '.' && fnm != '..' && __can_be_a_gdir(@abspath+fnm)
 dnames.push( @path+fnm )
	  end
	}
	dnames.each do |nm| 
	  dir = GDir.new(nm)
	  result.push(*dir.find_data(name,long_name,units))
	  dir.close
	end
  end

  result
end

#find_dir(filt) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/numru/gdir.rb', line 610

def find_dir( filt )
  # filt is a Regexp or String
  filt = Regexp.new( filt ) if filt.is_a?(String)
  if ( filt =~ name )
	result = [ path ]
  else
	result = []
  end

  if @dir
	dnames = []
	@dir.rewind
	@dir.each{|fnm|
	  if fnm != '.' && fnm != '..' && __can_be_a_gdir(@abspath+fnm)
 dnames.push( @path+fnm )
	  end
	}
	dnames.each do |nm|
	  dir = GDir.new(nm)
	  result.push(*dir.find_dir(filt))
	  dir.close
	end
  end

  result
end

#inspectObject



529
530
531
# File 'lib/numru/gdir.rb', line 529

def inspect
  path
end

#list_all(path = nil) ⇒ Object



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/numru/gdir.rb', line 839

def list_all(path = nil)
  result = ''
  dirs = list_dirs(path)
  if dirs.length > 0
	result << "Directories:\n"
	dirs.each{|s| result << "  '#{s}/'\n"}
  end
  texts = list_texts(path)
  if texts.length > 0
	result << "Text files?:\n"
	texts.each{|s| result << "  '#{s}'\n"}
  end
  data = list_data(path)
  if data.length > 0
	result << "Data:\n"
	data.each{|s| result << "  '#{s}'\n"}
  end
  result
end

#list_all_v(path = nil) ⇒ Object



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/numru/gdir.rb', line 863

def list_all_v(path = nil)
  result = ''
  dirs = list_dirs_v(path)
  if dirs.length > 0
	result << "Directories:\n"
	dirs.each{|s| result << "  #{s}\n"}
  end
  texts = list_texts_v(path)
  if texts.length > 0
	result << "Text files?:\n"
	texts.each{|s| result << "  #{s}\n"}
  end
  data = list_data_v(path)
  if data.length > 0
	result << "Data:\n"
	data.each{|s| result << "  #{s}\n"}
  end
  result
end

#list_data(path = nil) ⇒ Object



735
736
737
738
739
740
741
742
743
744
745
# File 'lib/numru/gdir.rb', line 735

def list_data(path = nil)
  if path
	self.dir(path).list_data
  else
	if @file
	  GPhys::IO::var_names(@file)
	else
	  []
	end
  end
end

#list_data_v(path = nil) ⇒ Object



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/numru/gdir.rb', line 747

def list_data_v(path = nil)
  # verbose version
  if path
	self.dir(path).list_data_v
  else
	if @file
	  GPhys::IO::var_names(@file).collect do |nm|
 gph = GPhys::IO::open(@file,nm)
 axinfo = '['
 (0...gph.rank).collect do |i| 
   axinfo += gph.axis(i).name + '=' + gph.shape[i].to_s + ','
 end
 axinfo.sub!(/,$/,']')
 "#{nm}\t#{axinfo}\t'#{gph.long_name}'\t(#{gph.units.to_s})"
	  end
	else
	  []
	end
  end
end

#list_dirs(path = nil) ⇒ Object



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/numru/gdir.rb', line 690

def list_dirs(path = nil)
  if path
	self.dir(path).list_dirs
  else
	if @dir
	  gdir_names = []
	  @dir.rewind
	  @dir.each{|fnm|
 if fnm != '.' && fnm != '..' && __can_be_a_gdir(@abspath+fnm)
   gdir_names.push( fnm )
 end
	  }
	  gdir_names.sort
	else
	  []
	end
  end
end

#list_dirs_v(path = nil) ⇒ Object



709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/numru/gdir.rb', line 709

def list_dirs_v(path = nil)
  # verbose version
  if path
	self.dir(path).list_dirs_v
  else
	if @dir
	  if /^http:\/\// =~ @abspath
 return self.list_dirs
	  end
	  list = []
	  @dir.rewind
	  @dir.collect{|x| x}.sort.each{|fnm|
 if fnm != '.' && fnm != '..' && __can_be_a_gdir(@abspath+fnm)
   gdir = GDir.new(@path+fnm)
   list.push( sprintf("%10d  %s %s/",gdir.size.to_s,
                             gdir.mtime_like_ls_l, fnm) )
   gdir.close
 end
	  }
	  list
	else
	  []
	end
  end
end

#list_texts(path = nil) ⇒ Object



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'lib/numru/gdir.rb', line 768

def list_texts(path = nil)
  if path
	self.dir(path).list_texts
  else
	if @dir
	  text_files = []
	  @dir.rewind
	  @dir.each{|fnm|
 if __ftype(@abspath+fnm) == "file"
   case fnm
   when *@@text_pattern
		text_files.push( fnm )
   end
 end
	  }
	  text_files.sort
	else
	  []
	end
  end
end

#list_texts_v(path = nil) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/numru/gdir.rb', line 790

def list_texts_v(path = nil)
  # verbose version
  if path
	self.dir(path).list_texts_v
  else
	if @dir
	  if /^http:\/\// =~ @abspath
 return self.list_texts
	  end
	  list = []
	  @dir.rewind
	  @dir.collect{|x| x}.sort.each{|fnm|
 if __ftype(@abspath+fnm) == "file"
   case fnm
   when *@@text_pattern
		path = @abspath + fnm
		list.push( sprintf("%10d  %s  %s",File.size(path),
                               __fmt_like_ls_l(File.mtime(path)), fnm) )
   end
 end
	  }
	  list
	else
	  []
	end
  end
end

#ls(path = nil) ⇒ Object



859
860
861
# File 'lib/numru/gdir.rb', line 859

def ls(path = nil)
  print list_all(path)
end

#ls_l(path = nil) ⇒ Object



883
884
885
# File 'lib/numru/gdir.rb', line 883

def ls_l(path = nil)
  print list_all_v(path)
end

#mtimeObject



887
888
889
# File 'lib/numru/gdir.rb', line 887

def mtime
  File.mtime(@abspath.sub(/\/$/,''))
end

#mtime_like_ls_lObject



900
901
902
903
# File 'lib/numru/gdir.rb', line 900

def mtime_like_ls_l
  # format mtime like ls -l
  __fmt_like_ls_l( self.mtime )
end

#nameObject



525
526
527
# File 'lib/numru/gdir.rb', line 525

def name
  @path=='/' ? @path : File.basename( @path.sub(/\/$/,'') )
end

#open_all_dataObject



600
601
602
603
604
605
606
607
608
# File 'lib/numru/gdir.rb', line 600

def open_all_data
  hash = Hash.new
  if @file
	GPhys::IO::var_names(@file).each do |nm|
	  hash[nm.to_sym] = data(nm)
	end
  end
  hash
end

#pathObject



522
523
524
# File 'lib/numru/gdir.rb', line 522

def path
  @path
end

#sizeObject



896
897
898
# File 'lib/numru/gdir.rb', line 896

def size
  File.size(@abspath.sub(/\/$/,''))
end

#text(path) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/numru/gdir.rb', line 577

def text(path)
  dirname = File.dirname(path)
  if dirname != '.'
	self.dir(dirname).text( File.basename(path) )
  else
	# path is for the current directory.
	if @dir
	  if __ftype(@abspath+path) == "file"
 case path
 when *@@text_pattern
   File.open(@abspath+path)
 else
   raise "#{path} does not match patterns to find text files"
 end
	  else
 raise "text #{path} is not a plain file"
	  end
	else
	  raise "text #{path} is not found"
	end
  end
end