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