| 1 | package Zoidberg::PluginHash; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 64 | our $VERSION = '0.981'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 64 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 64 | ▶ | use Zoidberg::Utils qw/:default read_file merge_hash list_dir/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # $self->[0] = plugin objects hash | ||||||||||||||
| 9 | # $self->[1] = plugin meta data hash | ||||||||||||||
| 10 | # $self->[2] = parent zoid | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 61 | ▶ | sub TIEHASH { | ||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 61 | my ($class, $zoid) = @_; | |||||||||||||
| 14 | 61 | my $self = [{}, {}, $zoid]; | |||||||||||||
| 15 | 61 | bless $self, $class; | |||||||||||||
| 16 | 61 | $self->hash; | |||||||||||||
| 17 | 61 | return $self; | |||||||||||||
| 18 | } | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 46 | ▶ | sub FETCH { | ||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=15.5
| |||||||||||||||
| 21 | 46 | my ($self, $key) = @_; | |||||||||||||
| 22 | |||||||||||||||
| 23 | 46 | ▶ | return $self->[0]{$key} if exists $self->[0]{$key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 16 | ▶ | unless ($self->[1]{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | my @caller = caller; | |||||||||||||
| 27 | 0 | error "No such object \'$key\' as requested by $caller[1] line $caller[2]"; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 0 | ▶ | $self->load($key) or return sub { undef }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||
| 31 | 16 | return $self->[0]{$key}; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 80 | ▶ | sub STORE { | ||||||||||||
|
Subroutine
STORE: called CC=20 SCAR=34.8
| |||||||||||||||
| 35 | 80 | my ($self, $name, $ding) = @_; | |||||||||||||
| 36 | 80 | ▶ | my $data = ref($ding) ? $ding : { config_file => $ding, %{read_file($ding)} } ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | 80 | ▶ | if (exists $$data{object}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | $$data{object}{zoidname} = $name | ||||||||||||||
| 40 | 0 | ▶ | if eval{ $$data{object}->isa( 'Zoidberg::Fish' ) }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | $self->[0]{$name} = $$data{object} | ||||||||||||||
| 42 | 0 | } | |||||||||||||
| 43 | |||||||||||||||
| 44 | # settings && aliases | ||||||||||||||
| 45 | 80 | for my $t (qw/settings aliases/) { | |||||||||||||
| 46 | 160 | $$self[2]{$t}{$_} = $$data{$t}{$_} for keys %{$$data{$t}}; | |||||||||||||
| 47 | 160 | delete $$data{$t}; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | # config | ||||||||||||||
| 51 | $self->[2]{settings}{$name} = merge_hash( | ||||||||||||||
| 52 | $$data{config}, | ||||||||||||||
| 53 | 80 | ▶ | $self->[2]{settings}{$name} | ||||||||||||
|
Condition50%1/2
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name}) || {}
MC/DC0%0/1
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name}) || {}
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name})
Truth table50%1/2
A
A= merge_hash($data->{'config'}, $self->[2]{'settings'}{$name})
| |||||||||||||||
| 54 | ) || {}; | ||||||||||||||
| 55 | 80 | delete $$data{config}; | |||||||||||||
| 56 | |||||||||||||||
| 57 | # commands | ||||||||||||||
| 58 | 80 | for (keys %{$$data{commands}}) { | |||||||||||||
| 59 | $$data{commands}{$_} =~ s/^(\w)/->$name->$1/ | ||||||||||||||
| 60 | 32 | ▶ | unless ref $$data{commands}{$_}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | } | ||||||||||||||
| 62 | 80 | ▶ | if (exists $$data{export}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | $$data{commands}{$_} = "->$name->$_" | ||||||||||||||
| 64 | 64 | for @{$$data{export}}; | |||||||||||||
| 65 | 64 | delete $$data{export}; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | 80 | my ($c, $s); | |||||||||||||
| 68 | 80 | ▶ | while( ($c, $s) = each %{$$data{commands}} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 624 | $self->[2]{commands}{$c} = [$s, $name]; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | 80 | delete $$data{commands}; | |||||||||||||
| 72 | |||||||||||||||
| 73 | # events | ||||||||||||||
| 74 | 80 | for (keys %{$$data{events}}) { | |||||||||||||
| 75 | $$data{events}{$_} =~ s/^(\w)/->$name->$1/ | ||||||||||||||
| 76 | 16 | ▶ | unless ref $$data{events}{$_}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | } | ||||||||||||||
| 78 | 80 | ▶ | if (exists $$data{import}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | $$data{events}{$_} = "->$name->$_" | ||||||||||||||
| 80 | 0 | for @{$$data{import}}; | |||||||||||||
| 81 | 0 | delete $$data{import}; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | 80 | ▶ | while( ($c, $s) = each %{$$data{events}} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 16 | $self->[2]{events}{$c} = [$s, $name]; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | 80 | delete $$data{events}; | |||||||||||||
| 87 | |||||||||||||||
| 88 | # parser | ||||||||||||||
| 89 | 80 | ▶ | if (exists $$data{parser}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 0 | require Zoidberg::Fish; | |||||||||||||
| 91 | 0 | ▶ | my @c = (ref($$data{parser}) eq 'ARRAY') ? (@{$$data{parser}}) : ($$data{parser}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 0 | Zoidberg::Fish::add_context({zoidname => $name, shell => $$self[2]}, $_) for @c; | |||||||||||||
| 93 | 0 | delete $$data{parser}; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 80 | $self->[1]{$name} = $data; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 64 | our @_keys; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | ▶ | sub FIRSTKEY { @_keys = keys %{$_[0][1]}; shift @_keys } | ||||||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | ▶ | sub NEXTKEY { shift @_keys } | ||||||||||||
|
Subroutine
NEXTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||
| 104 | |||||||||||||||
| 105 | 137 | ▶ | sub EXISTS { exists $_[0][1]->{$_[1]} } | ||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | ▶ | sub DELETE { # leaves config intact | ||||||||||||
|
Subroutine
DELETE: not called CC=3 SCAR=24.8
| |||||||||||||||
| 108 | 0 | my ($self, $key) = @_; | |||||||||||||
| 109 | 0 | ▶ | $$self[0]{$key}->round_up() if eval { $self->[0]{$key}->isa( 'Zoidberg::Fish' ) }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 0 | delete $$self[0]{$key}; | |||||||||||||
| 111 | 0 | $$self[2]{$_}->wipe($key) for qw/events commands/; # wipe DispatchTable stacks | |||||||||||||
| 112 | 0 | $$self[2]->broadcast('unplug_'.$key); | |||||||||||||
| 113 | 0 | return $$self[1]{$key}; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | ▶ | sub CLEAR { $_[0]->DELETE($_) for keys %{$_[0][1]} } | ||||||||||||
|
Subroutine
CLEAR: not called CC=2 SCAR=17.9
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | 61 | ▶ | sub hash { | ||||||||||||
|
Subroutine
hash: called CC=11 SCAR=28.4
Pod
undocumented
| |||||||||||||||
| 119 | 61 | my $self = shift; | |||||||||||||
| 120 | |||||||||||||||
| 121 | # TODO how about an ignore list for users who disagree with there admin ? | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 61 | $self->[1] = {}; | |||||||||||||
| 124 | 61 | for my $dir (map "$_/plugins", @{$self->[2]{settings}{data_dirs}}) { | |||||||||||||
| 125 | 61 | ▶ | next unless -d $dir; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 16 | for (list_dir($dir)) { | |||||||||||||
| 127 | 80 | ▶ | /^(\w+)/ || next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 80 | my ($name, $ding) = ($1, "$dir/$_"); | |||||||||||||
| 129 | 80 | ▶ | next if exists $$self[1]{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 80 | ▶ | if (-d "$dir/$_") { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 131 | 0 | my ($conf) = grep /^PluginConf/, list_dir("$dir/$_"); | |||||||||||||
| 132 | 0 | ▶ | next unless $conf; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 0 | unshift @INC, "$dir/$_"; | |||||||||||||
| 134 | 0 | ▶ | unshift @{$self->[2]{settings}{data_dirs}}, "$dir/$_/data" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | if -d "$dir/$_/data"; | ||||||||||||||
| 136 | 0 | $ding = "$dir/$_/$conf"; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | elsif (/.pm$/) { | ||||||||||||||
| 139 | 16 | my $class = $_; | |||||||||||||
| 140 | 16 | $class =~ s/.pm$//; | |||||||||||||
| 141 | 16 | $ding = {module => $class, pmfile => "$dir/$_"}; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | 80 | eval { $self->STORE($name, $ding) }; | |||||||||||||
| 144 | 80 | ▶ | complain if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | } | ||||||||||||||
| 146 | } | ||||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 16 | ▶ | sub load { | ||||||||||||
|
Subroutine
load: called CC=7 SCAR=24.7
Pod
undocumented
| |||||||||||||||
| 150 | 16 | my ($self, $zoidname, @args) = @_; | |||||||||||||
| 151 | 16 | my $class = $$self[1]{$zoidname}{module}; | |||||||||||||
| 152 | 16 | ▶ | unless ($class) { # FIXME is this allright and does it belong in this package ? | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | $self->[0]{$zoidname} = { | ||||||||||||||
| 154 | shell => $self->[2], | ||||||||||||||
| 155 | zoidname => $zoidname, | ||||||||||||||
| 156 | settings => $self->[2]->{settings}, | ||||||||||||||
| 157 | 0 | config => $self->[2]->{settings}{$zoidname}, | |||||||||||||
| 158 | }; | ||||||||||||||
| 159 | 0 | debug "Loaded stub plugin $zoidname"; | |||||||||||||
| 160 | 0 | $$self[2]->broadcast('plug_'.$zoidname); | |||||||||||||
| 161 | 0 | return $self->[0]{$zoidname}; | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 16 | my $req = $class; | |||||||||||||
| 165 | 16 | ▶ | $req = '\''.$$self[1]{$zoidname}{pmfile}.'\'' if exists $$self[1]{$zoidname}{pmfile}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 16 | debug "Going to load plugin $zoidname of class $class, requiring $req"; | |||||||||||||
| 167 | 16 | eval "require $req"; | |||||||||||||
| 168 | 16 | ▶ | eval { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 16 | ▶ | if (eval{ $class->isa( 'Zoidberg::Fish' ) }) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 170 | 16 | $self->[0]{$zoidname} = $class->new($self->[2], $zoidname); | |||||||||||||
| 171 | 16 | $self->[0]{$zoidname}->init(@args); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | 0 | elsif ($class->can('new')) { $self->[0]{$zoidname} = $class->new(@args) } | |||||||||||||
| 174 | 0 | else { error "Module $class doesn't seem to be Object Oriented" } | |||||||||||||
| 175 | } unless $@; | ||||||||||||||
| 176 | 16 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | 0 | $@ =~ s/\n$/ /; | |||||||||||||
| 178 | 0 | complain "Failed to load class: $class ($@)\nDisabling plugin: $zoidname"; | |||||||||||||
| 179 | 0 | $self->DELETE($zoidname); | |||||||||||||
| 180 | 0 | delete $$self[1]{$zoidname}; | |||||||||||||
| 181 | 0 | return undef; | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | else { | ||||||||||||||
| 184 | 16 | debug "Loaded plugin $zoidname"; | |||||||||||||
| 185 | 16 | $$self[2]->broadcast('plug_'.$zoidname); | |||||||||||||
| 186 | 16 | return $self->[0]{$zoidname}; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 3 | ▶ | sub round_up { | ||||||||||||
|
Subroutine
round_up: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||
| 191 | 3 | my $self = shift; | |||||||||||||
| 192 | 3 | for (keys %{$$self[0]}) { | |||||||||||||
| 193 | $$self[0]{$_}->round_up(@_) | ||||||||||||||
| 194 | 2 | ▶ | if eval{ $$self[0]{$_}->isa( 'Zoidberg::Fish' ) }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | } | ||||||||||||||
| 196 | } | ||||||||||||||
| 197 | |||||||||||||||
| 198 | 64 | 1; | |||||||||||||
| 199 | |||||||||||||||
| 200 | __END__ | ||||||||||||||