<commit_before>require 'yaml'
require 'log4r'
require 'breaktime/schedule'
require 'dante'
require 'breaktime/cli'

# Handles configuration options, CLI stuff and process daemonizing.
class Breaktime::Main
  include Log4r

  attr_reader :options, :log, :cli

  # Default options that can be overridden in the YAML file.
  DEFAULT_OPTIONS = {'interval' => 60,
                     'pid_path' => ENV['HOME'] + File::SEPARATOR + "breaktime.pid",
                     'log_path' => ENV['HOME'] + File::SEPARATOR + "breaktime.log",
                     'daemonize' => true,
                     'days' => ['monday',
                                'tuesday',
                                'wednesday',
                                'thursday',
                                'friday',
                                'saturday',
                                'sunday']}

  # Set up the logger, parse CLI options and the YAML file.
  def initialize
    create_logger 'error'
    @cli = Breaktime::CLI.new
    @options = DEFAULT_OPTIONS
    set_log_level @cli.options[:level]

    parse_yaml_file
  end

  # Exit with a trollop message.
  def die(message)
    Trollop::die message
  end

  # Print out the gem motto and exit with the given exit code.
  def say_goodbye(exit_code)
    puts "\n\s\sBreaktime says, \"Have a break, have an unbranded chocolate snack.\""
    exit exit_code
  end

  # Run the given mode.
  #
  # The mode is one of the command line modes (run with the --help flag).
  def run_mode(mode)
    command = Breaktime::Command.new @options['command'], @log

    # Switch on CLI mode.
    case mode
    # Schedule the breaktime.
    when "start"
      @log.info { "When it's breaktime I'll run: `#{command.command}`" }
      startd

    # Stop a currently running daemonized process.
    when "stop"
      @log.info { "Stopping breaktime background process" }
      stopd
      say_goodbye Breaktime::EX_OK

    # Open a dialog to notify the user about an impending screen break.
    when "dialog"
			@log.info { "Opening dialog" }
      require 'breaktime/dialog'
      # Automatically loads green shoes window

    # Run the command that will start the break.
    when "now"
      command.execute

    # Unknown mode.
    else
      die "unknown mode #{mode.inspect}"

    end
  end

  # Start the scheduler as a daemon.
  #
  # The process can be kept on top if the "daemonize" option is set to be false
  # in the configuration YAML file.
  #
  # The logger output format is changed to add the time, as this is more
  # helpful for debugging.
  #
  # Uses Dante for daemonizing.
  def startd
    dante_opts = {:daemonize => @options['daemonize'], 
                  :pid_path => @options['pid_path'], 
                  :log_path => @options['log_path']}

    if dante_opts[:daemonize]
      @log.info { "Starting daemon, PID file => #{dante_opts[:pid_path]}, log file => #{dante_opts[:log_path]}" }
    end

    schedule = Breaktime::Schedule.new(@options['interval'], @options['days'], @cli.options, @log)

    Dante::Runner.new('breaktime').execute(dante_opts) do
      schedule.start
    end
  end<commit_msg>Check to see whether fork() is implemented, fix #2<commit_after>require 'yaml'
require 'log4r'
require 'breaktime/schedule'
require 'dante'
require 'breaktime/cli'

# Handles configuration options, CLI stuff and process daemonizing.
class Breaktime::Main
  include Log4r

  attr_reader :options, :log, :cli

  # Default options that can be overridden in the YAML file.
  DEFAULT_OPTIONS = {'interval' => 60,
                     'pid_path' => ENV['HOME'] + File::SEPARATOR + "breaktime.pid",
                     'log_path' => ENV['HOME'] + File::SEPARATOR + "breaktime.log",
                     'daemonize' => true,
                     'days' => ['monday',
                                'tuesday',
                                'wednesday',
                                'thursday',
                                'friday',
                                'saturday',
                                'sunday']}

  # Set up the logger, parse CLI options and the YAML file.
  def initialize
    create_logger 'error'
    @cli = Breaktime::CLI.new
    @options = DEFAULT_OPTIONS
    set_log_level @cli.options[:level]

    parse_yaml_file
  end

  # Exit with a trollop message.
  def die(message)
    Trollop::die message
  end

  # Print out the gem motto and exit with the given exit code.
  def say_goodbye(exit_code)
    puts "\n\s\sBreaktime says, \"Have a break, have an unbranded chocolate snack.\""
    exit exit_code
  end

  # Run the given mode.
  #
  # The mode is one of the command line modes (run with the --help flag).
  def run_mode(mode)
    command = Breaktime::Command.new @options['command'], @log

    # Switch on CLI mode.
    case mode
    # Schedule the breaktime.
    when "start"
      @log.info { "When it's breaktime I'll run: `#{command.command}`" }
      startd

    # Stop a currently running daemonized process.
    when "stop"
      @log.info { "Stopping breaktime background process" }
      stopd
      say_goodbye Breaktime::EX_OK

    # Open a dialog to notify the user about an impending screen break.
    when "dialog"
			@log.info { "Opening dialog" }
      require 'breaktime/dialog'
      # Automatically loads green shoes window

    # Run the command that will start the break.
    when "now"
      command.execute

    # Unknown mode.
    else
      die "unknown mode #{mode.inspect}"

    end
  end

  # Check if Process has the fork method, but also check for Java
  #
  # Apparently jRuby doesn't implement this correctly, hence the secondary
  # check.
  def fork_implemented?
    if Process.respond_to?(:fork)
      RUBY_PLATFORM !~ /java/ && RUBY_ENGINE != 'macruby'
    else
      false
    end
  end

  # Start the scheduler as a daemon.
  #
  # The process can be kept on top if the "daemonize" option is set to be false
  # in the configuration YAML file.
  #
  # The logger output format is changed to add the time, as this is more
  # helpful for debugging.
  #
  # Uses Dante for daemonizing.
  def startd
    dante_opts = {:daemonize => @options['daemonize'], 
                  :pid_path => @options['pid_path'], 
                  :log_path => @options['log_path']}

    if dante_opts[:daemonize]
      if not fork_implemented?
        dante_opts[:daemonize] = false
        @log.warn { "Daemonizing is not available, as fork is not implemented - running on top" }
      else
        @log.info { "Starting daemon, PID file => #{dante_opts[:pid_path]}, log file => #{dante_opts[:log_path]}" }
      end
    end

    schedule = Breaktime::Schedule.new(@options['interval'], @options['days'], @cli.options, @log)

    Dante::Runner.new('breaktime').execute(dante_opts) do
      schedule.start
    end
  end
{'fraction_non_alphanumeric': 0.10117474302496329, 'fraction_numerical': 0.0013215859030837004, 'mean_word_length': 2.26353617632966, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7019216', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1994, 'n_words': 661}