Changeset 1327

Show
Ignore:
Timestamp:
08/15/06 20:01:51
Author:
miyagawa
Message:

refactored load_include and load_recipes to Plagger::ConfigLoader?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/bin/plagger-ircbot

    r1325 r1327  
    33use strict; 
    44 
    5 use Plagger; 
    65use FindBin; 
     6use lib "$FindBin::Bin/../lib"; 
     7 
    78use Getopt::Long; 
    89use List::Util qw(first); 
    910use YAML; 
     11 
     12use Plagger::ConfigLoader; 
    1013 
    1114use POE qw( 
     
    2528msg "loading configuration $path"; 
    2629 
    27 my $config_yaml = YAML::LoadFile($path); 
     30my $loader = Plagger::ConfigLoader->new; 
     31my $base_config = $loader->load($path); 
    2832 
    29 # temp hack to allow include/recipes 
    30 Plagger::load_include(undef, $config_yaml); 
    31 Plagger::load_recipes(undef, $config_yaml); 
    32 my $plugin = first { $_->{module} eq 'Notify::IRC' } @{ $config_yaml->{plugins} } 
     33$loader->load_include($base_config); 
     34$loader->load_recipes($base_config); 
     35 
     36my $plugin = first { $_->{module} eq 'Notify::IRC' } @{ $base_config->{plugins} } 
    3337    or die "Can't find Notify::IRC config in $path"; 
    3438 
  • trunk/plagger/lib/Plagger.pm

    r1267 r1327  
    1919use Plagger::Cache; 
    2020use Plagger::CacheProxy; 
     21use Plagger::ConfigLoader; 
    2122use Plagger::Date; 
    2223use Plagger::Entry; 
     
    4041    }, $class; 
    4142 
    42     my $config; 
    43     if (-e $opt{config} && -r _) { 
    44         $config = YAML::LoadFile($opt{config}); 
    45         $self->{config_path} = $opt{config}; 
    46     } elsif (ref($opt{config}) && ref($opt{config}) eq 'SCALAR') { 
    47         $config = YAML::Load(${$opt{config}}); 
    48     } elsif (ref($opt{config}) && ref($opt{config}) eq 'HASH') { 
    49         $config = Storable::dclone($opt{config}); 
    50     } else { 
    51         croak "Plagger->bootstrap: $opt{config}: $!"; 
    52     } 
    53  
    54     $self->load_include($config); 
     43    my $loader = Plagger::ConfigLoader->new; 
     44    my $config = $loader->load($opt{config}); 
     45 
     46    $loader->load_include($config); 
    5547    $self->{conf} = $config->{global}; 
    5648    $self->{conf}->{log} ||= { level => 'debug' }; 
     
    6355    local *Plagger::context = sub { $self }; 
    6456 
    65     $self->load_recipes($config); 
     57    $loader->load_recipes($config); 
    6658    $self->load_cache($opt{config}); 
    6759    $self->load_plugins(@{ $config->{plugins} || [] }); 
     
    109101 
    110102        $self->log(info => "Rewrote $count password(s) and saved to $self->{config_path}"); 
    111     } 
    112 } 
    113  
    114 sub load_include { 
    115     my($self, $config) = @_; 
    116  
    117     return unless $config->{include}; 
    118     for (@{ $config->{include} }) { 
    119         my $include = YAML::LoadFile($_); 
    120  
    121         for my $key (keys %{ $include }) { 
    122             my $add = $include->{$key}; 
    123             unless ($config->{$key}) { 
    124                 $config->{$key} = $add; 
    125                 next; 
    126             } 
    127             if (ref($config->{$key}) eq 'HASH') { 
    128                 next unless ref($add) eq 'HASH'; 
    129                 for (keys %{ $include->{$key} }) { 
    130                     $config->{$key}->{$_} = $include->{$key}->{$_}; 
    131                 } 
    132             } elsif (ref($include->{$key}) eq 'ARRAY') { 
    133                 $add = [ $add ] unless ref($add) eq 'ARRAY'; 
    134                 push(@{ $config->{$key} }, @{ $include->{$key} }); 
    135             } elsif ($add) { 
    136                 $config->{$key} = $add; 
    137             } 
    138         } 
    139     } 
    140 } 
    141  
    142 sub load_recipes { 
    143     my($self, $config) = @_; 
    144  
    145     for (@{ $config->{recipes} }) { 
    146         $self->error("no such recipe to $_") unless $config->{define_recipes}->{$_}; 
    147         my $plugin = $config->{define_recipes}->{$_}; 
    148         $plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY'; 
    149         push(@{ $config->{plugins} }, @{ $plugin }); 
    150103    } 
    151104} 
  • trunk/plagger/t/core/include.t

    r1326 r1327  
    66run { 
    77    my $block = shift; 
    8     my $config = YAML::Load($block->input); 
    98 
    10     my $context = bless {}, 'Plagger'; 
    11     $context->load_include($config); 
    12     $context->load_recipes($config); 
     9    my $loader = Plagger::ConfigLoader->new; 
     10    my $config = $loader->load(\$block->input); 
     11 
     12    $loader->load_include($config); 
     13    $loader->load_recipes($config); 
    1314 
    1415    # no need to compare these things