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

Last change on this file since 191 was 191, checked in by nanardon, 15 years ago
  • trap error on DB object creation
  • Property svn:keywords set to Id Rev
File size: 4.8 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        or return;
57    $base->load or return;
58    $base;
59}
60
61sub default_base_name {
62    my ($self) = @_;
63    $self->val('_default_', 'base', ($self->list_bases)[0]);
64}
65
66sub default_synchro_name {
67    my ($self) = @_;
68    $self->val('_default_', 'sync');
69}
70
71sub list_bases {
72    my ($self) = @_;
73    grep {
74        !m/^_.*_$/ &&
75        !m/^sync:/
76    } $self->Sections
77}
78
79sub list_synchro {
80    my ($self) = @_;
81    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
82}
83
84sub load_all_base {
85    my ($self) = @_;
86    foreach ($self->list_bases) {
87        $self->load_base($_) or do {
88            warn "Cannot load base $_\n";
89            return 0;
90        };
91    }
92    1;
93}
94
95sub default_synchro {
96    my ($self, %options) = @_;
97    my $syncname = $self->default_synchro_name or return;
98    $self->create_synchro($syncname, %options);
99}
100
101sub sync_from_name {
102    my ($self, $syncname) = @_;
103    return if (!$syncname);
104    $self->val("sync:$syncname", 'from', $self->default_base_name);
105}
106
107sub create_synchro {
108    my ($self, $name, %options) = @_;
109
110    # taking options from config
111    if ($name) {
112        foreach my $param ($self->Parameters("sync:$name")) {
113            if (!defined($options{$param})) {
114                my @args = $self->val("sync:$name", $param);
115                $options{$param} = ($args[1] || $param eq 'to')
116                    ? [ @args ]
117                    : $args[0];
118            }
119        }
120    }
121
122    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
123
124    my @labto = map { $self->base($_) } @{ $options{to} || []} or return;
125
126    my $sync = LATMOS::Accounts::Synchro->new(
127        $labfrom, [ @labto ],
128        state_file => $self->val('_default_', 'state_file'),
129        %options,
130        name => $name,
131    );
132}
133
134sub sync_access {
135    my ($self, $name, %options) = @_;
136
137    my @obases;
138    if ($name) {
139        @obases =
140        (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));
141    } elsif(@{ $options{bases} || []}) {
142        @obases = map { $self->base($_) } @{ $options{bases} || []};
143    } elsif (my $sname = $self->default_synchro_name) {
144        @obases = (map { $self->base($_) }
145            ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))
146        );
147    }
148
149    LATMOS::Accounts::SynchAccess->new([ @obases ]);
150}
151
1521;
153
154__END__
155# Below is stub documentation for your module. You'd better edit it!
156
157=head1 NAME
158
159LATMOS::Accounts - Perl extension for blah blah blah
160
161=head1 SYNOPSIS
162
163  use LATMOS::Accounts;
164  blah blah blah
165
166=head1 DESCRIPTION
167
168Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the
169author of the extension was negligent enough to leave the stub
170unedited.
171
172Blah blah blah.
173
174=head2 EXPORT
175
176None by default.
177
178
179
180=head1 SEE ALSO
181
182Mention other useful documentation such as the documentation of
183related modules or operating system documentation (such as man pages
184in UNIX), or any relevant external documentation such as RFCs or
185standards.
186
187If you have a mailing list set up for your module, mention it here.
188
189If you have a web site set up for your module, mention it here.
190
191=head1 AUTHOR
192
193Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt>
194
195=head1 COPYRIGHT AND LICENSE
196
197Copyright (C) 2009 by Thauvin Olivier
198
199This library is free software; you can redistribute it and/or modify
200it under the same terms as Perl itself, either Perl version 5.10.0 or,
201at your option, any later version of Perl 5 you may have available.
202
203
204=cut
Note: See TracBrowser for help on using the repository browser.