<commit_before>module Listen
  module Adapter
    # Adapter implementation for Windows `wdm`.
    #
    class Windows < Base
      # The message to show when wdm gem isn't available
      #
      BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
        Please add the following to your Gemfile to avoid polling for changes:
          require 'rbconfig'
          if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
            gem 'wdm', '>= 0.1.0'
          end
      EOS

      def self.usable?
        if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
          require 'wdm'
          true
        end
      rescue LoadError
        Kernel.warn BUNDLER_DECLARE_GEM
        false
      end

      def start
        worker = _init_worker
        Thread.new { worker.run! }
      end

      private

      # Initializes a WDM monitor and adds a watcher for
      # each directory passed to the adapter.
      #
      # @return [WDM::Monitor] initialized worker
      #
      def _init_worker
        WDM::Monitor.new.tap do |worker|
          _directories_path.each do |path|
            worker.watch_recursively(path.to_s, :files,
                                     &_worker_file_callback)

            worker.watch_recursively(path.to_s, :directories,
                                     &_worker_dir_callback)

            worker.watch_recursively(path.to_s, :attributes, :last_write,
                                     &_worker_attr_callback)
          end
        end
      end

      def _worker_file_callback
        lambda do |change|
          begin
            path = _path(change.path)
            options = { type: 'File', change: _change(change.type) }
            _notify_change(path, options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _worker_attr_callback
        lambda do |change|
          begin
            path = _path(change.path)
            return if path.directory?

            options = { type: 'File', change: _change(change.type) }
            _notify_change(_path(change.path), options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _worker_dir_callback
        lambda do |change|
          begin
            path = _path(change.path)
            if change.type == :removed
              _notify_change(path.dirname, type: 'Dir')
            elsif change.type == :added
              _notify_change(path, type: 'Dir')
            else
              # do nothing - changed directory means either:
              #   - removed subdirs (handled above)
              #   - added subdirs (handled above)
              #   - removed files (handled by _worker_file_callback)
              #   - added files (handled by _worker_file_callback)
            end
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _path(path)<commit_msg>add extra wdm debugging<commit_after>module Listen
  module Adapter
    # Adapter implementation for Windows `wdm`.
    #
    class Windows < Base
      # The message to show when wdm gem isn't available
      #
      BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
        Please add the following to your Gemfile to avoid polling for changes:
          require 'rbconfig'
          if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
            gem 'wdm', '>= 0.1.0'
          end
      EOS

      def self.usable?
        if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
          require 'wdm'
          true
        end
      rescue LoadError
        _log :error, "wdm - load failed: #{$!}:#{$@.join("\n")}"
        Kernel.warn BUNDLER_DECLARE_GEM
        false
      end

      def start
        _log :warn, 'wdm - starting...'
        worker = _init_worker
        Thread.new do
          begin
            _log :warn, 'wdm - running worker ...'
            worker.run!
          rescue LoadError
            _log :error, "wdm - worker run failed: #{$!}:#{$@.join("\n")}"
          end
        end
      rescue
        _log :error, "wdm - start failed: #{$!}:#{$@.join("\n")}"
        raise
      end

      private

      # Initializes a WDM monitor and adds a watcher for
      # each directory passed to the adapter.
      #
      # @return [WDM::Monitor] initialized worker
      #
      def _init_worker
        WDM::Monitor.new.tap do |worker|
          _directories_path.each do |path|
            _log :debug, "wdm - watching recursively: #{path.inspect}"

            worker.watch_recursively(path.to_s, :files,
                                     &_worker_file_callback)

            worker.watch_recursively(path.to_s, :directories,
                                     &_worker_dir_callback)

            worker.watch_recursively(path.to_s, :attributes, :last_write,
                                     &_worker_attr_callback)
          end
        end
      end

      def _worker_file_callback
        lambda do |change|
          begin
            path = _path(change.path)
            _log :debug, "wdm - FILE callback: #{change.inspect}"
            options = { type: 'File', change: _change(change.type) }
            _notify_change(path, options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _worker_attr_callback
        lambda do |change|
          begin
            path = _path(change.path)
            return if path.directory?

            _log :debug, "wdm - ATTR callback: #{change.inspect}"
            options = { type: 'File', change: _change(change.type) }
            _notify_change(_path(change.path), options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _worker_dir_callback
        lambda do |change|
          begin
            path = _path(change.path)
            _log :debug, "wdm - DIR callback: #{change.inspect}"
            if change.type == :removed
              _notify_change(path.dirname, type: 'Dir')
            elsif change.type == :added
              _notify_change(path, type: 'Dir')
            else
              # do nothing - changed directory means either:
              #   - removed subdirs (handled above)
              #   - added subdirs (handled above)
              #   - removed files (handled by _worker_file_callback)
              #   - added files (handled by _worker_file_callback)
              # so what's left?
            end
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      rescue
        _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
        raise
      end

      def _path(path)
{'fraction_non_alphanumeric': 0.10175538746967318, 'fraction_numerical': 0.0011417154274297131, 'mean_word_length': 1.5064377682403434, 'pattern_counts': {'":': 0, '<': 9, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412185', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1981, 'n_words': 519}