root/lib/cyborghood/order.rb @ d5a0d3bc
e7315259 | Marc Dequènes (Duck) | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|||
#
|
|||
# This program is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
cb0d68fd | Marc Dequènes (Duck) | 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
|
|||
8347a623 | Marc Dequènes (Duck) | attr_reader :content_type, :content
|
|
cb0d68fd | Marc Dequènes (Duck) | ||
8347a623 | Marc Dequènes (Duck) | def initialize(content, content_type = nil)
|
|
cb0d68fd | Marc Dequènes (Duck) | @content = content
|
|
8347a623 | Marc Dequènes (Duck) | @content_type = content_type
|
|
cb0d68fd | Marc Dequènes (Duck) | end
|
|
end
|
|||
class ParameterReference
|
|||
attr_reader :reference
|
|||
def initialize(reference)
|
|||
@reference = reference
|
|||
end
|
|||
end
|
|||
class Order
|
|||
ed09e1e5 | Marc Dequènes (Duck) | include I18nTranslation
|
|
cb0d68fd | Marc Dequènes (Duck) | 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 = []
|
|||
2da8345d | Marc Dequènes (Duck) | begin
|
|
raw_cmd_parts = line.shellsplit
|
|||
rescue
|
|||
eb6e0359 | Marc Dequènes (Duck) | errors << _("Syntax error in command.")
|
|
2da8345d | Marc Dequènes (Duck) | raw_cmd_parts = []
|
|
end
|
|||
cmd_parts = raw_cmd_parts.collect do |word|
|
|||
b25e9035 | Marc Dequènes (Duck) | yield word, errors, used_refs
|
|
cb0d68fd | Marc Dequènes (Duck) | 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"
|
|||
b25e9035 | Marc Dequènes (Duck) | new(:user => user, :commands => commands, :shared_parameters => shared_parameters)
|
|
cb0d68fd | Marc Dequènes (Duck) | end
|
|
50460df2 | Marc Dequènes (Duck) | def self.dereference_param(shared_parameters, param, ref = nil)
|
|
cb0d68fd | Marc Dequènes (Duck) | if param.is_a? SharedParameter
|
|
8347a623 | Marc Dequènes (Duck) | [ref, param]
|
|
cb0d68fd | Marc Dequènes (Duck) | elsif param.is_a? ParameterReference
|
|
d_ref = param.reference
|
|||
d_param = shared_parameters[d_ref]
|
|||
50460df2 | Marc Dequènes (Duck) | return dereference_param(shared_parameters, d_param, d_ref)
|
|
cb0d68fd | Marc Dequènes (Duck) | else
|
|
nil
|
|||
end
|
|||
end
|
|||
def valid?
|
|||
@error.nil? and @user and not @commands.empty?
|
|||
end
|
|||
end
|
|||
end
|