<commit_before>module Olelo
  # Olelo plugin system
  class Plugin < Module
    include Util
    include Hooks

    has_around_hooks :load

    @loaded = {}
    @failed = []
    @disabled = []
    @dir = ''

    class<< self
      attr_accessor :dir, :disabled

      # Get failed plugins
      attr_reader :failed

      # Get loaded plugins
      def loaded
        @loaded.values
      end

      # Start plugins
      # @return [void]
      def start
        @loaded.each_value {|plugin| plugin.start }
      end

      def register(path, plugin)
        @loaded[path] = plugin
      end

      # Load plugins by path
      #
      # @param list List of plugin paths to load
      # @return [Boolean] true if every plugin was loaded
      def load(*list)
        files = list.map {|path| [File.join(@dir, path, 'main.rb'), File.join(@dir, "#{path}.rb")] }.flatten.select {|file| File.file?(file) }
        return false if files.empty?
        files.inject(true) do |result,file|
          path = File.basename(file) == 'main.rb' ? file[(@dir.size+1)..-9] : file[(@dir.size+1)..-4]
          if @loaded.include?(path)
	    result
	  elsif @failed.include?(path) || !enabled?(path)
	    false
	  else
            begin
	      new(path, file)
            rescue Exception => ex
              @failed << path
              if LoadError === ex
                Olelo.logger.warn "Plugin #{path} could not be loaded due to: #{ex.message} (Missing gem?)"
              else
                Olelo.logger.error "Plugin #{path} could not be loaded due to: #{ex.message}"
                Olelo.logger.debug ex
              end
              @loaded.delete(path)
              false
            end
	  end
        end
      end

      # Load all plugins
      def load_all
        load(*Dir[File.join(@dir, '**', '*.rb')].map {|file| file[(@dir.size+1)..-4] })
      end

      # Check if plugin is enabled
      #<commit_msg>plugin loading improved<commit_after>module Olelo
  # Olelo plugin system
  class Plugin < Module
    include Util
    include Hooks

    has_around_hooks :load

    @loaded = {}
    @failed = []
    @disabled = []
    @dir = ''

    class<< self
      attr_accessor :dir, :disabled

      # Get failed plugins
      attr_reader :failed

      # Get loaded plugins
      def loaded
        @loaded.values
      end

      # Start plugins
      # @return [void]
      def start
        @loaded.each_value {|plugin| plugin.start }
      end

      def register(path, plugin)
        @loaded[path] = plugin
      end

      # Load plugins by path
      #
      # @param list List of plugin paths to load
      # @return [Boolean] true if every plugin was loaded
      def load(*list)
        files = []
        list.each do |path|
          path = path.sub(%r{/main$}, '')
          f = [File.join(@dir, path, 'main.rb'), File.join(@dir, "#{path}.rb")].select {|file| File.file?(file) }
          raise "Duplicate plugin #{f.join(', ')}" if f.size == 2
          files << [path, f.first] unless f.empty?
        end

        return false if files.empty?
        files.inject(true) do |result,(path,file)|
          if @loaded.include?(path)
	    result
	  elsif @failed.include?(path) || !enabled?(path)
	    false
	  else
            begin
	      new(path, file)
            rescue Exception => ex
              @failed << path
              if LoadError === ex
                Olelo.logger.warn "Plugin #{path} could not be loaded due to: #{ex.message} (Missing gem?)"
              else
                Olelo.logger.error "Plugin #{path} could not be loaded due to: #{ex.message}"
                Olelo.logger.debug ex
              end
              @loaded.delete(path)
              false
            end
	  end
        end
      end

      # Load all plugins
      def load_all
        load(*Dir[File.join(@dir, '**', '*.rb')].map {|file| file[(@dir.size+1)..-4] })
      end

      # Check if plugin is enabled
      #
{'fraction_non_alphanumeric': 0.12179649835067242, 'fraction_numerical': 0.002283684344075108, 'mean_word_length': 1.7055593685655457, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '1734310', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1154, 'n_words': 337}