| 1 | # PiFlash::Plugin - plugin extension interface for PiFlash | ||||||||||||
| 2 | # by Ian Kluft | ||||||||||||
| 3 | |||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 5 | ▶ | use v5.14.0; # require 2011 or newer version of Perl | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | package PiFlash::Plugin; | ||||||||||||
| 9 | 5 | $PiFlash::Plugin::VERSION = '0.4.3'; | |||||||||||
| 10 | 5 | ▶ | use autodie; # report errors instead of silently continuing ("die" actions are used as exceptions - caught & reported) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 5 | ▶ | use parent 'PiFlash::Object'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 5 | ▶ | use PiFlash::State; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 5 | ▶ | use Module::Pluggable require => 1, search_path => [__PACKAGE__]; # RPM: perl-Module-Pluggable, DEB: libmodule-pluggable-perl | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | # ABSTRACT: plugin extension interface for PiFlash | ||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | # required parameter list | ||||||||||||
| 19 | # class method | ||||||||||||
| 20 | # used by PiFlash::Object for new() method | ||||||||||||
| 21 | sub object_params | ||||||||||||
| 22 | 0 | ▶ | { | ||||||||||
|
Subroutine
object_params: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 23 | 0 | return qw(name class); | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | # initialize enabled plugins | ||||||||||||
| 27 | # class method | ||||||||||||
| 28 | sub init_plugins | ||||||||||||
| 29 | 0 | ▶ | { | ||||||||||
|
Subroutine
init_plugins: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||
| 30 | # get list of enabled plugins from command line and config file | ||||||||||||
| 31 | 0 | my %enabled; | |||||||||||
| 32 | 0 | ▶ | if (PiFlash::State::has_cli_opt("plugin")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 0 | ▶ | foreach my $plugin ( split(/[^\w:]+/, PiFlash::State::cli_opt("plugin") // "")) { | ||||||||||
|
Condition0%0/2
PiFlash::State::cli_opt("plugin") // ""
MC/DC0%0/1
PiFlash::State::cli_opt("plugin") // ""
PiFlash::State::cli_opt("plugin")
Truth table0%0/2
A
A= PiFlash::State::cli_opt("plugin")
| |||||||||||||
| 34 | 0 | ▶ | next if $plugin eq ""; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | $plugin =~ s/^.*:://; | |||||||||||
| 36 | 0 | $enabled{$plugin} = 1; | |||||||||||
| 37 | } | ||||||||||||
| 38 | } | ||||||||||||
| 39 | 0 | ▶ | if (PiFlash::State::has_config("plugin")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | ▶ | foreach my $plugin ( split(/[^\w:]+/, PiFlash::State::config("plugin") // "")) { | ||||||||||
|
Condition0%0/2
PiFlash::State::config("plugin") // ""
MC/DC0%0/1
PiFlash::State::config("plugin") // ""
PiFlash::State::config("plugin")
Truth table0%0/2
A
A= PiFlash::State::config("plugin")
| |||||||||||||
| 41 | 0 | ▶ | next if $plugin eq ""; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | $plugin =~ s/^.*:://; | |||||||||||
| 43 | 0 | $enabled{$plugin} = 1; | |||||||||||
| 44 | } | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | # for each enabled plugin, allocate state storage, load its config (if any) and run its init method | ||||||||||||
| 48 | 0 | my @plugins_available = PiFlash::Plugin->plugins(); | |||||||||||
| 49 | 0 | foreach my $plugin (@plugins_available) { | |||||||||||
| 50 | # fool function that it was called as class method | ||||||||||||
| 51 | # we don't call the subclass' method until we're sure the class is loaded | ||||||||||||
| 52 | # but we know it will inherit the method function from here | ||||||||||||
| 53 | 0 | my $modname = PiFlash::Plugin::get_modname($plugin); | |||||||||||
| 54 | |||||||||||||
| 55 | # check if the module is enabled by user from config or CLI | ||||||||||||
| 56 | 0 | ▶ | if (exists $enabled{$modname}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | # load the plugin code if its symbol table doesn't already exist (not already defined by a loaded module) | ||||||||||||
| 58 | 0 | ▶ | (defined(*{$plugin."::"})) or require $plugin; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | |||||||||||||
| 60 | # verify it's a subclass of PiFlash::Plugin | ||||||||||||
| 61 | 0 | ▶ | if ($plugin->isa("PiFlash::Plugin")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | # skip if its object/storage area exists | ||||||||||||
| 63 | 0 | ▶ | if (PiFlash::State::has_plugin($modname)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | next; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | # find any YAML documents addressed to this plugin from the config file | ||||||||||||
| 68 | 0 | my @data; | |||||||||||
| 69 | 0 | my $plugin_docs = PiFlash::State::plugin("docs"); | |||||||||||
| 70 | 0 | ▶ | if (exists $plugin_docs->{$modname}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | push @data, ("config" => $plugin_docs->{$modname}); | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | # if the plugin class has an init() method, inherited PiFlash::Object->new() will call it | ||||||||||||
| 75 | 0 | PiFlash::State::plugin($modname, $plugin->new({name => $modname, class => $plugin, @data})); | |||||||||||
| 76 | } | ||||||||||||
| 77 | } | ||||||||||||
| 78 | } | ||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | # derive module name from class name | ||||||||||||
| 82 | # class method | ||||||||||||
| 83 | sub get_modname | ||||||||||||
| 84 | 0 | ▶ | { | ||||||||||
|
Subroutine
get_modname: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 85 | 0 | my $class = shift; | |||||||||||
| 86 | 0 | ▶ | if ($class =~ /^PiFlash::Plugin::([A-Z]\w+)/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | return $1; | |||||||||||
| 88 | } | ||||||||||||
| 89 | 0 | return; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | # find the data/instance for the plugin | ||||||||||||
| 93 | # class method | ||||||||||||
| 94 | sub get_data | ||||||||||||
| 95 | 0 | ▶ | { | ||||||||||
|
Subroutine
get_data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 96 | 0 | my $class = shift; | |||||||||||
| 97 | 0 | my $modname = $class->get_modname(); | |||||||||||
| 98 | 0 | return PiFlash::State::plugin($modname); | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 5 | 1; | |||||||||||
| 102 | |||||||||||||
| 103 | __END__ | ||||||||||||