root/main/trunk/greenstone2/perllib/plugins/AutoLoadConverters.pm @ 32341

Revision 32341, 4.9 KB (checked in by ak19, 13 months ago)

1. Fixing up regex syntax in DirectoryPlugin? for perl 5.26 that comes with Ubuntu 18.04, see Kathy's commit message at http://trac.greenstone.org/changeset/32129 2. AutoLoadConverters? info message commented out as it ends up breaking XML output and then GLI can't parse this and won't load.

Line 
1package AutoLoadConverters;
2
3use strict;
4no strict 'refs';
5no strict 'subs';
6
7use PrintInfo;
8use util;
9
10use gsprintf 'gsprintf';
11
12sub BEGIN {
13    @AutoLoadConverters::ISA = ('PrintInfo');
14}
15
16# the dynamic conversion args will get added here
17my $arguments = [];
18
19my $options = { 'name'     => "AutoLoadConverters",
20        'desc'     => "{AutoLoadConverters.desc}",
21        'abstract' => "yes",
22        'inherits' => "yes",
23        'args'     => $arguments };
24
25sub dynamically_load_converter
26{
27    my ($autoPackage, $calling_class) = @_;
28
29    my $settings = {};
30   
31    my ($autoName)  = ($autoPackage =~ m/(^.*)Converter$/);
32    my $autoVar = lc($autoName);
33    $settings->{'converter_name'} = $autoName;
34    $settings->{'converter_var'} = $autoVar;
35   
36    eval("require $autoPackage");
37    if ($@) {
38    # Useful debugging statement if there is a syntax error in the plugin
39    #print STDERR "$@\n";
40    $settings->{'converter_installed'} = 0;
41    $settings->{'conversion_available'} = 0;
42    }
43    else {
44    # found the converter
45    $settings->{'conversion_installed'} = 1;
46#   print STDERR "AutoLoadConverters: $autoName Extension to Greenstone detected for $calling_class\n";
47    # but can it run???
48    if (eval "\$${autoPackage}::${autoVar}_conversion_available") {
49        $settings->{'conversion_available'} = 1;
50        push(@AutoLoadConverters::ISA, $autoPackage);
51
52    } else {
53        $settings->{'conversion_available'} = 0;
54        print STDERR "... but it appears to be broken\n";
55        &gsprintf(STDERR, "AutoLoadConverters: {AutoloadConverter.noconversionavailable}");
56        my $dictentry_reason = eval "\$${autoPackage}::no_${autoVar}_conversion_reason";
57
58        &gsprintf(STDERR, " ({$autoPackage\.$dictentry_reason})\n");
59
60    }
61    }
62
63    if ($settings->{'conversion_available'}) {
64
65    my $opt_conversion_args =
66        [ { 'name' => "$autoVar\_conversion",
67        'desc' => "{$autoPackage.$autoVar\_conversion}",
68        'type' => "flag",
69        'reqd' => "no" } ];
70
71    $settings->{'converter_arguments'} = $opt_conversion_args;
72
73    }
74    return $settings;
75
76
77}
78
79####
80# This plugin takes an extra initial parameter in its constructor (compared
81#   with the norm).  The extra parameter is an array of converters
82# it should  try to dynamically load
83#####
84
85sub new {
86
87    # Start the AutoExtractMetadata Constructor
88    my $class = shift (@_);
89    my ($pluginlist,$inputargs,$hashArgOptLists,$autoPackages, $auxiliary) = @_;
90   
91    push(@$pluginlist, $class);
92    my $classPluginName = $pluginlist->[0];
93
94    push(@{$hashArgOptLists->{"OptList"}},$options);
95
96    # this static list is just for pluginfo on AutoLoadConverters - we will try to explicitly load all packages
97    $autoPackages = ["OpenOfficeConverter", "PDFBoxConverter"] unless defined $autoPackages;
98
99    my @self_array = ();
100    my $temporary_self = {};
101    $temporary_self->{'converter_list'} = [];
102    push (@self_array, $temporary_self);
103    foreach my $packageName (@$autoPackages) {
104    my $package_settings = &dynamically_load_converter($packageName, $classPluginName);
105    my $available_var = $package_settings->{'converter_var'}."_available";
106    if ($package_settings->{'conversion_available'}) {
107        push(@$arguments, @{$package_settings->{'converter_arguments'}});
108        my $package_self = eval "new $packageName(\$pluginlist, \$inputargs, \$hashArgOptLists,1);";
109        push (@self_array, $package_self);
110        $temporary_self->{$available_var} = 1;
111        push(@{$temporary_self->{'converter_list'}}, $packageName);
112    } else {
113        $temporary_self->{$available_var} = 0;
114    }
115    }
116    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
117    my $self;
118    my $pi_self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
119    push (@self_array, $pi_self);
120    $self = BaseImporter::merge_inheritance(@self_array);
121
122    return bless $self, $class;
123}
124
125sub init {
126    my $self = shift (@_);
127
128    foreach my $converter (@{$self->{'converter_list'}}) {
129    eval "\$self->${converter}::init(\@_);";
130    }
131
132}
133
134sub begin {
135    my $self = shift (@_);
136
137    foreach my $converter(@{$self->{'converter_list'}}) {
138    eval "\$self->${converter}::begin(\@_);";
139    }
140}
141
142sub deinit {
143    my $self = shift (@_);
144
145    foreach my $converter (@{$self->{'converter_list'}}) {
146    eval "\$self->${converter}::deinit(\@_);";
147    }
148
149}
150
151sub tmp_area_convert_file {
152    my $self = shift (@_);
153    my ($output_ext, $input_filename, $textref) = @_;
154
155    foreach my $converter(@{$self->{'converter_list'}}) {
156    my ($var) = ($converter =~ m/(^.*)Converter$/);
157    $var = lc($var);
158    if ($self->{"${var}_conversion"}) {
159        my ($result, $result_str, $new_filename)
160        = eval "\$self->${converter}::convert(\$input_filename, \$output_ext);";
161        if (defined $result && $result != 0) {
162        return $new_filename;
163        }
164        my $outhandle=$self->{'outhandle'};
165        print $outhandle "$converter Conversion error\n";
166        print $outhandle "$@\n";
167        if (defined $result_str) {
168        print $outhandle "$result_str\n";
169        }
170        return "";
171    }
172    }
173   
174    # if got here, no converter was specified in plugin args
175    return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
176   
177}
Note: See TracBrowser for help on using the browser.