<commit_before>
module Coral
module Plugin
class Action < Base
  
  include Mixin::CLI::Node

  #-----------------------------------------------------------------------------
  # Action plugin interface
  
  def self.exec_safe(provider, options)
    begin
      logger = Coral.logger
      
      logger.debug("Running coral action #{provider} with #{options.inspect}")
      exit_status = Coral.action(provider, options).execute
      
    rescue Exception => error
      logger.error("Coral action #{provider} experienced an error:")
      logger.error(error.inspect)
      logger.error(error.message)
      logger.error(Coral::Util::Data.to_yaml(error.backtrace))

      Coral.ui.error(error.message, { :prefix => false }) if error.message
  
      exit_status = error.status_code if error.respond_to?(:status_code)
    end

    exit_status = Coral.code.unknown_status unless exit_status.is_a?(Integer)
    exit_status  
  end
  
  def self.exec(provider, options, quiet = true)
    exec_safe(provider, { :settings => Config.ensure(options), :quiet => quiet })
  end
  
  def self.exec_cli(provider, args, quiet = false)
    exec_safe(provider, { :args => args, :quiet => quiet })
  end
  
  #---
  
  def normalize
    args = array(delete(:args, []))
    
    if get(:settings, nil)
      set(:processed, true)  
    else
      set(:settings, {})
      parse(args)
    end
  end
  
  #-----------------------------------------------------------------------------
  # Checks
  
  def quiet?
    get(:quiet, false)
  end
  
  #---
  
  def processed?
    get(:processed, false)
  end
  
  #-----------------------------------------------------------------------------
  # Property accessor / modifiers
  
  def params
    get_array(:params)
  end
  
  #--
  
  def settings
    get(:settings)
  end
  
  #---
  
  def help
    return @parser.help if @parser
    ''
  end

  #-----------------------------------------------------------------------------
  # Operations
  
  def parse(args, banner = '')    
    logger.info("Parsing action #{plugin_provider} with: #{args.inspect}")
    
    set(:params, array(args))
    
    @parser = Util::CLI::Parser.new(args, banner) do |parser| 
      yield(parser) if block_given?
      node_options(parser)
      
      extension(:parse, { :parser => parser })
    end
    
    if @parser 
      if @parser.processed
        set(:processed, true)
        set(:settings, Config.new(Util::Data.merge([ @parser.options, @parser.arguments ], true)))
        
        logger.debug("Parse successful: #{export.inspect}")
        
      elsif @parser.options[:help] && ! quiet?
        puts I18n.t('coral.core.exec.help.usage') + ': ' + @parser.help + "\n"
        
      else
        if @parser.options[:help]
          logger.debug("Help wanted but running in silent mode")
        else
          logger.warn("Parse failed for unknown reasons")
        end
      end
    end 
    self  
  end
  
  #---
  
  def execute
    logger.info("Executing action #{plugin_provider}")
    
    if processed?
      status = node_exec do |node, network|
        hook_config = { :node => node, :network => network }
        
        begin
          status = yield(node, network) if extension_check(:exec_init, hook_config) && block_given?
          status = extension_set(:exec_exit, status, hook_config)
          
        ensure
          cleanup
        end
        status
      end
    else
      if @parser.options[:help]
        status = Coral.code.help_wanted
      else
        status = Coral.code.action_unprocessed
      end
    end
    
    code_name = Codes.index(status)
    logger.warn("Execution failed for #{plugin_provider} with status #{status} (#{code_name}): #{export.inspect}") if processed? && status > 1 
    <commit_msg>Removing params accessor and set in the base action plugin and adding a default execute status.<commit_after>
module Coral
module Plugin
class Action < Base
  
  include Mixin::CLI::Node

  #-----------------------------------------------------------------------------
  # Action plugin interface
  
  def self.exec_safe(provider, options)
    begin
      logger = Coral.logger
      
      logger.debug("Running coral action #{provider} with #{options.inspect}")
      exit_status = Coral.action(provider, options).execute
      
    rescue Exception => error
      logger.error("Coral action #{provider} experienced an error:")
      logger.error(error.inspect)
      logger.error(error.message)
      logger.error(Coral::Util::Data.to_yaml(error.backtrace))

      Coral.ui.error(error.message, { :prefix => false }) if error.message
  
      exit_status = error.status_code if error.respond_to?(:status_code)
    end

    exit_status = Coral.code.unknown_status unless exit_status.is_a?(Integer)
    exit_status  
  end
  
  def self.exec(provider, options, quiet = true)
    exec_safe(provider, { :settings => Config.ensure(options), :quiet => quiet })
  end
  
  def self.exec_cli(provider, args, quiet = false)
    exec_safe(provider, { :args => args, :quiet => quiet })
  end
  
  #---
  
  def normalize
    args = array(delete(:args, []))
    
    if get(:settings, nil)
      set(:processed, true)  
    else
      set(:settings, {})
      parse(args)
    end
  end
  
  #-----------------------------------------------------------------------------
  # Checks
  
  def quiet?
    get(:quiet, false)
  end
  
  #---
  
  def processed?
    get(:processed, false)
  end
  
  #-----------------------------------------------------------------------------
  # Property accessor / modifiers
  
  def settings
    get(:settings)
  end
  
  #---
  
  def help
    return @parser.help if @parser
    ''
  end

  #-----------------------------------------------------------------------------
  # Operations
  
  def parse(args, banner = '')    
    logger.info("Parsing action #{plugin_provider} with: #{args.inspect}")
    
    @parser = Util::CLI::Parser.new(args, banner) do |parser| 
      yield(parser) if block_given?
      node_options(parser)
      
      extension(:parse, { :parser => parser })
    end
    
    if @parser 
      if @parser.processed
        set(:processed, true)
        set(:settings, Config.new(Util::Data.merge([ @parser.options, @parser.arguments ], true)))
        
        logger.debug("Parse successful: #{export.inspect}")
        
      elsif @parser.options[:help] && ! quiet?
        puts I18n.t('coral.core.exec.help.usage') + ': ' + @parser.help + "\n"
        
      else
        if @parser.options[:help]
          logger.debug("Help wanted but running in silent mode")
        else
          logger.warn("Parse failed for unknown reasons")
        end
      end
    end 
    self  
  end
  
  #---
  
  def execute
    logger.info("Executing action #{plugin_provider}")
    
    if processed?
      status = node_exec do |node, network|
        hook_config = { :node => node, :network => network }
        
        begin
          status = Coral.code.success
          status = yield(node, network) if extension_check(:exec_init, hook_config) && block_given?
          status = extension_set(:exec_exit, status, hook_config)
        ensure
          cleanup
        end
        status
      end
    else
      if @parser.options[:help]
        status = Coral.code.help_wanted
      else
        status = Coral.code.action_unprocessed
      end
    end
    
    code_name = Codes.index(status)
    logger.warn("Execution failed for #{plugin_provider} with status #{status} (#{code_name}): #{export.inspect}") if processed? && status > 1 
    
{'fraction_non_alphanumeric': 0.18978295394388564, 'fraction_numerical': 0.0007940709370037057, 'mean_word_length': 1.7772877618522602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1532540', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2140, 'n_words': 503}