[1] | 1 | package LATMOS::Accounts; |
---|
| 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
[22] | 6 | use base qw(Config::IniFiles); |
---|
| 7 | use LATMOS::Accounts::Bases; |
---|
[1] | 8 | |
---|
[22] | 9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
[1] | 10 | |
---|
[22] | 11 | sub new { |
---|
| 12 | my ($class, $config) = @_; |
---|
[1] | 13 | |
---|
[24] | 14 | $config ||= '/etc/latmos-account.ini'; |
---|
| 15 | |
---|
[22] | 16 | my $self = Config::IniFiles->new( |
---|
| 17 | -file => $config |
---|
| 18 | ); |
---|
[1] | 19 | |
---|
[22] | 20 | bless($self, $class) |
---|
| 21 | } |
---|
[1] | 22 | |
---|
[22] | 23 | sub base { |
---|
| 24 | my ($self, $section) = @_; |
---|
| 25 | # this method perform a cache |
---|
| 26 | $self->{_bases}{$section} and return $self->{_bases}{$section}; |
---|
| 27 | $self->load_base($section) ? $self->{_bases}{$section} : undef; |
---|
| 28 | } |
---|
[1] | 29 | |
---|
[24] | 30 | sub default_base { |
---|
| 31 | my ($self) = @_; |
---|
[34] | 32 | my $default = $self->default_base_name or return; |
---|
[24] | 33 | $self->base($default); |
---|
| 34 | } |
---|
| 35 | |
---|
[23] | 36 | # load or a if need base |
---|
[22] | 37 | sub load_base { |
---|
| 38 | my ($self, $section) = @_; |
---|
[23] | 39 | return ($self->{_bases}{$section} ||= $self->_load_base($section)) |
---|
[22] | 40 | ? 1 |
---|
| 41 | : 0; |
---|
| 42 | } |
---|
[1] | 43 | |
---|
[22] | 44 | # do the bad work |
---|
| 45 | sub _load_base { |
---|
| 46 | my ($self, $section) = @_; |
---|
| 47 | my $type = $self->val($section, 'type') or return; |
---|
[34] | 48 | my %params = map { $_ => ($self->val($section, $_)) } $self->Parameters($section); |
---|
[49] | 49 | my $base = LATMOS::Accounts::Bases->new($type, %params, label => $section); |
---|
[34] | 50 | $base->load or return; |
---|
| 51 | $base; |
---|
[22] | 52 | } |
---|
[1] | 53 | |
---|
[24] | 54 | sub default_base_name { |
---|
| 55 | my ($self) = @_; |
---|
| 56 | $self->val('_default_', 'base', ($self->list_bases)[0]); |
---|
| 57 | } |
---|
| 58 | |
---|
[23] | 59 | sub list_bases { |
---|
| 60 | my ($self) = @_; |
---|
| 61 | grep { |
---|
[24] | 62 | !m/^_.*_$/ |
---|
[23] | 63 | } $self->Sections |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | sub load_all_base { |
---|
| 67 | my ($self) = @_; |
---|
| 68 | foreach ($self->list_bases) { |
---|
| 69 | $self->load_base($_) or do { |
---|
| 70 | warn "Cannot load base $_\n"; |
---|
| 71 | return 0; |
---|
| 72 | }; |
---|
| 73 | } |
---|
| 74 | 1; |
---|
| 75 | } |
---|
| 76 | |
---|
[41] | 77 | sub config_sync_base { |
---|
| 78 | my ($self, $from) = @_; |
---|
| 79 | return split(/ /, $self->val($from, 'push', '')); |
---|
| 80 | } |
---|
| 81 | |
---|
[1] | 82 | 1; |
---|
[24] | 83 | |
---|
[1] | 84 | __END__ |
---|
| 85 | # Below is stub documentation for your module. You'd better edit it! |
---|
| 86 | |
---|
| 87 | =head1 NAME |
---|
| 88 | |
---|
| 89 | LATMOS::Accounts - Perl extension for blah blah blah |
---|
| 90 | |
---|
| 91 | =head1 SYNOPSIS |
---|
| 92 | |
---|
| 93 | use LATMOS::Accounts; |
---|
| 94 | blah blah blah |
---|
| 95 | |
---|
| 96 | =head1 DESCRIPTION |
---|
| 97 | |
---|
| 98 | Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the |
---|
| 99 | author of the extension was negligent enough to leave the stub |
---|
| 100 | unedited. |
---|
| 101 | |
---|
| 102 | Blah blah blah. |
---|
| 103 | |
---|
| 104 | =head2 EXPORT |
---|
| 105 | |
---|
| 106 | None by default. |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | =head1 SEE ALSO |
---|
| 111 | |
---|
| 112 | Mention other useful documentation such as the documentation of |
---|
| 113 | related modules or operating system documentation (such as man pages |
---|
| 114 | in UNIX), or any relevant external documentation such as RFCs or |
---|
| 115 | standards. |
---|
| 116 | |
---|
| 117 | If you have a mailing list set up for your module, mention it here. |
---|
| 118 | |
---|
| 119 | If you have a web site set up for your module, mention it here. |
---|
| 120 | |
---|
| 121 | =head1 AUTHOR |
---|
| 122 | |
---|
[41] | 123 | Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt> |
---|
[1] | 124 | |
---|
| 125 | =head1 COPYRIGHT AND LICENSE |
---|
| 126 | |
---|
| 127 | Copyright (C) 2009 by Thauvin Olivier |
---|
| 128 | |
---|
| 129 | This library is free software; you can redistribute it and/or modify |
---|
| 130 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 131 | at your option, any later version of Perl 5 you may have available. |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | =cut |
---|