1 | package LATMOS::Accounts; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use base qw(Config::IniFiles); |
---|
7 | use LATMOS::Accounts::Bases; |
---|
8 | use LATMOS::Accounts::Synchro; |
---|
9 | use LATMOS::Accounts::SynchAccess; |
---|
10 | |
---|
11 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
12 | |
---|
13 | sub 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 | |
---|
26 | sub 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 | |
---|
33 | sub 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 |
---|
40 | sub 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 |
---|
48 | sub _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 | |
---|
60 | sub default_base_name { |
---|
61 | my ($self) = @_; |
---|
62 | $self->val('_default_', 'base', ($self->list_bases)[0]); |
---|
63 | } |
---|
64 | |
---|
65 | sub default_synchro_name { |
---|
66 | my ($self) = @_; |
---|
67 | $self->val('_default_', 'sync'); |
---|
68 | } |
---|
69 | |
---|
70 | sub list_bases { |
---|
71 | my ($self) = @_; |
---|
72 | grep { |
---|
73 | !m/^_.*_$/ && |
---|
74 | !m/^sync:/ |
---|
75 | } $self->Sections |
---|
76 | } |
---|
77 | |
---|
78 | sub list_synchro { |
---|
79 | my ($self) = @_; |
---|
80 | grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections |
---|
81 | } |
---|
82 | |
---|
83 | sub 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 | |
---|
94 | sub default_synchro { |
---|
95 | my ($self, %options) = @_; |
---|
96 | my $syncname = $self->default_synchro_name or return; |
---|
97 | $self->create_synchro($syncname, %options); |
---|
98 | } |
---|
99 | |
---|
100 | sub sync_from_name { |
---|
101 | my ($self, $syncname) = @_; |
---|
102 | return if (!$syncname); |
---|
103 | $self->val("sync:$syncname", 'from', $self->default_base_name); |
---|
104 | } |
---|
105 | |
---|
106 | sub 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 | |
---|
133 | sub 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 | |
---|
142 | 1; |
---|
143 | |
---|
144 | __END__ |
---|
145 | # Below is stub documentation for your module. You'd better edit it! |
---|
146 | |
---|
147 | =head1 NAME |
---|
148 | |
---|
149 | LATMOS::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 | |
---|
158 | Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the |
---|
159 | author of the extension was negligent enough to leave the stub |
---|
160 | unedited. |
---|
161 | |
---|
162 | Blah blah blah. |
---|
163 | |
---|
164 | =head2 EXPORT |
---|
165 | |
---|
166 | None by default. |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | =head1 SEE ALSO |
---|
171 | |
---|
172 | Mention other useful documentation such as the documentation of |
---|
173 | related modules or operating system documentation (such as man pages |
---|
174 | in UNIX), or any relevant external documentation such as RFCs or |
---|
175 | standards. |
---|
176 | |
---|
177 | If you have a mailing list set up for your module, mention it here. |
---|
178 | |
---|
179 | If you have a web site set up for your module, mention it here. |
---|
180 | |
---|
181 | =head1 AUTHOR |
---|
182 | |
---|
183 | Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt> |
---|
184 | |
---|
185 | =head1 COPYRIGHT AND LICENSE |
---|
186 | |
---|
187 | Copyright (C) 2009 by Thauvin Olivier |
---|
188 | |
---|
189 | This library is free software; you can redistribute it and/or modify |
---|
190 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
191 | at your option, any later version of Perl 5 you may have available. |
---|
192 | |
---|
193 | |
---|
194 | =cut |
---|