Perlcritic overhaul, add missing 'use module', update cpanfile, replace Mojo::Log with Log::Any

* Use more strict policy in perlcritic (add more policies)
* Add missing Use Encode and Use File::Spec in Craniac.pm
* Replace Mojo::Log with Log::Any, as Log::Any can catch stacktraces
* Update cpanfile
This commit is contained in:
eleksir
2022-02-12 18:22:01 +03:00
parent 5df11d2437
commit 622b690946
7 changed files with 98 additions and 83 deletions

View File

@@ -1,14 +1,13 @@
package Conf;
# loads config
# Загружает конфиг файл
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Encode qw (encode decode);
use JSON::XS;
use JSON::XS ();
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -16,17 +15,26 @@ our @EXPORT_OK = qw (LoadConf);
sub LoadConf {
my $c = 'data/config.json';
open my $CH, '<', $c or die "[FATAL] No conf at $c: $OS_ERROR\n";
my $json;
my $CH;
unless (open $CH, '<', $c) {
die "[FATAL] No conf at $c: $OS_ERROR\n";
}
my $len = (stat $c) [7];
my $json;
my $readlen = read $CH, $json, $len;
$json = encode 'UTF-8', $json;
unless ($readlen) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
die "[FATAL] Unable to read $c: $OS_ERROR\n";
}
if ($readlen != $len) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
die "[FATAL] File $c is $len bytes on disk, but we read only $readlen bytes\n";
}
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
my $j = JSON::XS->new->utf8->relaxed;
return $j->decode ($json);

View File

@@ -9,7 +9,7 @@ package Craniac;
# проигнорировать.
# Общие модули - синтаксис, кодировки итд
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -17,14 +17,17 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
# Модули для работы приложения
use Data::Dumper qw (Dumper);
use Digest::SHA qw (sha1_base64);
use Hailo;
use Mojo::IOLoop;
use Mojo::IOLoop::Signal;
use Mojo::Log;
use Encode qw (encode_utf8);
use File::Spec ();
use Hailo ();
use Log::Any qw ($log);
use Mojo::IOLoop ();
use Mojo::IOLoop::Signal ();
# Чтобы "уж точно" использовать hiredis-биндинги, загрузим этот модуль перед Mojo::Redis
use Protocol::Redis::XS;
use Mojo::Redis;
use Protocol::Redis::XS ();
use Mojo::Redis ();
use Math::Random::Secure qw (irand);
use Text::Fuzzy qw (distance_edits);
@@ -42,22 +45,7 @@ sub brains (@);
sub RunCraniac ();
my $c = LoadConf ();
my $loglevel = $c->{'loglevel'} // 'info';
my $fwd_cnt = $c->{'forward_max'} // 5;
my $logfile;
my $log;
if (defined $c->{'log'}) {
$logfile = $c->{'log'};
} elsif (defined $c->{'debug_log'}) {
$logfile = $c->{'debug_log'};
}
if (defined $logfile) {
$log = Mojo::Log->new (path => $logfile, level => $loglevel);
} else {
$log = Mojo::Log->new (path => '/dev/null', level => 'fatal');
}
my $hailo;
@@ -127,7 +115,7 @@ sub brains (@) {
$hailo->{$chatid} = eval {
Hailo->new (
brain => $brainname,
order => 3
order => 3,
);
};
@@ -267,8 +255,8 @@ my $parse_message = sub {
userid => $userid,
chatid => $chatid,
plugin => $m->{plugin},
message => $reply
}
message => $reply,
},
);
}
} else {
@@ -312,8 +300,8 @@ my $parse_message = sub {
userid => $userid,
chatid => $chatid,
plugin => $m->{plugin},
message => $reply
}
message => $reply,
},
);
return;
@@ -337,8 +325,8 @@ my $parse_message = sub {
userid => $userid,
chatid => $chatid,
plugin => $m->{plugin},
message => $reply
}
message => $reply,
},
);
return;
@@ -380,8 +368,8 @@ my $parse_message = sub {
userid => $userid,
chatid => $chatid,
plugin => $m->{plugin},
message => $reply
}
message => $reply,
},
);
}
}
@@ -408,7 +396,7 @@ my $__signal_handler = sub {
my ($self, $name) = @_;
$log->info ("[INFO] Caught a signal $name");
if (defined $main::pidfile && -f $main::pidfile) {
if (defined $main::pidfile && -e $main::pidfile) {
unlink $main::pidfile;
}
@@ -421,7 +409,7 @@ sub RunCraniac () {
$log->info ("[INFO] Connecting to $c->{server}, $c->{port}");
my $redis = Mojo::Redis->new (
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port}
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port},
);
$log->info ('[INFO] Registering connection-event callback');
@@ -438,11 +426,11 @@ sub RunCraniac () {
my ($conn, $error) = @_;
$log->error ("[ERROR] Redis connection error: $error");
return;
}
},
);
return;
}
},
);
my $pubsub = $redis->pubsub;
@@ -453,13 +441,13 @@ sub RunCraniac () {
$log->debug ("[DEBUG] Subscribing to $channel");
$sub->{$channel} = $pubsub->json ($channel)->listen (
$channel => sub { $parse_message->(@_); }
$channel => sub { $parse_message->(@_); },
);
}
Mojo::IOLoop::Signal->on (
TERM => $__signal_handler,
INT => $__signal_handler
INT => $__signal_handler,
);
do { Mojo::IOLoop->start } until Mojo::IOLoop->is_running;

View File

@@ -1,7 +1,7 @@
package Craniac::Lat;
# Крылатые латинские выражения спёрты с https://ru.wikipedia.org/wiki/Список_крылатых_латинских_выражений
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -10,8 +10,8 @@ use English qw ( -no_match_vars );
use Carp qw (cluck);
use File::Basename qw (dirname);
use File::Path qw (make_path);
use Hailo;
use Mojo::Log;
use Hailo ();
use Log::Any qw ($log);
use Conf qw (LoadConf);
@@ -25,20 +25,6 @@ my $brain_order = 2;
my $c = LoadConf ();
my $brain = $c->{lat}->{brain};
my $srcfile = $c->{lat}->{src};
my $loglevel = $c->{'loglevel'} // 'info';
my $log;
if (defined $c->{'log'}) {
$logfile = $c->{'log'};
} elsif (defined $c->{'debug_log'}) {
$logfile = $c->{'debug_log'};
}
if (defined $logfile) {
$log = Mojo::Log->new (path => $logfile, level => $loglevel);
} else {
$log = Mojo::Log->new (path => '/dev/null', level => 'fatal');
}
sub Train () {
my $braindir = dirname $brain;
@@ -52,7 +38,7 @@ sub Train () {
my $lat = Hailo->new (
brain => $brain,
order => $brain_order
order => $brain_order,
);
$lat->train ($srcfile);
@@ -71,7 +57,7 @@ sub Lat () {
return '';
}
unless (-f $brain) {
unless (-e $brain) {
$log->error ("[ERROR] No lat module data: $brain is absent! Train lat first.");
return '';
}
@@ -79,7 +65,7 @@ sub Lat () {
my $lat = Hailo->new (
brain => $brain,
order => $brain_order,
save_on_exit => 0
save_on_exit => 0,
);
return $lat->reply ();