class Sanzang::Command::Batch
This class implements a command for batch translation of texts. The command presumes that the list of input files will be read from $stdin, while the output files will be written to a single directory. Usage information can be accessed by passing in the “-h” or “–help” options.
Attributes
name[R]
Name of the command
Public Class Methods
new()
click to toggle source
Create a new instance of the batch command.
# File lib/sanzang/command/batch.rb, line 32 def initialize @name = "sanzang batch" @encoding = Sanzang::Platform.data_encoding @outdir = nil @jobs = nil @verbose = false end
Public Instance Methods
run(args)
click to toggle source
Run the batch command with the given arguments. The parameter args would typically be an array of command options and parameters. Calling this method with the “-h” or “–help” option will print full usage information necessary for running the command. This method will return either 0 (success) or 1 (failure).
# File lib/sanzang/command/batch.rb, line 46 def run(args) parser = option_parser parser.parse!(args) if args.length != 2 $stderr.puts parser return 1 end translator = nil File.open(args[0], "rb", encoding: @encoding) do |table_file| table = Sanzang::TranslationTable.new(table_file.read) translator = Sanzang::BatchTranslator.new(table) end $stdin.binmode.set_encoding(@encoding) puts translator.translate_to_dir($stdin.read.split, args[1], true, @jobs) return 0 rescue SystemExit => err return err.status rescue Interrupt puts return 0 rescue Errno::EPIPE return 0 rescue Exception => err if @verbose $stderr.puts err.backtrace end $stderr.puts "#{@name.split[0]}: #{err.inspect}" return 1 end
Private Instance Methods
option_parser()
click to toggle source
Return an OptionParser object for this command
# File lib/sanzang/command/batch.rb, line 87 def option_parser OptionParser.new do |op| op.banner = "Usage: #{@name} [options] table output_dir < queue\n" op.banner << "\nBatch translate files concurrently. A list of files " op.banner << "is read from STDIN, while\nprogress information is " op.banner << "printed to STDERR. The list of output files written is\n" op.banner << "printed to STDOUT at the end of the batch. The " op.banner << "output directory is specified as\na parameter.\n" op.banner << "\nOptions:\n" op.on("-h", "--help", "show this help message and exit") do |v| puts op exit 0 end op.on("-E", "--encoding=ENC", "set data encoding to ENC") do |v| @encoding = Encoding.find(v) end op.on("-L", "--list-encodings", "list possible encodings") do |v| Sanzang::Platform.valid_encodings.each {|e| puts e.to_s } exit 0 end op.on("-j", "--jobs=N", "allow N concurrent processes") do |v| @jobs = v.to_i end op.on("-v", "--verbose", "verbose mode for debugging") do |v| @verbose = true end end end