Class: Madeline::Interface

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

Constant Summary collapse

DEFAULT_OPTIONS =
{}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interface

Returns a new instance of Interface.



12
13
14
15
# File 'lib/madeline/interface.rb', line 12

def initialize(options={})
  @madeline = options.delete(:madeline) || MADELINE_COMMAND
  @options = serialize_options(DEFAULT_OPTIONS.merge(options))
end

Instance Method Details

#draw(io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/madeline/interface.rb', line 17

def draw(io)
  tempfile = Tempfile.new('madeline')
  if io.respond_to? :read
    while buffer = io.read(4096) do
      tempfile.write(buffer)
    end
  else 
    tempfile.write(io.to_s)
  end
  tempfile.flush
  tempoutfile = Tempfile.new('madeline_output')
  if !File.exists?(tempfile.path)
    raise Error, "Failed to create madeline input file #{tempfile.path}"
  end
  begin
    log = `#{command} --outputprefix #{tempoutfile.path} --outputext .xml #{tempfile.path} 2>&1`
    unless (log.match(/Pedigree output file is/)) then
	  raise Error, "Madeline failed to run or had a segmentation fault.  #{command} --outputprefix #{tempoutfile.path} --outputext .xml #{tempfile.path}"
	end
    filename = "#{tempoutfile.path}.xml"
	if (!File.exists?(filename)) then
	  raise Error, "Output File doesn't exist. #{filename}   command was : #{command} --outputprefix #{tempoutfile.path} --outputext .xml #{tempfile.path}"
    end
  rescue 
	if log.nil? then
      raise Error, "Madeline failed to run. Tried #{@madeline}.  #{$!}"
	else
	  raise Error, "Madeline had an error: #{log}  Tried #{@madeline}.\n #{$!}\n"
	end
  ensure
    tempfile.close!
  end
  unless $?.exitstatus.zero?
    raise Error, log 
  end
  warnings = Array.new
  lines = log.split("\n")
  lines.each do |line|
	line.gsub!("\e[0m","") # remove the end color code from madeline2
    warnings.push(line.match(/Warning:.*/)[0]) if line.match(/Warning:/)
  end
  warn = warnings.join("; ")
  if block_given? then
    f = File.new(filename)
	contents = f.read
	File.delete(filename)
    yield(StringIO.new(contents), warn)
  end
  return filename, warn
end