class CastError < StandardError
end
-# Class using OptionParser to parse command line options according to a list of
-# casts. Each cast prescribes the long and short name of the option, the type,
-# if it is mandatory, the default value, and allowed and disallowed values. An
-# optional list of extra casts can be supplied, and the integrity of the casts
-# are checked. Following the command line parsing, the options are checked
-# according to the casts.
+# Class using for handling Biopieces the parsing and emitting of Biopiece records,
+# which are ASCII text records consisting of lines with a key/value pair seperated
+# by a colon and a white space ': '. Each record is separated by a line with three
+# dashes '---'.
+#
+# Biopieces are command line scripts and uses OptionParser to parse command line
+# options according to a list of casts. Each cast prescribes the long and short
+# name of the option, the type, if it is mandatory, the default value, and allowed
+# and disallowed values. An optional list of extra casts can be supplied, and the
+# integrity of the casts are checked. Following the command line parsing, the
+# options are checked according to the casts.
class Biopieces
TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
MANDATORY = %w[long short type mandatory default allowed disallowed]
REGEX_INT = /^(int|uint)$/
REGEX_STRING = /^(string|file|file!|dir|dir!|genome)$/
+ # Initialize a Biopiece and write the status to file.
+ def initialize
+ status_set
+ end
+
# Check the integrity of a list of casts, followed by parsion options from argv
# and finally checking the options according to the casts. Returns nil if
# argv is empty, otherwise an options hash.
@options
end
+ # Open Biopiece input stream if not open and iterate over all Biopiece
+ # records in the stream.
+ def each_record
+ @in = stream_in_open unless @in.is_a? IO
+
+ record = {}
+
+ @in.each_line do |line|
+ case line
+ when /^([^:]+): (.*)$/
+ record[$1] = $2
+ when /^---$/
+ yield record unless record.empty?
+ record = {}
+ else
+ raise "Bad record format: #{line}"
+ end
+ end
+
+ yield record unless record.empty?
+
+ self # conventionally
+ end
+
+ alias :each :each_record
+
+ # Open Biopiece output stream if not open and puts record to the stream.
+ def puts(record)
+ @out = stream_out_open unless @out.is_a? IO
+
+ record.each do |key,value|
+ @out.print "#{key}: #{value}\n"
+ end
+
+ @out.print "---\n"
+ end
+
+ # Close Biopiece streams, remove tmp_dir, end log status.
+ def clean
+ @in.close if @in.respond_to? :close
+ @out.close if @out.respond_to? :close
+ # remove tmpdir if found
+ status_log
+ # remove status file
+ end
+
private
# Given the script name determine the path of the wiki file with the usage info.
end
end
end
-end
-
-
-class Stream
- def initialize(options)
- @options = options
- @in = stream_in_open
- @out = stream_out_open
- end
-
- def each_record
- record = {}
-
- @in.each_line do |line|
- case line
- when /^([^:]+): (.*)$/
- record[$1] = $2
- when /^---$/
- yield record unless record.empty?
- record = {}
- else
- raise "Bad record format: #{line}"
- end
- end
-
- yield record unless record.empty?
-
- self # conventionally
- end
-
- alias :each :each_record
-
- def puts(record)
- record.each do |key,value|
- @out.print "#{key}: #{value}\n"
- end
-
- @out.print "---\n"
- end
-
- private
# Open Biopieces input data stream for reading from either
# stdin or from a list of files specified in options["stream_in"].
def nwrite(file)
File.open(file, mode="w")
end
-end
-
-
-class Status
- def initialize
- status_set
- end
+ # Write the status to a status file.
def status_set
now = Time.new
time = now.strftime("%Y-%m-%d %X")
File.open(path, mode="w") { |file| file.puts [time, ARGV.join(" ")].join(";") }
end
- def log(status="OK")
+ # Write the Biopiece status to the log file.
+ def status_log(status="OK")
now = Time.new
time1 = now.strftime("%Y-%m-%d %X")
user = ENV["USER"]
File.open(log_file, mode="a") { |file| file.puts [time0, time1, elap, user, status, command].join("\t") }
end
- private
-
+ # Get the elapsed time from the difference between two time stamps.
def time_diff(t0, t1)
t0 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/
year0 = $1.to_i
end
end
-
__END__