root/branches/hackathon-summary/plagger/lib/Plagger/ConfigLoader.pm

Revision 1479 (checked in by miyagawa, 2 years ago)

BIG CHANGE: decode YAML from UTF-8 to Unicode when we load config files.
Fixes #103

Line 
1 package Plagger::ConfigLoader;
2 use strict;
3 use Carp;
4 use Plagger::Walker;
5
6 sub new {
7     my $class = shift;
8     bless {}, $class;
9 }
10
11 sub load {
12     my($self, $stuff) = @_;
13
14     my $config;
15     if (-e $stuff && -r _) {
16         $config = YAML::LoadFile($stuff);
17         $self->{config_path} = $stuff;
18     } elsif (ref($stuff) && ref($stuff) eq 'SCALAR') {
19         $config = YAML::Load(${$stuff});
20     } elsif (ref($stuff) && ref($stuff) eq 'HASH') {
21         $config = Storable::dclone($stuff);
22     } else {
23         croak "Plagger::ConfigLoader->load: $stuff: $!";
24     }
25
26     unless ($config->{global} && $config->{global}->{no_decode_utf8}) {
27         Plagger::Walker->decode_utf8($config);
28     }
29
30     return $config;
31 }
32
33 sub load_include {
34     my($self, $config) = @_;
35
36     my $includes = $config->{include} or return;
37     $includes = [ $includes ] unless ref $includes;
38
39     for my $file (@$includes) {
40         my $include = YAML::LoadFile($file);
41
42         for my $key (keys %{ $include }) {
43             my $add = $include->{$key};
44             unless ($config->{$key}) {
45                 $config->{$key} = $add;
46                 next;
47             }
48             if (ref($config->{$key}) eq 'HASH') {
49                 next unless ref($add) eq 'HASH';
50                 for (keys %{ $include->{$key} }) {
51                     $config->{$key}->{$_} = $include->{$key}->{$_};
52                 }
53             } elsif (ref($include->{$key}) eq 'ARRAY') {
54                 $add = [ $add ] unless ref($add) eq 'ARRAY';
55                 push(@{ $config->{$key} }, @{ $include->{$key} });
56             } elsif ($add) {
57                 $config->{$key} = $add;
58             }
59         }
60     }
61 }
62
63 sub load_recipes {
64     my($self, $config) = @_;
65
66     for (@{ $config->{recipes} }) {
67         $self->error("no such recipe to $_") unless $config->{define_recipes}->{$_};
68         my $plugin = $config->{define_recipes}->{$_};
69         $plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY';
70         push(@{ $config->{plugins} }, @{ $plugin });
71     }
72 }
73
74 1;
Note: See TracBrowser for help on using the browser.