Class: CLConfig

Inherits:
Object
  • Object
show all
Defined in:
ext/lib/CompLearnLib/CLConfig.rb

Constant Summary collapse

@@BASECONFIGVARS =

The allowed config variables

[     #  prettyName     isArray   Type    Default Value
      [ 'Compressor'    , false,  String,     FoundComp.defaultCompressor() ],
      [ 'CompressorCommand'   , false,  String,     'gzip -c -' ],
      [ 'InputDir'      , false,  String,     'in'        ],
      [ 'OutputDir'     , false,  String,     'out'       ],
      [ 'WorkDir'       , false,  String,     'work'      ],
      [ 'Symmetric'     , false,  TrueClass,   true       ],
      [ 'Hosts'         , true,   String,  ['localhost']  ],
      [ 'SingleProcess' , false,  TrueClass,   true      ],
# maketree
      [ 'UnpairedPenalty',false, Float, 0.0 ],
      [ 'UseBestThirdOnly', false, TrueClass, false ],
      [ 'MaxFailedTries', false, Integer, 100 ],
      [ 'TreesPerTry', false, Integer, 1000 ],
      [ 'InternalNodePrefix', false, String, 'n' ],
]
@@CONFIGVARS =
{ }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ CLConfig

constructor, read file



92
93
94
95
96
97
98
99
100
101
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 92

def initialize(filename)
    @@CONFIGVARS.each_value { |a|
      methodName, defVal = a[4], a[2]
      instance_eval("@#{methodName} = defVal")
    }
    if filename
      @filename = filename
      readCLConfig()
    end
end

Class Method Details

.getDefaultConfigObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 59

def self.getDefaultConfig
  unless defined?(@@DEFCON)
    homedir = ENV['HOME'] || '/home'
    filename = 'complearnrc'
    goodcon = nil
    [ "#{homedir}/.#{filename}", "/etc/#{filename}" ].each { |pathname|
      next unless File.exist?(pathname)
      goodcon = CLConfig.new(pathname)
      break if goodcon
    }
    goodcon = CLConfig.new(nil) unless goodcon
    @@DEFCON = goodcon
  end
  @@DEFCON
end

.printVersionAndExitObject



237
238
239
240
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 237

def self.printVersionAndExit()
  puts "CompLearn #{FoundComp::VERSION}"
  exit(0)
end

.setDefaultConfig(cfg) ⇒ Object



56
57
58
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 56

def self.setDefaultConfig(cfg)
  @@DEFCON = cfg
end

.setSingleUserObject



53
54
55
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 53

def self.setSingleUser()
  @@SINGLEUSER = true
end

.singleUser?Boolean

A class-method (since self is in class/module-scope, it refers to the Class object CLConfig). This one just tries to find a configuration file in a default spot, either in $HOME or /etc Also, this method caches the first read config object as a Singleton.

Returns:

  • (Boolean)


50
51
52
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 50

def self.singleUser?()
  defined?(@@SINGLEUSER)
end

.writeDefaultConfigFile(fname) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 103

def self.writeDefaultConfigFile(fname)
  cfgmap = { }
  @@CONFIGVARS.each { |lowername, stuff|
    isArray, typ, defval, prettyName, methodName = stuff
    cfgmap[prettyName] = defval
  }
  File.open(fname, "w") { |f|
    f.write(cfgmap.to_yaml)
  }
end

Instance Method Details

#findInputFile(fname) ⇒ Object

Searches for a file or directory specified by fname if fname is relative, it will search for it in the following order: inputDir specified in this CLConfig object current working directory

If found, a string is returned with the absolute (full) pathname. If not, an exception is raised



179
180
181
182
183
184
185
186
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 179

def findInputFile(fname)
  raise "fname can not be nil" if fname == nil
  maybe = File.expand_path(fname, self.inputDir)
  return maybe if File.exist?(maybe)
  maybe = File.expand_path(fname)
  return maybe if File.exist?(maybe)
  raise "Cannot find file to open: #{fname}"
end

#getFilelistFromDirOrFile(objname) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 211

def getFilelistFromDirOrFile(objname)
  files = [ ]
  if File.ftype(objname) == 'directory'
    d = Dir.new(objname)
    d.each { |f|
      goodf = "#{objname}/#{f}"
      files << goodf if File.ftype(goodf) == 'file'
    }
    files.sort!
  else
    files = readFileList(objname)
  end
  files
end

#parseValue(typ, val) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 144

def parseValue(typ, val)
  val = val.clone
  # remove whitespace
  val.gsub!(/^\s+/,'')
  val.gsub!(/\s+$/,'')
  case typ.to_s   # to_s necessary because === checks is_a? for Class

    when 'TrueClass'
      return val =~ /true/i || val =~ /yes/i || val =~ /on/i

    when 'String'
      return val 

    when 'Float'
      return val.to_f

    when 'Integer'
      return val.to_i

    else
      puts "Illegal type: #{typ} for value #{val}"
      exit(1)

  end
    
end

#readCLConfigObject

read in the configuration file



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 117

def readCLConfig()
    begin
        yamlcfg = YAML::parse(File::new(@filename,"r").read)
    rescue
        print "Unable to read config file " + @filename + ": " + $!   
        print "\n"
        exit
    end

    #fail "@filename must be a map not a #{yamlcfg.type_id}" unless yamlcfg.type_id.to_s == 'map'

    yamlcfg.value.each { |itagname, whatnot|
          tagname, value = itagname.downcase, whatnot[1].value
          if @@CONFIGVARS.has_key?(tagname)
            isArray,typ,default, pretty, methodName = @@CONFIGVARS[tagname]
            if isArray
              result = value.map { |i| parseValue(typ, i.value) }
            else
              result = parseValue(typ, value)
            end
            instance_eval("@#{methodName}=result")
          else
            fail "Unknown configuration option: #{itagname}"
          end
    }
end

#readFileList(fname) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 225

def readFileList(fname)
  f = File.open(fname, 'r')
  result = [ ]
  while line = f.gets
    line.chomp!
    next if line =~ /^#/
    next unless line =~ /[\S]/
    realfname = findInputFile(line)
    result << realfname
  end
  result
end

#readTaggedFileList(fname) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/lib/CompLearnLib/CLConfig.rb', line 188

def readTaggedFileList(fname)
  training = [ ]
  features = [ ]
  testing = [ ]
  f = File.open(fname, 'r')
  while line = f.gets
    line.chomp!
    next if line =~ /^\s*#/
    next unless line =~ /[\S]/
    if line =~ /^\s*(\S+)\s+(\S)\s+([^\s#]+)/
      tagnum, tagtype, fname = $1.to_i, $2.downcase, $3
      realfname = findInputFile(fname)
      if tagtype == 'p'
        testing << [ tagnum.to_i, realfname ]
      elsif tagtype == 'f'
        features << realfname
      else   # tagtype == 'g'
        training << [ tagnum.to_i, realfname ]
      end
    end
  end
  return [ training, features, testing ]
end