source: LATMOS-Accounts/lib/LATMOS/Accounts.pm @ 137

Last change on this file since 137 was 137, checked in by nanardon, 15 years ago
  • manage default value
  • Property svn:keywords set to Id Rev
File size: 4.5 KB
Line 
1package LATMOS::Accounts;
2
3use 5.010000;
4use strict;
5use warnings;
6use base qw(Config::IniFiles);
7use LATMOS::Accounts::Bases;
8use LATMOS::Accounts::Synchro;
9use LATMOS::Accounts::SynchAccess;
10
11our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
12
13sub new {
14    my ($class, $config) = @_;
15
16    $config ||= '/etc/latmos-account.ini';
17
18    my $self = Config::IniFiles->new(
19        -file => $config,
20        '-default' => '_default_',
21    );
22
23    bless($self, $class)
24}
25
26sub base {
27    my ($self, $section) = @_;
28    # this method perform a cache
29    $self->{_bases}{$section} and return $self->{_bases}{$section};
30    $self->load_base($section) ? $self->{_bases}{$section} : undef;
31}
32
33sub default_base {
34    my ($self) = @_;
35    my $default = $self->default_base_name or return;
36    $self->base($default);
37}
38
39# load or a if need base
40sub load_base {
41    my ($self, $section) = @_;
42    return ($self->{_bases}{$section} ||= $self->_load_base($section))
43        ? 1
44        : 0;
45}
46
47# do the bad work
48sub _load_base {
49    my ($self, $section) = @_;
50    my $type = $self->val($section, 'type') or return;
51    my %params = (
52        map { $_ => ($self->val($section, $_)) } $self->Parameters($section),
53        defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },
54    );
55    my $base = LATMOS::Accounts::Bases->new($type, %params, label => $section);
56    $base->load or return;
57    $base;
58}
59
60sub default_base_name {
61    my ($self) = @_;
62    $self->val('_default_', 'base', ($self->list_bases)[0]);
63}
64
65sub default_synchro_name {
66    my ($self) = @_;
67    $self->val('_default_', 'sync');
68}
69
70sub list_bases {
71    my ($self) = @_;
72    grep {
73        !m/^_.*_$/ &&
74        !m/^sync:/
75    } $self->Sections
76}
77
78sub list_synchro {
79    my ($self) = @_;
80    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
81}
82
83sub load_all_base {
84    my ($self) = @_;
85    foreach ($self->list_bases) {
86        $self->load_base($_) or do {
87            warn "Cannot load base $_\n";
88            return 0;
89        };
90    }
91    1;
92}
93
94sub default_synchro {
95    my ($self, %options) = @_;
96    my $syncname = $self->default_synchro_name or return;
97    $self->create_synchro($syncname, %options);
98}
99
100sub sync_from_name {
101    my ($self, $syncname) = @_;
102    return if (!$syncname);
103    $self->val("sync:$syncname", 'from', $self->default_base_name);
104}
105
106sub create_synchro {
107    my ($self, $name, %options) = @_;
108
109    # taking options from config
110    if ($name) {
111        foreach my $param ($self->Parameters("sync:$name")) {
112            if (!defined($options{$param})) {
113                my @args = $self->val("sync:$name", $param);
114                $options{$param} = ($args[1] || $param eq 'to')
115                    ? [ @args ]
116                    : $args[0];
117            }
118        }
119    }
120
121    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
122
123    my @labto = map { $self->base($_) } @{ $options{to} || []} or return;
124
125    my $sync = LATMOS::Accounts::Synchro->new(
126        $labfrom, [ @labto ],
127        state_file => $self->val('_default_', 'state_file'),
128        %options,
129        name => $name,
130    );
131}
132
133sub sync_access {
134    my ($self, $name, %options) = @_;
135
136    my @obases = $name 
137        ? (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')))
138        : map { $self->base($_) } @{ $options{bases} || []};
139    LATMOS::Accounts::SynchAccess->new([ @obases ]);
140}
141
1421;
143
144__END__
145# Below is stub documentation for your module. You'd better edit it!
146
147=head1 NAME
148
149LATMOS::Accounts - Perl extension for blah blah blah
150
151=head1 SYNOPSIS
152
153  use LATMOS::Accounts;
154  blah blah blah
155
156=head1 DESCRIPTION
157
158Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the
159author of the extension was negligent enough to leave the stub
160unedited.
161
162Blah blah blah.
163
164=head2 EXPORT
165
166None by default.
167
168
169
170=head1 SEE ALSO
171
172Mention other useful documentation such as the documentation of
173related modules or operating system documentation (such as man pages
174in UNIX), or any relevant external documentation such as RFCs or
175standards.
176
177If you have a mailing list set up for your module, mention it here.
178
179If you have a web site set up for your module, mention it here.
180
181=head1 AUTHOR
182
183Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt>
184
185=head1 COPYRIGHT AND LICENSE
186
187Copyright (C) 2009 by Thauvin Olivier
188
189This library is free software; you can redistribute it and/or modify
190it under the same terms as Perl itself, either Perl version 5.10.0 or,
191at your option, any later version of Perl 5 you may have available.
192
193
194=cut
Note: See TracBrowser for help on using the repository browser.