class FSObjects

This class concentrates all action on file-system-objects. It does not need to be instantiated and thus contains only static members.

Constants

CONFIG
FO
FOLONG
INI
LONG
MILLE
SECTIONS

TODO add patterns for other sections

Public Class Methods

complete(dir) click to toggle source

Returns an Array, describing the currently available configuration-files.

# File lib/fsobjects.rb, line 165
def self::complete(dir)
  if(!@@entries)
    read_config(dir)
  end

  items = []
  nend = CONFIG + INI
  flen1 = flen2 = 0
  @@entries.each do |n|
    sn = n[0..1]
    ln = LONG[sn]
    if(ln)
      item = []
      field = n.sub(nend, '')

      flen1 = field.length if field.length > flen1
      item << field
      field = ln.dup
      if (FO == n[2..3])
        field << ' ' << FOLONG
        if(n[4..5].to_i > 0)
          field << ' ' << MILLE + n[4..5]
        end
      elsif (n[2..3].to_i > 0 )
        field << ' ' << MILLE + n[2..3]
      end
      flen2 = field.length if field.length > flen2
      item << field
      item << n
      items << item
    end
  end
  items.collect!{|it| "%#{flen1 + 2}s\t%-#{flen2 + 2}s %s" %it}
end
copy_buttons(source, target, section, dir = nil) click to toggle source

The Original Function.


not really… but the ORIGINAL CAUSE (stand in Awe or something)!

Find new buttons in the 'source'-file and add those, not aready present in the 'target'-file, to the latter.

Beware that :button is the only possible section, here. The function needs to analyze sections based on button-specific field-values.

# File lib/fsobjects.rb, line 55
def self::copy_buttons(source, target, section, dir = nil)
        pt_next_symbol = /\[/
        pt_unique_field = /(Exepath)=(.*)/
        button_label = "[UserButton%i]"

        # handle short_names
        source, target = [source, target].collect do |ele|
                if(dir)
                        if(!ele.include?(CONFIG+INI))
                           ele = ini_for_short_name(ele, dir)
                           if !ele
                                   UserIO::msg_cannot_copy("No configuration file found for #{ele}. Check your options.")
                                   exit false
                           end
                           dir.dup << File::Separator << ele
                        else 
                                ele
                        end
                else
                        UserIO::msg_files_or_dir_for_sn
                        exit false
                end
        end
        begin
                contentSrc = File.read(source)
                contentTrg = File.read(target)
        rescue Exception => ex
                
                UserIO::msg_cannot_copy("File " << (contentSrc ? target : source) << " is unusable as " << (contentSrc ? 'target' : 'source') << "-file!", ex)
                exit false
        end

        maxSection = maxSectionNumber(contentTrg, SECTIONS[section])
        vacantNumbers = vacantSectionNumbers(contentTrg, SECTIONS[section], maxSection)
        maxSection ||= 0

        # collect all sought sections
        source_sections = get_sections(section, pt_next_symbol, contentSrc)
        target_sections = get_sections(section, pt_next_symbol, contentTrg)

        # compare old and new sections, avoid duplicate entries.
        originally = source_sections.length   
        source_sections.delete_if { |s| target_sections.detect {|ts| ts.include?(s.match(pt_unique_field)[0].to_s) }}
        now = source_sections.length
        if(now > 0)
                UserIO::msg_will_copy(source_sections, originally, now)
                uin = UserIO::wait_for_user
                loop do 
                        if(89 == uin || 121 == uin)
                                # Append new button-definitions to the target file.
                                # First use empty positions below the maximum section number.
                                File.open(target, "a") do |tf|
                                        source_sections.each do |s|
                                                tf << button_label %(vacantNumbers.empty? ? maxSection += 1 : vacantNumbers.delete(vacantNumbers.first)) << "\n"
                                                tf << s << "\n"
                                        end
                                end        
                                UserIO::msg_done
                                exit true
                        elsif(118 == uin || 86 == uin)
                                uin = UserIO::show_sections_prior_copy(source_sections)
                        else
                                UserIO::msg_doing_nothing
                                exit true
                        end
                end  
        else
                UserIO::msg_all_sections_eliminated
        end
end
get_sections(section, next_symbol, text) click to toggle source

Returns an array of section-bodies (without label). Parameters are:

    section - a pattern, describing the label of the section.
next_symbol - a symbol which follows the section, and thus signals its end.
       text - the content or fragment, which shall be scrutinized.
# File lib/fsobjects.rb, line 154
def self::get_sections(section, next_symbol, text)
  return text.split(SECTIONS[section]).collect do |s|
    if !s.empty?
      s = s.split(next_symbol)[0].strip
      s.length > 1 ? s: nil
    end
  end.compact
end
ini_for_short_name(sn, dir) click to toggle source
# File lib/fsobjects.rb, line 200
def self::ini_for_short_name(sn, dir)
  list = read_config(dir)
  nend = CONFIG + INI
  list.detect {|fn| fn == sn + nend}
end
maxSectionNumber(text, sectionPattern) click to toggle source

Find the highest button-number in the target-file if applicable

# File lib/fsobjects.rb, line 127
def self::maxSectionNumber(text, sectionPattern)
        text.lines.collect do |l|
                label = l.match(sectionPattern)
                if(label)
                        label[0].match(/\d{1,3}/)[0].to_i   
                end
        end.compact.max
end
read_config(dir) click to toggle source

Called by other static members of this class, read_config will just create the list of config-files in the given directory.

# File lib/fsobjects.rb, line 216
def self::read_config(dir)
  begin
    @@entries = Dir.entries(dir)
  rescue Exception => ex
    UserIO::msg_cannot_read_dir(dir, ex)
    exit false
  end
  @@entries.collect! {|fo| fo if fo.end_with?(CONFIG + INI) }.compact!
  if(@@entries.empty?)
    UserIO::msg_conf_dir_empty("Config-files look like this: *" << CONFIG << INI)
    exit false
  end
  @@entries
end
short_names(dir) click to toggle source

Returns an array of short application designators.

# File lib/fsobjects.rb, line 207
def self::short_names(dir)
  list = read_config(dir)
  nend = CONFIG + INI
  list.collect{|n| n.sub(nend, '') }
end
vacantSectionNumbers(text, sectionPattern, max) click to toggle source

Find and return a list of unallocated section numbers.

# File lib/fsobjects.rb, line 137
def self::vacantSectionNumbers(text, sectionPattern, max)
  sections = text.match(sectionPattern)
  if sections && sections.length > 0
    section = sections[0].match(/(\D*)\d*\]/)[1]
    (1..max).collect do |n|
      n if !text.include?(section + n.to_s + ']')
    end.compact
  else
    [] 
  end
end