Project

General

Profile

Download (2.15 KB) Statistics
| Branch: | Tag: | Revision:
module CyborgHood
class Command
attr_reader :cmdline, :cmdsplit, :parsing_errors

def initialize(cmdline, cmdsplit, parsing_errors = [])
@cmdline = cmdline
@cmdsplit = cmdsplit
@parsing_errors = parsing_errors
end

def valid?
@parsing_errors.empty?
end
end

class SharedParameter
attr_reader :type, :content

def initialize(content, type = nil)
@content = content
@type = type
end
end

class ParameterReference
attr_reader :reference

def initialize(reference)
@reference = reference
end
end

class Order
include GetText
attr_reader :error, :user, :commands, :shared_parameters

def initialize(params = {})
@error = params[:error]
@user = params[:user]
@commands = params[:commands]
@shared_parameters = params[:shared_parameters]
end

def self.parse(user, command_lines, shared_parameters)
logger.debug "Parsing Order"
used_refs = []
commands = []
command_lines.each do |line|
line.strip!
# skip empty lines and comments
next if line == "" or line[0, 1] == "#"

errors = []
begin
raw_cmd_parts = line.shellsplit
rescue
errors << N_("Syntax error in command.")
raw_cmd_parts = []
end
cmd_parts = raw_cmd_parts.collect do |word|
yield word, errors, used_refs
end

commands << Command.new(line, cmd_parts, errors)
end

# remove references to unused parameters
shared_parameters.delete_if{|ref, param| not used_refs.include?(ref) }

logger.debug "Order is OK"
new(:user => user, :commands => commands, :shared_parameters => shared_parameters)
end

def dereference_param(shared_parameters, param)
if param.is_a? SharedParameter
[ref, ParameterReference(ref)]
elsif param.is_a? ParameterReference
d_ref = param.reference
d_param = shared_parameters[d_ref]
return dereference_param(shared_parameters, d_param)
else
nil
end
end

def valid?
@error.nil? and @user and not @commands.empty?
end
end
end
(9-9/9)