source: main/trunk/greenstone2/perllib/plugins/PrintInfo.pm@ 32122

Last change on this file since 32122 was 25957, checked in by kjdon, 12 years ago

adding in support for plugins knowing what version of greenstone (2/3) they are running in. gs_version now passed in when loading plugins, and '-gs_version 2/3' will be added to input args. base plugin class PrintInfo defines the gs_version arg

  • Property svn:executable set to *
File size: 9.0 KB
Line 
1###########################################################################
2#
3# PrintInfo - most base plugin
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27## Most basic plugin, just handles parsing the arguments and printing out descriptions. Used for plugins and Extractor plugins
28
29package PrintInfo;
30
31BEGIN {
32 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33}
34
35eval {require bytes};
36eval "require diagnostics"; # some perl distros (eg mac) don't have this
37
38# suppress the annoying "subroutine redefined" warning that various
39# plugins cause under perl 5.6
40$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
41
42use strict;
43no strict 'subs';
44
45use gsprintf 'gsprintf';
46use parse2;
47use printusage;
48
49my $arguments = [
50 { 'name' => "gs_version",
51 'desc' => "{PrintInfo.gs_version}",
52 'type' => "string",
53 'reqd' => "no",
54 'hiddengli' => "yes" }
55
56];
57
58my $options = { 'name' => "PrintInfo",
59 'desc' => "{PrintInfo.desc}",
60 'abstract' => "yes",
61 'inherits' => "no",
62 'args' => $arguments };
63
64# $auxiliary_plugin argument passed in by "on-the-side" plugin helpers such as Extractors and ImageConverter. We don't want parsing of args done by them.
65sub new
66{
67 my $class = shift (@_);
68 my ($pluginlist,$args,$hashArgOptLists, $auxiliary) = @_;
69 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
70
71 if ($plugin_name eq $class) {
72 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
73 push(@{$hashArgOptLists->{"OptList"}},$options);
74 }
75 my $self = {};
76 $self->{'outhandle'} = STDERR;
77 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
78 $self->{"info_only"} = 0;
79 $self->{'gs_version'} = "2";
80 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
81 # the args, just return the object.
82 # gsdlinfo must come before gs_version. both are set by plugin.pm
83 my $v=0;
84 foreach my $strArg (@{$args})
85 {
86 if ($v) {
87 $self->{'gs_version'} = $strArg;
88 last;
89 }
90 elsif($strArg eq "-gsdlinfo")
91 {
92 $self->{"info_only"} = 1;
93 #return bless $self, $class;
94 }
95 elsif ($strArg eq "-gs_version") {
96 $v = 1;
97 }
98 }
99
100 if ($self->{"info_only"}) {
101 return bless $self, $class;
102 }
103 if (defined $auxiliary) { # don't parse the args here
104 return bless $self, $class;
105 }
106
107 # now that we are passed printing out info, we do need to add in this class's options so that they are available for parsing.
108 if ($plugin_name ne $class) {
109 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
110 push(@{$hashArgOptLists->{"OptList"}},$options);
111 }
112
113 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
114 {
115 my $classTempClass = bless $self, $class;
116 print STDERR "<BadPlugin p=$plugin_name>\n";
117 &gsprintf(STDERR, "\n{PrintInfo.bad_general_option}\n", $plugin_name);
118 $classTempClass->print_txt_usage(""); # Use default resource bundle
119 die "\n";
120 }
121
122 delete $self->{"info_only"};
123 # else parsing was successful.
124
125 $self->{'plugin_type'} = $plugin_name;
126
127 return bless $self, $class;
128
129}
130
131#sub init {
132#}
133
134sub set_incremental {
135 my $self = shift(@_);
136 my ($incremental_mode) = @_;
137
138 if (!defined $incremental_mode) {
139 $self->{'incremental'} = 0;
140 $self->{'incremental_mode'} = "none";
141 }
142 elsif ($incremental_mode eq "all") {
143 $self->{'incremental'} = 1;
144 $self->{'incremental_mode'} = "all";
145 }
146 else {
147 # none, onlyadd
148 $self->{'incremental'} = 0;
149 $self->{'incremental_mode'} = $incremental_mode;
150 }
151}
152
153sub get_arguments
154{
155 my $self = shift(@_);
156 my $optionlistref = $self->{'option_list'};
157 my @optionlist = @$optionlistref;
158 my $pluginoptions = pop(@$optionlistref);
159 my $pluginarguments = $pluginoptions->{'args'};
160 return $pluginarguments;
161}
162
163
164sub print_xml_usage
165{
166 my $self = shift(@_);
167 my $header = shift(@_);
168 my $high_level_information_only = shift(@_);
169
170 # XML output is always in UTF-8
171 gsprintf::output_strings_in_UTF8;
172
173 if ($header) {
174 &PrintUsage::print_xml_header("plugin");
175 }
176 $self->print_xml($high_level_information_only);
177}
178
179
180sub print_xml
181{
182 my $self = shift(@_);
183 my $high_level_information_only = shift(@_);
184
185 my $optionlistref = $self->{'option_list'};
186 my @optionlist = @$optionlistref;
187 my $pluginoptions = shift(@$optionlistref);
188 return if (!defined($pluginoptions));
189
190 # Find the process and block default expressions in the plugin arguments
191 my $process_exp = "";
192 my $block_exp = "";
193 if (defined($pluginoptions->{'args'})) {
194 foreach my $option (@{$pluginoptions->{'args'}}) {
195 if ($option->{'name'} eq "process_exp") {
196 $process_exp = $option->{'deft'};
197 }
198 if ($option->{'name'} eq "block_exp") {
199 $block_exp = $option->{'deft'};
200 }
201 }
202 }
203
204 gsprintf(STDERR, "<PlugInfo>\n");
205 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
206 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
207 $desc =~ s/</&amp;lt;/g; # doubly escaped
208 $desc =~ s/>/&amp;gt;/g;
209 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
210 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
211 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
212 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
213 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
214 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
215 # adding new option that works with replace_srcdoc_with_html.pl
216 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
217 unless (defined($high_level_information_only)) {
218 gsprintf(STDERR, " <Arguments>\n");
219 if (defined($pluginoptions->{'args'})) {
220 &PrintUsage::print_options_xml($pluginoptions->{'args'});
221 }
222 gsprintf(STDERR, " </Arguments>\n");
223
224 # Recurse up the plugin hierarchy
225 $self->print_xml();
226 }
227 gsprintf(STDERR, "</PlugInfo>\n");
228}
229
230
231sub print_txt_usage
232{
233 my $self = shift(@_);
234 # Print the usage message for a plugin (recursively)
235 my $descoffset = $self->determine_description_offset(0);
236 $self->print_plugin_usage($descoffset, 1);
237}
238
239
240sub determine_description_offset
241{
242 my $self = shift(@_);
243 my $maxoffset = shift(@_);
244
245 my $optionlistref = $self->{'option_list'};
246 my @optionlist = @$optionlistref;
247 my $pluginoptions = shift(@$optionlistref);
248 return $maxoffset if (!defined($pluginoptions));
249
250 # Find the length of the longest option string of this plugin
251 my $pluginargs = $pluginoptions->{'args'};
252 if (defined($pluginargs)) {
253 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
254 if ($longest > $maxoffset) {
255 $maxoffset = $longest;
256 }
257 }
258
259 # Recurse up the plugin hierarchy
260 $maxoffset = $self->determine_description_offset($maxoffset);
261 $self->{'option_list'} = \@optionlist;
262 return $maxoffset;
263}
264
265
266sub print_plugin_usage
267{
268 my $self = shift(@_);
269 my $descoffset = shift(@_);
270 my $isleafclass = shift(@_);
271
272 my $optionlistref = $self->{'option_list'};
273 my @optionlist = @$optionlistref;
274 my $pluginoptions = shift(@$optionlistref);
275 return if (!defined($pluginoptions));
276
277 my $pluginname = $pluginoptions->{'name'};
278 my $pluginargs = $pluginoptions->{'args'};
279 my $plugindesc = $pluginoptions->{'desc'};
280
281 # Produce the usage information using the data structure above
282 if ($isleafclass) {
283 if (defined($plugindesc)) {
284 gsprintf(STDERR, "$plugindesc\n\n");
285 }
286 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
287 }
288
289 # Display the plugin options, if there are some
290 if (defined($pluginargs)) {
291 # Calculate the column offset of the option descriptions
292 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
293
294 if ($isleafclass) {
295 gsprintf(STDERR, " {common.specific_options}:\n");
296 }
297 else {
298 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
299 }
300
301 # Display the plugin options
302 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
303 }
304
305 # Recurse up the plugin hierarchy
306 $self->print_plugin_usage($descoffset, 0);
307 $self->{'option_list'} = \@optionlist;
308}
309
310
3111;
312
313
Note: See TracBrowser for help on using the repository browser.