| 1 | package Pipeline::Config::YAML; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings::register; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Pipeline; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Pipeline::Config::LoadError; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use UNIVERSAL qw( can isa ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | use YAML qw( LoadFile Dump ); | ||
| 12 | |||
| 13 | use base qw( Pipeline::Base ); | ||
| 14 | |||
| 15 | our $VERSION = ((require Pipeline::Config), $Pipeline::Config::VERSION)[1]; | ||
| 16 | our $REVISION = (split(/ /, ' $Revision: 1.9 $ '))[2]; | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my $self = shift; | ||
| 20 | $self->{search} = Pipeline::Config::YAML::Search->new; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub load { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | $self->{file} = shift; | ||
| 27 | |||
| 28 | $self->emit( "loading $self->{file}" ); | ||
| 29 | my $config = LoadFile( $self->{file} ); | ||
| 30 | |||
| 31 | return $self->parse( $config ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub parse { | ||
| 35 | my $self = shift; | ||
| 36 | my $config = shift; | ||
| 37 | |||
| 38 | $self->{indent} ++; | ||
| 39 | $self->emit( "parsing config" ); | ||
| 40 | |||
| 41 | $self->{pipe} = new Pipeline; | ||
| 42 | $self->parse_hash( $config ); | ||
| 43 | |||
| 44 | $self->{indent} --; | ||
| 45 | |||
| 46 | return $self->{pipe}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub parse_hash { | ||
| 50 | my $self = shift; | ||
| 51 | my $hash = shift; | ||
| 52 | my $context = shift; | ||
| 53 | |||
| 54 | throw Pipeline::Config::LoadError( "[$hash] is not a hash! context: [$context]!" ) | ||
| 55 | unless isa( $hash, 'HASH' ); | ||
| 56 | |||
| 57 | $self->{indent} ++; | ||
| 58 | $self->emit( "parsing hash\t($context)" ); | ||
| 59 | |||
| 60 | foreach my $key (keys %$hash) { | ||
| 61 | my $context2 = $self->get_context( $key, $context ); | ||
| 62 | my $val = $hash->{$key}; | ||
| 63 | my $type = ref($val); | ||
| 64 | |||
| 65 | if ($type) { | ||
| 66 | my $parse_method = 'parse_' . lc( $type ); | ||
| 67 | $self->$parse_method( $val, $context2 ); | ||
| 68 | } else { | ||
| 69 | $self->parse_text( $val, $context2 ); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->{indent} --; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub parse_array { | ||
| 77 | my $self = shift; | ||
| 78 | my $list = shift; | ||
| 79 | my $context = shift; | ||
| 80 | |||
| 81 | throw Pipeline::Config::LoadError( "[$list] is not a list! context: [$context]" ) | ||
| 82 | unless isa( $list, 'ARRAY' ); | ||
| 83 | |||
| 84 | $self->{indent} ++; | ||
| 85 | $self->emit( "parsing list\t($context)" ); | ||
| 86 | |||
| 87 | foreach my $val (@$list) { | ||
| 88 | if (my $type = ref($val)) { | ||
| 89 | my $parse_method = 'parse_' . lc( $type ); | ||
| 90 | $self->$parse_method( $val, $context ); | ||
| 91 | } else { | ||
| 92 | $self->parse_text( $val, $context ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | $self->{indent} --; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub parse_text { | ||
| 100 | my $self = shift; | ||
| 101 | my $text = shift; | ||
| 102 | my $context = shift; | ||
| 103 | |||
| 104 | $self->{indent} ++; | ||
| 105 | $self->emit( "$text\t($context)" ); | ||
| 106 | |||
| 107 | if (blessed( $context )) { | ||
| 108 | if ($context->isa( 'Pipeline::Config::YAML::Search' )) { | ||
| 109 | push @{ $context->{packages} }, $text; | ||
| 110 | } elsif ($context->isa( 'Pipeline' )) { | ||
| 111 | $context->add_segment( $self->create_segment( $text ) ); | ||
| 112 | } else { | ||
| 113 | $self->emit( "unknown context: $context" ); | ||
| 114 | } | ||
| 115 | } elsif (ref($context) eq 'CODE') { | ||
| 116 | &$context( $text ); | ||
| 117 | } else { | ||
| 118 | $self->emit( "unknown context: $context" ); | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->{indent} --; | ||
| 122 | |||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub get_context { | ||
| 127 | my $self = shift; | ||
| 128 | my $text = shift; | ||
| 129 | my $context = shift; | ||
| 130 | |||
| 131 | $self->{indent} ++; | ||
| 132 | $self->emit( "getting context for $text\t($context)" ); | ||
| 133 | |||
| 134 | my $new_context; | ||
| 135 | if ($text =~ /^search.packages$/i) { | ||
| 136 | $new_context = $self->{search}; | ||
| 137 | } elsif ($text =~ /^cleanups$/i) { | ||
| 138 | $new_context = $self->{pipe}->cleanups; | ||
| 139 | } elsif ($text =~ /^pipeline$/i) { | ||
| 140 | $new_context = $self->{pipe}; | ||
| 141 | } elsif ($text =~ /^(.+) pipe$/i) { | ||
| 142 | $new_context = $self->create_pipe($1); | ||
| 143 | if (blessed( $context ) and $context->isa( 'Pipeline' )) { | ||
| 144 | $context->add_segment( $new_context ); | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | if (blessed( $context )) { | ||
| 148 | if ($context->isa( 'Pipeline' )) { | ||
| 149 | $new_context = $self->create_segment($text); | ||
| 150 | $context->add_segment( $new_context ); | ||
| 151 | } elsif ($context->isa( 'Pipeline::Segment' )) { | ||
| 152 | $new_context = sub { $context->$text( @_ ) }; | ||
| 153 | } else { | ||
| 154 | $new_context = $text; | ||
| 155 | } | ||
| 156 | } else { | ||
| 157 | $new_context = $text; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | return $new_context; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub create_pipe { | ||
| 165 | my $self = shift; | ||
| 166 | my $pipe_class = shift; | ||
| 167 | # TODO: load $pipe_class, or create it | ||
| 168 | return Pipeline->new; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub create_segment { | ||
| 172 | my $self = shift; | ||
| 173 | my $seg_class = shift; | ||
| 174 | my @args = @_; | ||
| 175 | |||
| 176 | $self->{indent} ++; | ||
| 177 | $self->emit( "creating new $seg_class" ); | ||
| 178 | |||
| 179 | my $actual_class = $self->load_seg_class( $seg_class ); | ||
| 180 | |||
| 181 | $self->{indent} --; | ||
| 182 | return $actual_class->new( @args ); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub load_seg_class { | ||
| 186 | my $self = shift; | ||
| 187 | my $seg_class = shift; | ||
| 188 | |||
| 189 | my @search_pkgs = ( $seg_class, | ||
| 190 | map { $_ . '::' . $seg_class } | ||
| 191 | @{ $self->{search}->{packages} } ); | ||
| 192 | |||
| 193 | foreach my $pkg ( @search_pkgs ) { | ||
| 194 | return $pkg if $pkg->can( 'new' ); | ||
| 195 | eval "require $pkg"; | ||
| 196 | return $pkg unless $@; | ||
| 197 | } | ||
| 198 | |||
| 199 | throw Pipeline::Config::LoadError( "Error loading class [$seg_class] - " . | ||
| 200 | "couldn't find new() in: " . | ||
| 201 | join( ', ', @search_pkgs) ); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub emit { | ||
| 205 | my $self = shift; | ||
| 206 | $self->SUPER::emit( ' ' x ($self->{indent}*2) . join('', @_) ); | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | package Pipeline::Config::YAML::Search; | ||
| 211 | |||
| 212 | use base qw( Pipeline::Base ); | ||
| 213 | |||
| 214 | sub init { | ||
| 215 | my $self = shift; | ||
| 216 | $self->{packages} = []; | ||
| 217 | return $self; | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | |||
| 222 | __END__ | ||