blib/lib/Bio/Graphics/Browser2/PluginSet.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 294 CC 31 SCAR 36.8
CC
31
Cov
0%
new
110.0
configure
56.0
set_filters
30.0
CRAP 39.6
1 package Bio::Graphics::Browser2::PluginSet;
2 # API for using plugins
3  
4 # $Id$
5  
6 use strict;
7 use Bio::Graphics::Browser2;
8 use Bio::Graphics::Browser2::Util 'shellwords';
9 use CGI 'param';
10 use constant DEBUG=>0;
11  
12 sub new {
13 my $package = shift;
14 my $config = shift;
15 my @search_path = @_;
16 my %plugin_list = ();
17  
18 warn "initializing plugins with $config..." if DEBUG;
19 my @plugins = shellwords($config->plugins);
20 # only one authorization plugin allowed, from globals
21 if (my $auth = $config->auth_plugin) {
22 unshift @plugins,$auth; # first one
23 }
24 warn "PLUGINS = @plugins" if DEBUG;
25  
26 PLUGIN:
27 for my $plugin (@plugins) {
28 my $class = "Bio\:\:Graphics\:\:Browser2\:\:Plugin\:\:$plugin";
29 for my $search_path (@search_path) {
30 my $plugin_with_path = "$search_path/$plugin.pm";
31 if (eval {require $plugin_with_path}) {
32 warn "plugin $plugin loaded successfully" if DEBUG;
33 my $obj = eval{$class->new};
34 unless ($obj) {
35 warn "$plugin: $@";
36 next PLUGIN;
37 }
38 warn "plugin name = ",$obj->name," base = $plugin" if DEBUG;
39 $plugin_list{$plugin} = $obj;
40 next PLUGIN;
41 } else {
42 warn $@ if $@ and $@ !~ /^Can\'t locate/;
43 }
44 }
45 warn $@ if !$plugin_list{$plugin} && $@ =~ /^Can\'t locate/;
46 }
47 my $self = bless {
48 config => $config,
49 plugins => \%plugin_list
50 },ref $package || $package;
51 return $self;
52 }
53  
54 sub config { shift->{config} }
55 sub plugins {
56 my $self = shift;
57 return wantarray ? values %{$self->{plugins}} : $self->{plugins};
58 }
59  
60 sub plugin {
61 my $self = shift;
62 my $plugin_base = shift;
63 $self->plugins->{$plugin_base};
64 }
65  
66 sub language {
67 my $self = shift;
68 my $d = $self->{language};
69 $self->{language} = shift if @_;
70 $d;
71 }
72  
73 sub auth_plugin {
74 my $self = shift;
75 my @a = grep {$_->type eq 'authenticator'} values %{$self->{plugins}};
76 return unless @a;
77 return $a[0];
78 }
79  
80 sub configure {
81 my $self = shift;
82 my $render = shift;
83  
84 my $database = $render->db;
85 my $page_settings = $render->state;
86 my $language = $render->language;
87 my $session = $render->session;
88 my $search = $render->get_search_object;
89  
90 my $conf = $self->config;
91 my $plugins = $self->plugins;
92 my $conf_dir = $conf->globals->config_base;
93 $self->language($language);
94  
95 for my $name (keys %$plugins) {
96  
97 eval {
98 my $p = $plugins->{$name};
99 $p->renderer($render);
100 $p->database($database);
101 $p->browser_config($conf);
102 $p->config_path($conf_dir);
103 $p->language($language);
104 $p->page_settings($page_settings);
105 $p->db_search($search);
106 $p->init(); # other initialization
107  
108 # retrieve persistent configuration
109 my $config = $session->plugin_settings($p->name);
110 unless (%$config) {
111 my $defaults = $p->config_defaults;
112 %$config = %{$defaults} if $defaults;
113 }
114  
115 # and tell the plugin about it
116 $p->configuration($config);
117  
118 # if there are any CGI parameters from the
119 # plugin's configuration screen, set it here
120 if (my @params = grep {/^$name\./} param()) {
121 $p->reconfigure unless param('plugin_action') eq $language->tr('Cancel');
122  
123 # turn the plugin on
124 my $setting_name = 'plugin:'.$p->name;
125 $p->page_settings->{features}{$setting_name}{visible} = 1;
126 }
127  
128 if ($p->type eq 'authenticator') {
129 my $source = $self->config;
130 $source->set_authenticator($p);
131 $source->set_username($render->session->username);
132 }
133 };
134  
135 warn "$name: $@" if $@;
136 }
137  
138  
139 $self->set_filters(); # allow filter plugins to adjust the data source
140 }
141  
142 sub destroy {
143 my $self = shift;
144 my $plugins = $self->plugins;
145 for my $name (keys %$plugins) {
146 eval {
147 my $p = $plugins->{$name};
148 $p->renderer(undef);
149 };
150 }
151 }
152  
153 sub set_filters {
154 my $self = shift;
155 my $source = $self->config;
156  
157 my @labels = grep {!/^_/} $source->labels;
158 for my $p ($self->plugins) {
159 next unless $p->type eq 'filter';
160 for my $l (@labels) {
161 $self->{'.ok'}{$l} ||= $source->setting($l,'key'); # remember this!
162 $self->{'.of'}{$l} ||= $source->setting($l,'filter'); # remember this!
163
164 if (my ($filter,$new_key) = $p->filter($l,$self->{'.ok'}{$l})) {
165 $source->set($l, filter => $filter);
166 $source->set($l, key => $new_key);
167 }
168 else {
169 $source->set($l, key => $self->{'.ok'}{$l}) if exists $self->{'.ok'}{$l};
170 $source->set($l, filter => $self->{'.of'}{$l}) if exists $self->{'.of'}{$l};
171 }
172 }
173 }
174 }
175  
176 sub annotate {
177 my $self = shift;
178 my $segment = shift;
179 my $feature_files = shift || {};
180 my $fast_mapper = shift; # fast mapper filters out features that are outside cur segment
181 my $slow_mapper = shift; # slow mapper doesn't
182 my $max_segment = shift; # ignored
183 my $whole_segment = shift;
184 my $region_segment = shift;
185  
186 my @plugins = $self->plugins;
187  
188 for my $p (@plugins) {
189 next unless $p->type eq 'annotator';
190 my $name = "plugin:".$p->id;
191 next unless $p->page_settings && $p->page_settings->{features}{$name}{visible};
192 warn "Plugin $name is visible, so running it on segment $segment" if DEBUG;
193 if ($segment->length > $max_segment) {
194 $feature_files->{$name} = Bio::Graphics::FeatureFile->new(); # empty
195 } else {
196 my $features = $p->annotate($segment,$fast_mapper) or next;
197 $features->name($name);
198 $feature_files->{$name} = $features;
199 }
200 }
201 }
202  
203 sub set_segments {
204 my $self = shift;
205 my $segments = shift;
206  
207 my $plugins = $self->plugins;
208 for my $k ( values %$plugins ) {
209 $k->segments($segments);
210 }
211 }
212  
213 sub _retrieve_plugin_config {
214 my $plugin = shift;
215 my $name = $plugin->name;
216 my %settings = cookie("${name}_config");
217 return $plugin->config_defaults unless %settings;
218 foreach (keys %settings) {
219 # need better serialization than this...
220 if ($settings{$_} =~ /$;/) {
221 my @settings = split $;,$settings{$_};
222 pop @settings unless defined $settings[-1];
223 $settings{$_} = \@settings;
224 }
225 }
226 \%settings;
227 }
228  
229 sub menu_labels {
230 my $self = shift;
231 my $plugins = $self->plugins;
232 my $config = $self->config;
233 my $lang = $self->language;
234  
235 my %verbs = (dumper => $lang->tr('Dump'),
236 finder => $lang->tr('Find'),
237 highlighter => $lang->tr('Highlight'),
238 annotator => $lang->tr('Annotate'),
239 filter => $lang->tr('Filter'),
240 );
241 my %labels = ();
242  
243 # Adjust plugin menu labels
244 for ( keys %{$plugins} ) {
245  
246 # plugin-defined verb
247 if ( $plugins->{$_}->verb ) {
248 $labels{$_} = $lang->tr($plugins->{$_}->verb) ||
249 ucfirst $plugins->{$_}->verb;
250 }
251 # default verb
252 else {
253 $labels{$_} = $verbs{$plugins->{$_}->type} ||
254 ucfirst $plugins->{$_}->type;
255 }
256 my $name = $plugins->{$_}->name;
257 $labels{$_} .= " $name";
258 $labels{$_} =~ s/^\s+//;
259 }
260 return \%labels;
261 }
262  
263 1;
264  
265 __END__