Net/Config.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 139 CC 16 SCAR 27.1
CC
16
Cov
0%
save_config
42.0
load_config
30.0
net
12.0
CRAP 15.0
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Config;
5  
6 use File::Find::Rule;
7 use File::Spec;
8 use FindBin;
9 use Hash::Merge qw/ merge /;
10 use Readonly;
11 use YAML::Syck qw/ LoadFile DumpFile /;
12  
13 =head1 NAME
14  
15 Bot::Net::Config - the configuration for your bot net
16  
17 =head1 SYNOPSIS
18  
19 my $config = Bot::Net->config;
20  
21 my $bot_config = $config->bot('CopyBot');
22 my $server_config = $config->server('MasterHost');
23  
24 head1 DESCRIPTION
25  
26 This module loads and stores the configuration for your bot net. The bot net configuration is typically stored in F<etc/config.yml> of your bot net application directory. However, you can have multiple configurations, which can be specified to the spawn command using the C<--config> option.
27  
28 =head1 METHODS
29  
30 =head2 new
31  
32 You should not need to call this method directory. Instead:
33  
34 my $config = Bot::Net->config;
35  
36 will call the constructor as needed.
37  
38 =cut
39  
40 sub new {
41 my $class = shift;
42 bless {}, $class;
43 }
44  
45 sub _search_paths {
46 my @paths = ( [ $FindBin::Bin, '..', 'etc' ] );
47  
48 if ($ENV{BOT_NET_CONFIG_PATH}) {
49 my @env_paths = split /:/, $ENV{BOT_NET_CONFIG_PATH};
50 unshift @paths, map { [ $_ ] } @env_paths;
51 }
52  
53 return @paths;
54 }
55  
56 sub _search_for_file {
57 my @file_path = @_;
58  
59 for my $search_path (_search_paths()) {
60 my $file_name = File::Spec->catfile(@$search_path, @file_path);
61 return $file_name if -f $file_name;
62 }
63  
64 return undef;
65 }
66  
67 =head2 net [ KEY ]
68  
69 The main configuration file for your L<Bot::Net> application is stored in F<etc/net.yml>. This returns a single value from that file if a C<KEY> is specified or returns the entire configuration if no key is given.
70  
71 =cut
72  
73 sub net {
74 my $self = shift;
75 my $key = shift;
76  
77 $self->load_config('net') unless defined $self->{net};
78  
79 return defined $key ? $self->{net}{$key} : $self->{net};
80 }
81  
82 =head2 net_file
83  
84 Returns the path to the main configuration file for your L<Bot::Net> application, which is stored in F<etc/net.yml>.
85  
86 =cut
87  
88 sub net_file {
89 my $self = shift;
90  
91 return _search_for_file('net.yml');
92 }
93  
94 =head2 load_config TYPE NAME
95  
96 Reloads one of the configuration files. You normally won't need to call this method, it will be called automatically.
97  
98 =cut
99  
100 sub load_config {
101 my $self = shift;
102 my $type = shift;
103 my $name = shift;
104  
105 my $filename = $type eq 'net' ? $self->net_file
106 : $type eq 'bot' ? $self->bot_file($name)
107 : $type eq 'server' ? $self->server_file($name)
108 : die "I don't know how to load a $type config file.";
109  
110 my $config = LoadFile($filename);
111  
112 if ($type eq 'net') {
113 $self->{$type} = $config;
114 }
115 else {
116 $self->{$type}{$name} = $config;
117 }
118 }
119  
120 =head2 save_config
121  
122 Saves a configuration file. This can be useful when making changes to the configuration from within a bot that you would liked saved for future use.
123  
124 =cut
125  
126 sub save_config {
127 my $self = shift;
128 my $type = shift;
129 my $name = shift;
130  
131 my $config = $type eq 'net' ? $self->{$type}
132 : $self->{$type}{$name}
133 ;
134  
135 die "No configuration found for $type:$name."
136 unless defined $config;
137  
138 my $filename = $type eq 'net' ? $self->net_file
139 : $type eq 'bot' ? $self->bot_file($name)
140 : $type eq 'server' ? $self->server_file($name)
141 : die "I don't know how to save a $type config file.";
142  
143 DumpFile($filename, $config);
144 }
145  
146 =head2 server_file NAME
147  
148 Returns the location of the configuration for the server named C<NAME>.
149  
150 =cut
151  
152 sub server_file {
153 my $self = shift;
154 my $name = shift;
155  
156 my @path = split /::/, $name;
157 $path[ $#path ] .= '.yml';
158  
159 return _search_for_file('server', @path);
160 }
161  
162 =head2 server NAME
163  
164 Returns the configuration for the named server.
165  
166 =cut
167  
168 sub server {
169 my $self = shift;
170 my $name = shift;
171  
172 $self->load_config( server => $name )
173 unless defined $self->{server}{$name};
174  
175 return $self->{server}{$name};
176 }
177  
178 =head2 bot_file NAME
179  
180 Returns the location of the bot configuration file for the named bot.
181  
182 =cut
183  
184 sub bot_file {
185 my $self = shift;
186 my $name = shift;
187  
188 my @path = split /::/, $name;
189 $path[ $#path ] .= '.yml';
190  
191 return _search_for_file('bot', @path);
192 }
193  
194 =head2 bot NAME
195  
196 Returns teh configuration for the named bot. This will include the default configuration and bot-specific overrides.
197  
198 =cut
199  
200 sub bot {
201 my $self = shift;
202 my $name = shift;
203  
204 $self->load_config( bot => $name )
205 unless defined $self->{bot}{$name};
206  
207 return $self->{bot}{$name};
208 }
209  
210 =head1 CONFIGURATION FILE LOCATIONS
211  
212 =head2 CONFIGURATION DIRECTORIES
213  
214 L<Bot::Net> will search for configuration files in the following places (and in the following order). The first file found according to this order will be used.
215  
216 =over
217  
218 =item 1.
219  
220 If the environment variable C<BOT_NET_CONFIG_PATH> is set. It is assumed to be a list of one or more paths (separated by colons) containing the names of the directories to search for configuration files. The directories will be searched in the order given in the variable.
221  
222 =item 2.
223  
224 The program will look for the binary file (using L<FindBin>) that was executed (probably F<bin/botnet>) and find the F<etc> directory one level above the directory containing the script. For example, if you're running your bot net from F</home/sterling/MyNet/bin/botnet>, it would look in F</home/sterling/MyNet/etc> for your configuration files.
225  
226 =back
227  
228 =head2 CONFIGURATION FILES
229  
230 Within the directory found, the files will be named as follows:
231  
232 =head3 PRIMARY BOT NET CONFIGURATION
233  
234 The F<net.yml> file must be found in this directory.
235  
236 =head3 SERVER CONFIGURATION
237  
238 Server config files will be found in a subdirectory named F<server> and then subdirectories based upon the name of the server.
239  
240 For example, a server package named C<MyNet::Server::Foo::Bar::Master> would find it's configuration file under F<server/Foo/Bar/Master.yml>.
241  
242 =head3 BOT CONFIGURATION
243  
244 Bot configuration files are in the subdirectory named F<bot> and then subdirectories based upon the name of the bot.
245  
246 For example, a bot package named C<MyNet::Bot::Foo::Bar::ChanOp> would find it's configuration file under F<bot/Foo/Bar/ChanOp.yml>.
247  
248 =head1 AUTHORS
249  
250 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
255  
256 This program is free software and may be modified and distributed under the same terms as Perl itself.
257  
258 =cut
259  
260 1;