File: Plugin.pm

package info (click to toggle)
libscrappy-perl 0.94112090-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 988 kB
  • ctags: 122
  • sloc: perl: 2,247; makefile: 7
file content (134 lines) | stat: -rw-r--r-- 3,686 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package Scrappy::Plugin;

BEGIN {
    $Scrappy::Plugin::VERSION = '0.94112090';
}

# load OO System
use Moose;

# load other libraries
use File::Find::Rule;

# a hash list of installed plugins
has registry => (
    is      => 'ro',
    isa     => 'HashRef',
    default => sub {

        # map plugins
        my $plugins = {};
        my @plugins = @{shift->plugins};
        foreach my $plugin (@plugins) {
            $plugins->{$plugin} = $plugin;
            $plugins->{lc($plugin)} = $plugin;
        }
        return $plugins;
    }
);

# return a list of installed plugins
has plugins => (
    is      => 'ro',
    isa     => 'Any',
    # registry() uses us in their initializer, defer our own
    # initialization until then
    lazy    => 1,
    default => sub {
        my @plugins = ();

        # fix for bug found by Patrick Woo

#Can't stat /etc/perl/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/local/lib/perl/5.10.1/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/lib/perl5/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/share/perl5/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/lib/perl/5.10/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/share/perl/5.10/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat /usr/local/lib/site_perl/Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595
#Can't stat ./Scrappy/Plugin: No such file or directory
#at /usr/share/perl5/File/Find/Rule.pm line 595

        # ... (IMO) due to analyzing @INC assuming each path has Scrappy in it

        my $library;

        foreach my $dir (@INC) {
            if (-d "$dir/Scrappy/Plugin") {
                $library = "$dir/Scrappy/Plugin";
                last;
            }
        }

        return [] unless $library;

        my @files = File::Find::Rule->file()->name('*.pm')->in($library);

        my %plugins =
          map { $_ => 1 }
          map { s/.*(Scrappy[\\\/]Plugin[\\\/].*\.pm)/$1/; $_ }
          @files;    #uniquenes

        for my $plugin (keys %plugins) {

            my ($plug) = $plugin =~ /(Scrappy\/Plugin\/.*)\.pm/;

            if ($plug) {
                $plug =~ s/\//::/g;
                push @plugins, $plug;
            }

        }

        return [@plugins];
    }
);

sub load_plugin {
    my $self    = shift;
    my @plugins = @_;
    my @returns = ();

    foreach my $plugin (@plugins) {

        unless ($plugin =~ /^Scrappy::Plugin::/) {

            # make fully-quaified plugin name
            $plugin = ucfirst $plugin;

            $plugin = join("::", map(ucfirst, split '-', $plugin))
              if $plugin =~ /\-/;
            $plugin = join("", map(ucfirst, split '_', $plugin))
              if $plugin =~ /\_/;

            $plugin = "Scrappy::Plugin::$plugin";
        }

        # check for a direct match
        if ($self->registry->{$plugin}) {
            with $self->registry->{$plugin};
            push @returns, $self->registry->{$plugin};
        }

        # last resort seek
        elsif ($self->registry->{lc($plugin)}) {
            with $self->registry->{lc($plugin)};
            push @returns, $self->registry->{lc($plugin)};
        }
        else {
            die(    "Error loading the plugin $plugin, "
                  . "please check that it has been installed");
        }
    }

    return @returns;
}

1;