| 1 | package App::Automaton; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ABSTRACT: Execute various tasks based on input from various sources | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use YAML::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Module::Load; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | has conf => ( is => 'rw' ); | |||||||||||||
| 15 | 1 | has yaml_conf => ( is => 'rw' ); | |||||||||||||
| 16 | 1 | has conf_file => ( is => 'rw' ); | |||||||||||||
| 17 | 1 | has found_bits => ( is => 'rw' ); | |||||||||||||
| 18 | 1 | has debug => ( is => 'rw'); | |||||||||||||
| 19 | |||||||||||||||
| 20 | 2 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 21 | 2 | my $self = shift; | |||||||||||||
| 22 | 2 | $self->load_conf(); | |||||||||||||
| 23 | 2 | return 1; | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 2 | ▶ | sub load_conf { | ||||||||||||
|
Subroutine
load_conf: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 27 | 2 | my $self = shift; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 2 | ▶ | if ( $self->{yaml_conf} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 30 | 1 | $self->{conf} = YAML::Tiny::Load($self->{yaml_conf}); | |||||||||||||
| 31 | } elsif ( $self->{conf_file} ) { | ||||||||||||||
| 32 | 1 | ▶ | $self->{conf} = YAML::Tiny::LoadFile($self->{conf_file}) or die; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | $self->logger('Loaded config'); | |||||||||||||
| 36 | 2 | return $self->{conf}; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | sub check_sources { | ||||||||||||
|
Subroutine
check_sources: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | my $sources = $self->{conf}->{sources}; | |||||||||||||
| 43 | 0 | foreach my $name (keys %$sources) { | |||||||||||||
| 44 | 0 | my $source = $sources->{$name}; | |||||||||||||
| 45 | 0 | ▶ | next if $source->{bypass}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | $source->{debug} = $self->{debug}; | |||||||||||||
| 47 | 0 | $self->logger("checking source: $name"); | |||||||||||||
| 48 | 0 | my $mod = 'App::Automaton::Plugin::Source::' . $source->{type}; | |||||||||||||
| 49 | 0 | load $mod; | |||||||||||||
| 50 | 0 | my $s = eval {$mod->new()}; | |||||||||||||
| 51 | 0 | ▶ | die $! unless $s; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 0 | ▶ | die $! unless $s->can('go'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | push(@{$self->{found_bits}}, $s->go($source)); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | return $1; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | ▶ | sub apply_filters { | ||||||||||||
|
Subroutine
apply_filters: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | my $filters = $self->{conf}{filters}; | |||||||||||||
| 63 | 0 | foreach my $name (keys %{$filters}) { | |||||||||||||
| 64 | 0 | my $filter = $filters->{$name}; | |||||||||||||
| 65 | 0 | ▶ | next if $filter->{bypass}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | $filter->{debug} = $self->{debug}; | |||||||||||||
| 67 | 0 | $self->logger("Applying filter: $name"); | |||||||||||||
| 68 | 0 | my $mod = 'App::Automaton::Plugin::Filter::' . $filter->{type}; | |||||||||||||
| 69 | 0 | load $mod; | |||||||||||||
| 70 | 0 | my $a = eval {$mod->new()}; | |||||||||||||
| 71 | 0 | ▶ | die $! unless $a; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | ▶ | die $! unless $a->can('go'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | $a->go($filter, $self->{found_bits}); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | return 1; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | ▶ | sub do_actions { | ||||||||||||
|
Subroutine
do_actions: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||||
| 81 | # process each action for each buffer | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | my $actions = $self->{conf}{actions}; | |||||||||||||
| 84 | 0 | foreach my $name (keys %$actions) { | |||||||||||||
| 85 | 0 | my $action = $actions->{$name}; | |||||||||||||
| 86 | 0 | ▶ | next if $action->{bypass}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | $action->{debug} = $self->{debug}; | |||||||||||||
| 88 | 0 | $self->logger("Executing action: $name"); | |||||||||||||
| 89 | 0 | my $mod = 'App::Automaton::Plugin::Action::' . $action->{type}; | |||||||||||||
| 90 | 0 | load $mod; | |||||||||||||
| 91 | 0 | my $a = eval {$mod->new()}; | |||||||||||||
| 92 | 0 | ▶ | die $! unless $a; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | ▶ | die $! unless $a->can('go'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 0 | my $r = $a->go($action, $self->found_bits()); | |||||||||||||
| 95 | 0 | ▶ | $self->logger("Unsuccessful return from action: $name") unless $r; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | return 1; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 1 | ▶ | sub dedupe { | ||||||||||||
|
Subroutine
dedupe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 102 | 1 | my $self = shift; | |||||||||||||
| 103 | 1 | my %hash; | |||||||||||||
| 104 | 1 | $self->logger("Removing duplicates"); | |||||||||||||
| 105 | 1 | @hash{@{$self->{found_bits}}} = (); | |||||||||||||
| 106 | 1 | @{$self->{found_bits}} = keys %hash; | |||||||||||||
| 107 | 1 | return 1; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 3 | ▶ | sub logger { | ||||||||||||
|
Subroutine
logger: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||
| 111 | 3 | my $self = shift; | |||||||||||||
| 112 | 3 | my $message = shift; | |||||||||||||
| 113 | 3 | ▶ | if ($self->{debug}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 0 | print STDERR "$message\n"; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | 3 | return 1; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 1 | 1; | |||||||||||||
| 120 | |||||||||||||||
| 121 | __END__ | ||||||||||||||