Project

General

Profile

Download (3.15 KB) Statistics
| Branch: | Tag: | Revision:
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/>.
#++

afd9ef46 Marc Dequènes (Duck)
require 'shellwords'


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

3d7bbe55 Marc Dequènes (Duck)
class CommandParameter < String
attr_reader :content_type
cb0d68fd Marc Dequènes (Duck)
8347a623 Marc Dequènes (Duck)
def initialize(content, content_type = nil)
3d7bbe55 Marc Dequènes (Duck)
super(content)
8347a623 Marc Dequènes (Duck)
@content_type = content_type
cb0d68fd Marc Dequènes (Duck)
end
3d7bbe55 Marc Dequènes (Duck)
def content
to_s
end
cb0d68fd Marc Dequènes (Duck)
end

3d7bbe55 Marc Dequènes (Duck)
class CommandParameterRef
cb0d68fd Marc Dequènes (Duck)
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

3d7bbe55 Marc Dequènes (Duck)
def valid?
@error.nil? and @user and not @commands.empty?
end
end

class OrderParser
include I18nTranslation

def initialize(user)
@user = user

@commands = []
@shared_parameters = {}
@used_refs = []
end

def parse(command_lines)
cb0d68fd Marc Dequènes (Duck)
logger.debug "Parsing Order"
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|
3d7bbe55 Marc Dequènes (Duck)
yield word, errors
cb0d68fd Marc Dequènes (Duck)
end

3d7bbe55 Marc Dequènes (Duck)
@commands << Command.new(line, cmd_parts, errors)
cb0d68fd Marc Dequènes (Duck)
end

# remove references to unused parameters
3d7bbe55 Marc Dequènes (Duck)
@shared_parameters.delete_if{|ref, param| not @used_refs.include?(ref) }
cb0d68fd Marc Dequènes (Duck)
logger.debug "Order is OK"
3d7bbe55 Marc Dequènes (Duck)
Order.new(:user => @user, :commands => @commands, :shared_parameters => @shared_parameters)
cb0d68fd Marc Dequènes (Duck)
end

3d7bbe55 Marc Dequènes (Duck)
def deref_param(param, old_ref = nil)
if param.is_a? CommandParameterRef
return deref_param(@shared_parameters[param.reference], param.reference)
elsif param.is_a? CommandParameter
return old_ref, param
elsif param.is_a? String or param.is_a? Fixnum
return deref_param(@shared_parameters[param], param)
cb0d68fd Marc Dequènes (Duck)
else
nil
end
end
end
end