source: gsdl/trunk/perllib/plugins/PrintInfo.pm@ 15881

Last change on this file since 15881 was 15881, checked in by kjdon, 16 years ago

auxiliary plugins now pass an extra argument to the PrintInfo constructor so that argument parsing is not done - parsing needs to be done by the main plugin which has all the arguments

  • Property svn:executable set to *
File size: 6.8 KB
Line 
1## Most basic plugin, just handles parsing the arguments and printing out descriptions. Used for plugins and Extractor plugins
2
3package PrintInfo;
4
5BEGIN {
6 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
7}
8
9eval {require bytes};
10
11# suppress the annoying "subroutine redefined" warning that various
12# plugins cause under perl 5.6
13$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
14
15use strict;
16no strict 'subs';
17
18use gsprintf 'gsprintf';
19use parse2;
20use printusage;
21
22my $arguments = [];
23
24my $options = { 'name' => "PrintInfo",
25 'desc' => "{PrintInfo.desc}",
26 'abstract' => "yes",
27 'inherits' => "no",
28 'args' => $arguments };
29
30# $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.
31sub new
32{
33 my $class = shift (@_);
34 my ($pluginlist,$args,$hashArgOptLists, $auxiliary) = @_;
35 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
36
37 if ($plugin_name eq $class) {
38 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
39 push(@{$hashArgOptLists->{"OptList"}},$options);
40 }
41 my $self = {};
42 $self->{'outhandle'} = STDERR;
43 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
44 $self->{"info_only"} = 0;
45
46 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
47 # the args, just return the object.
48 foreach my $strArg (@{$args})
49 {
50 if($strArg eq "-gsdlinfo")
51 {
52 $self->{"info_only"} = 1;
53 return bless $self, $class;
54 }
55 }
56
57 if (defined $auxiliary) { # don't parse the args here
58 return bless $self, $class;
59 }
60
61 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
62 {
63 my $classTempClass = bless $self, $class;
64 print STDERR "<BadPlugin p=$plugin_name>\n";
65 &gsprintf(STDERR, "\n{PrintInfo.bad_general_option}\n", $plugin_name);
66 $classTempClass->print_txt_usage(""); # Use default resource bundle
67 die "\n";
68 }
69
70 delete $self->{"info_only"};
71 # else parsing was successful.
72
73 $self->{'plugin_type'} = $plugin_name;
74
75 return bless $self, $class;
76
77}
78
79#sub init {
80#}
81
82sub get_arguments
83{
84 my $self = shift(@_);
85 my $optionlistref = $self->{'option_list'};
86 my @optionlist = @$optionlistref;
87 my $pluginoptions = pop(@$optionlistref);
88 my $pluginarguments = $pluginoptions->{'args'};
89 return $pluginarguments;
90}
91
92
93sub print_xml_usage
94{
95 my $self = shift(@_);
96 my $header = shift(@_);
97 my $high_level_information_only = shift(@_);
98
99 # XML output is always in UTF-8
100 gsprintf::output_strings_in_UTF8;
101
102 if ($header) {
103 &PrintUsage::print_xml_header("plugin");
104 }
105 $self->print_xml($high_level_information_only);
106}
107
108
109sub print_xml
110{
111 my $self = shift(@_);
112 my $high_level_information_only = shift(@_);
113
114 my $optionlistref = $self->{'option_list'};
115 my @optionlist = @$optionlistref;
116 my $pluginoptions = shift(@$optionlistref);
117 return if (!defined($pluginoptions));
118
119 # Find the process and block default expressions in the plugin arguments
120 my $process_exp = "";
121 my $block_exp = "";
122 if (defined($pluginoptions->{'args'})) {
123 foreach my $option (@{$pluginoptions->{'args'}}) {
124 if ($option->{'name'} eq "process_exp") {
125 $process_exp = $option->{'deft'};
126 }
127 if ($option->{'name'} eq "block_exp") {
128 $block_exp = $option->{'deft'};
129 }
130 }
131 }
132
133 gsprintf(STDERR, "<PlugInfo>\n");
134 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
135 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
136 $desc =~ s/</&amp;lt;/g; # doubly escaped
137 $desc =~ s/>/&amp;gt;/g;
138 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
139 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
140 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
141 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
142 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
143 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
144 # adding new option that works with replace_srcdoc_with_html.pl
145 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
146 unless (defined($high_level_information_only)) {
147 gsprintf(STDERR, " <Arguments>\n");
148 if (defined($pluginoptions->{'args'})) {
149 &PrintUsage::print_options_xml($pluginoptions->{'args'});
150 }
151 gsprintf(STDERR, " </Arguments>\n");
152
153 # Recurse up the plugin hierarchy
154 $self->print_xml();
155 }
156 gsprintf(STDERR, "</PlugInfo>\n");
157}
158
159
160sub print_txt_usage
161{
162 my $self = shift(@_);
163 # Print the usage message for a plugin (recursively)
164 my $descoffset = $self->determine_description_offset(0);
165 $self->print_plugin_usage($descoffset, 1);
166}
167
168
169sub determine_description_offset
170{
171 my $self = shift(@_);
172 my $maxoffset = shift(@_);
173
174 my $optionlistref = $self->{'option_list'};
175 my @optionlist = @$optionlistref;
176 my $pluginoptions = shift(@$optionlistref);
177 return $maxoffset if (!defined($pluginoptions));
178
179 # Find the length of the longest option string of this plugin
180 my $pluginargs = $pluginoptions->{'args'};
181 if (defined($pluginargs)) {
182 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
183 if ($longest > $maxoffset) {
184 $maxoffset = $longest;
185 }
186 }
187
188 # Recurse up the plugin hierarchy
189 $maxoffset = $self->determine_description_offset($maxoffset);
190 $self->{'option_list'} = \@optionlist;
191 return $maxoffset;
192}
193
194
195sub print_plugin_usage
196{
197 my $self = shift(@_);
198 my $descoffset = shift(@_);
199 my $isleafclass = shift(@_);
200
201 my $optionlistref = $self->{'option_list'};
202 my @optionlist = @$optionlistref;
203 my $pluginoptions = shift(@$optionlistref);
204 return if (!defined($pluginoptions));
205
206 my $pluginname = $pluginoptions->{'name'};
207 my $pluginargs = $pluginoptions->{'args'};
208 my $plugindesc = $pluginoptions->{'desc'};
209
210 # Produce the usage information using the data structure above
211 if ($isleafclass) {
212 if (defined($plugindesc)) {
213 gsprintf(STDERR, "$plugindesc\n\n");
214 }
215 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
216 }
217
218 # Display the plugin options, if there are some
219 if (defined($pluginargs)) {
220 # Calculate the column offset of the option descriptions
221 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
222
223 if ($isleafclass) {
224 gsprintf(STDERR, " {common.specific_options}:\n");
225 }
226 else {
227 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
228 }
229
230 # Display the plugin options
231 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
232 }
233
234 # Recurse up the plugin hierarchy
235 $self->print_plugin_usage($descoffset, 0);
236 $self->{'option_list'} = \@optionlist;
237}
238
2391;
240
241
Note: See TracBrowser for help on using the repository browser.