<commit_before># Copyright 2008-2009 Nokia Siemens Networks Oyj
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


require 'xmlrpc/server'
require 'xmlrpc/utils'
require 'stringio'

# Necessary to parse out documentation and args
require 'yard'


class RobotRemoteServer < XMLRPC::Server
  #
  # Constructor for RobotRemoteServer
  #
  # @param [Object] library instance of a class in which are defined keywords
  # @param [String] host the name or location from which this server will serve content
  # @param [Integer] port the port at which this server will serve content
  # @param [String] yardoc_file the file which YARD expects when loading its Registry
  # @param [Hash] yardoc_options a hash of YARD::CodeObject::Base.method => human-readable label for docs
  def initialize(library, host='localhost', port=8270, yardoc_file = '.yardoc', yardoc_options = [[:docstring, '']])
    @library = library
    # Get YARD registry loaded into memory
    @reg = YARD::Registry
    @reg.load(yardoc_file)
    @reg.load_all
    @reg_options = yardoc_options
    # End yard setup
    super(port, host)  # TODO: Disable logging to stdout
    add_handler('get_keyword_names') { get_keyword_names }
    add_handler('run_keyword') { |name,args| run_keyword(name, args) }
    add_handler('get_keyword_arguments') { |name| get_keyword_arguments(name) }
    add_handler('get_keyword_documentation') { |name| get_keyword_documentation(name) }
    add_handler('stop_remote_server') { shutdown }
    puts "Robot Framework remote library started at #{host}:#{port}"
    serve
  end

  def get_keyword_names
    # Implicit methods can't be used as keywords
    @library.methods - Object.new.methods
  end

  def run_keyword(name, args)
    intercept_stdout()
    result = {:status=>'PASS', :return=>'', :output=>'',
              :error=>'', :traceback=>''}
    begin
      return_value = @library.send(name, *args)
      result[:return] = handle_return_value(return_value)
    rescue Exception => exception
      result[:status] = 'FAIL'
      result[:error] = exception.message
      result[:traceback] = "Traceback:\n" + exception.backtrace.join("\n")
    end
    result[:output] = restore_stdout
    return result
  end

  def get_keyword_arguments(name)
    parent_class = @library.method(name).owner.to_s
    codeobj = @reg.resolve(P(parent_class), "#" + name)
    params = codeobj.parameters
    params_docs = []
    params.each do |param|
      if param[1].nil? # YARD sets the keyword arg to nil if absent<commit_msg>Fixed support for "Stop Remote Server" keyword expected to be present for Remote libraries<commit_after># Copyright 2008-2009 Nokia Siemens Networks Oyj
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


require 'xmlrpc/server'
require 'xmlrpc/utils'
require 'stringio'

# Necessary to parse out documentation and args
require 'yard'


class RobotRemoteServer < XMLRPC::Server
  #
  # Constructor for RobotRemoteServer
  #
  # @param [Object] library instance of a class in which are defined keywords
  # @param [String] host the name or location from which this server will serve content
  # @param [Integer] port the port at which this server will serve content
  # @param [String] yardoc_file the file which YARD expects when loading its Registry
  # @param [Hash] yardoc_options a hash of YARD::CodeObject::Base.method => human-readable label for docs
  def initialize(library, host='localhost', port=8270, yardoc_file = '.yardoc', yardoc_options = [[:docstring, '']])
    @library = library
    # Get YARD registry loaded into memory
    @reg = YARD::Registry
    @reg.load(yardoc_file)
    @reg.load_all
    @reg_options = yardoc_options
    # End yard setup
    super(port, host)  # TODO: Disable logging to stdout
    add_handler('get_keyword_names') { get_keyword_names }
    add_handler('run_keyword') { |name,args| run_keyword(name, args) }
    add_handler('get_keyword_arguments') { |name| get_keyword_arguments(name) }
    add_handler('get_keyword_documentation') { |name| get_keyword_documentation(name) }
    add_handler('stop_remote_server') { shutdown }
    puts "Robot Framework remote library started at #{host}:#{port}"
    serve
  end

  def get_keyword_names
    # Implicit methods can't be used as keywords
    @library.methods - Object.new.methods + [:stop_remote_server]
  end

  def run_keyword(name, args)
    intercept_stdout()
    result = {:status=>'PASS', :return=>'', :output=>'',
              :error=>'', :traceback=>''}
    begin
      if name == 'stop_remote_server'
        shutdown
      else
        return_value = @library.send(name, *args)
      end
      result[:return] = handle_return_value(return_value)
    rescue Exception => exception
      result[:status] = 'FAIL'
      result[:error] = exception.message
      result[:traceback] = "Traceback:\n" + exception.backtrace.join("\n")
    end
    result[:output] = restore_stdout
    return result
  end

  def get_keyword_arguments(name)
    parent_class = @library.method(name).owner.to_s
    codeobj = @reg.resolve(P(parent_class), "#" + name)
    params = codeobj.parameters
    params_docs = []
    params.each do |param|
      if param[1].nil? # YARD sets the keyword arg to nil if absent
{'fraction_non_alphanumeric': 0.10066139699951605, 'fraction_numerical': 0.005484755605742862, 'mean_word_length': 3.95603517186251, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4980557', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1786, 'n_words': 601}