<commit_before>package App::MPDJ;

use strict;
use warnings;
use 5.010;

our $VERSION = '1.06';

use Getopt::Long;
use Net::MPD;
use Proc::Daemon;
use Log::Dispatch::Screen;
use Log::Dispatch::Syslog;

sub new {
  my ($class, @options) = @_;

  my $self = bless {
    action     => undef,
    after      => 2,
    before     => 2,
    calls_path => 'calls',
    calls_freq => 3600,
    daemon     => 1,
    last_call  => 0,
    mpd        => undef,
    mpd_conn   => 'localhost',
    music_path => 'music',
    @options
  }, $class;
}

sub mpd {
  my ($self) = @_;

  $self->{mpd};
}

sub parse_options {
  my ($self, @options) = @_;

  local @ARGV = @options;

  Getopt::Long::Configure('bundling');
  Getopt::Long::GetOptions(
    'D|daemon!'      => \$self->{daemon},
    'V|version'      => sub { $self->{action} = 'show_version' },
    'a|after=i'      => \$self->{after},
    'b|before=i'     => \$self->{before},
    'calls-path=s'   => \$self->{calls_path},
    'c|calls-freq=i' => \$self->{calls_freq},
    'h|help'         => sub { $self->{action} = 'show_help' },
    'mpd=s'          => \$self->{mpd_conn},
    'music-path=s'   => \$self->{music_path},
    's|syslog=s'     => \$self->{syslog},
    'l|conlog=s'     => \$self->{conlog},
  );
}

sub connect {
  my ($self) = @_;

  $self->{mpd} = Net::MPD->connect($self->{mpd_conn});
}

sub execute {
  my ($self) = @_;

  if (my $action = $self->{action}) {
    $self->$action() and return 1;
  }

  @SIG{qw( INT TERM HUP )} = sub { $self->safe_exit() };

  $self->{log} = Log::Dispatch->new();

  if ($self->{conlog}) {
    $self->{log}->add(
      Log::Dispatch::Screen->new(
          name      => 'screen',
          min_level => $self->{conlog},
	  newline   => 1,
      )
    )
  }

  if ($self->{syslog}) {
    $self->{log}->add(
      Log::Dispatch::Syslog->new(
          name      => 'syslog',
          min_level => $self->{syslog},
	  ident     => 'mpdj',
      )
    )
  }

  if ($self->{daemon}) {
    $self->{log}->notice('Forking to background');
    Proc::Daemon::Init;
  }

  $self->connect;
  $self->configure;

  $self->mpd->subscribe('mpdj');

  $self->update_cache;

  while (1) {
    $self->{log}->debug('Waiting');
    my @changes = $self->mpd->idle(qw(database player playlist message options));
    $self->mpd->update_status();

    foreach my $subsystem (@changes) {
      my $function = $subsystem . '_changed';
      $self->$function();
    }
  }
}

sub configure {
  my ($self) = @_;

  $self->{log}->notice('Configuring MPD server');

  $self->mpd->repeat(0);<commit_msg>less verbose instantiation of log objects<commit_after>package App::MPDJ;

use strict;
use warnings;
use 5.010;

our $VERSION = '1.06';

use Getopt::Long;
use Net::MPD;
use Proc::Daemon;
use Log::Dispatch;

sub new {
  my ($class, @options) = @_;

  my $self = bless {
    action     => undef,
    after      => 2,
    before     => 2,
    calls_path => 'calls',
    calls_freq => 3600,
    daemon     => 1,
    last_call  => 0,
    mpd        => undef,
    mpd_conn   => 'localhost',
    music_path => 'music',
    @options
  }, $class;
}

sub mpd {
  my ($self) = @_;

  $self->{mpd};
}

sub parse_options {
  my ($self, @options) = @_;

  local @ARGV = @options;

  Getopt::Long::Configure('bundling');
  Getopt::Long::GetOptions(
    'D|daemon!'      => \$self->{daemon},
    'V|version'      => sub { $self->{action} = 'show_version' },
    'a|after=i'      => \$self->{after},
    'b|before=i'     => \$self->{before},
    'calls-path=s'   => \$self->{calls_path},
    'c|calls-freq=i' => \$self->{calls_freq},
    'h|help'         => sub { $self->{action} = 'show_help' },
    'mpd=s'          => \$self->{mpd_conn},
    'music-path=s'   => \$self->{music_path},
    's|syslog=s'     => \$self->{syslog},
    'l|conlog=s'     => \$self->{conlog},
  );
}

sub connect {
  my ($self) = @_;

  $self->{mpd} = Net::MPD->connect($self->{mpd_conn});
}

sub execute {
  my ($self) = @_;

  if (my $action = $self->{action}) {
    $self->$action() and return 1;
  }

  @SIG{qw( INT TERM HUP )} = sub { $self->safe_exit() };


  my @loggers;
  push @loggers, ( [ 'Screen',   min_level => $self->{conlog}, newline => 1    ] ) if $self->{conlog};
  push @loggers, ( [ 'Syslog',   min_level => $self->{syslog}, ident => 'mpdj' ] ) if $self->{syslog};

  $self->{log} = Log::Dispatch->new( outputs => \@loggers );

  if ($self->{daemon}) {
    $self->{log}->notice('Forking to background');
    Proc::Daemon::Init;
  }

  $self->connect;
  $self->configure;

  $self->mpd->subscribe('mpdj');

  $self->update_cache;

  while (1) {
    $self->{log}->debug('Waiting');
    my @changes = $self->mpd->idle(qw(database player playlist message options));
    $self->mpd->update_status();

    foreach my $subsystem (@changes) {
      my $function = $subsystem . '_changed';
      $self->$function();
    }
  }
}

sub configure {
  my ($self) = @_;

  $self->{log}->notice('Configuring MPD server');

  $self->mpd->repeat(0);
{'fraction_non_alphanumeric': 0.22523608599557968, 'fraction_numerical': 0.007635121559172192, 'mean_word_length': 2.665684830633284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 148, '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': '456311', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1924, 'n_words': 356}