- Timestamp:
- 06/01/12 08:25:18 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm
r1044 r1047 105 105 !m/^sync:/ 106 106 } $self->Sections 107 } 108 109 =head2 default_base_name 110 111 Return the default base name according config file 112 113 =cut 114 115 sub default_base_name { 116 my ($self) = @_; 117 $self->val('_default_', 'base', ($self->list_bases)[0]); 118 } 119 120 =head2 base($basename) 121 122 Return a L<LATMOS::Accounts::Base> object over base named $basename 123 defined in the config file. 124 125 The base is loaded by this function. 126 127 =cut 128 129 sub base { 130 my ($self, $section) = @_; 131 # this method perform a cache 132 $self->_load_base($section || $self->default_base_name); 133 } 134 135 # do the bad work 136 sub _load_base { 137 my ($self, $section) = @_; 138 my $type = $self->val($section, 'type') or return; 139 my %params = ( 140 map { $_ => ($self->val($section, $_)) } $self->Parameters($section), 141 defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') }, 142 ); 143 my $base = LATMOS::Accounts::Bases->new( 144 $type, 145 %params, 146 label => $section, 147 acls => $self->{_acls}, 148 allowed_values => $self->{_allowed_values}, 149 configdir => $self->_configdir, 150 _la => $self, 151 ) or do { 152 la_log(LA_WARN, "Cannot instanciate base $section ($type)"); 153 return; 154 }; 155 $base->load or return; 156 $base; 157 } 158 159 =head2 list_synchro 160 161 List synchronisation setup in L<latmos-accounts.ini> 162 163 =cut 164 165 sub list_synchro { 166 my ($self) = @_; 167 grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections 168 } 169 170 =head2 default_synchro_name 171 172 Return de default synchronisation name 173 174 =cut 175 176 sub default_synchro_name { 177 my ($self) = @_; 178 $self->val('_default_', 'sync'); 179 } 180 181 =head2 default_synchro 182 183 Return a reference to default synchronisation object 184 185 =cut 186 187 sub default_synchro { 188 my ($self, %options) = @_; 189 my $syncname = $self->default_synchro_name or do { 190 la_log(LA_ERR, 'Cannot find default synchro in config'); 191 return; 192 }; 193 $self->create_synchro($syncname, %options); 194 } 195 196 =head2 create_synchro($name, %options) 197 198 Return a reference to synchronisation object for C<$name> synchronisation. 199 200 =cut 201 202 sub create_synchro { 203 my ($self, $name, %options) = @_; 204 205 # taking options from config 206 if ($name) { 207 foreach my $param ($self->Parameters("sync:$name")) { 208 if (!defined($options{$param})) { 209 my @args = $self->val("sync:$name", $param); 210 $options{$param} = ($args[1] || $param eq 'to') 211 ? [ @args ] 212 : $args[0]; 213 } 214 } 215 } 216 217 my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base; 218 219 my @labto = 220 grep { $_ } 221 map { $self->base($_) } 222 @{ $options{to} || []} 223 or do { 224 la_log(LA_ERR, "No destination base load in this synchro"); 225 return; 226 }; 227 228 my $sync = LATMOS::Accounts::Synchro->new( 229 $labfrom, [ @labto ], 230 state_dir => ($self->val('_default_', 'state_dir') || undef), 231 %options, 232 name => $name, 233 ); 234 } 235 236 =head2 sync_access($name, %options) 237 238 Return a L<LATMOS::Accounts::SynchAccess> object over C<$name> synchronisation. 239 240 =cut 241 242 sub sync_access { 243 my ($self, $name, %options) = @_; 244 245 my @obases; 246 if ($name) { 247 @obases = 248 (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to'))); 249 } elsif(@{ $options{bases} || []}) { 250 @obases = map { $self->base($_) } @{ $options{bases} || []}; 251 } elsif (my $sname = $self->default_synchro_name) { 252 @obases = (map { $self->base($_) } 253 ($self->sync_from_name($sname), $self->val("sync:$sname", 'to')) 254 ); 255 } 256 257 LATMOS::Accounts::SynchAccess->new([ @obases ]); 107 258 } 108 259 … … 137 288 } 138 289 139 =head2 default_base_name140 141 Return the default base name according config file142 143 =cut144 145 sub default_base_name {146 my ($self) = @_;147 $self->val('_default_', 'base', ($self->list_bases)[0]);148 }149 150 =head2 base($basename)151 152 Return a L<LATMOS::Accounts::Base> object over base named $basename153 defined in the config file.154 155 The base is loaded by this function.156 157 =cut158 159 sub base {160 my ($self, $section) = @_;161 # this method perform a cache162 $self->_load_base($section || $self->default_base_name);163 }164 165 # do the bad work166 sub _load_base {167 my ($self, $section) = @_;168 my $type = $self->val($section, 'type') or return;169 my %params = (170 map { $_ => ($self->val($section, $_)) } $self->Parameters($section),171 defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },172 );173 my $base = LATMOS::Accounts::Bases->new(174 $type,175 %params,176 label => $section,177 acls => $self->{_acls},178 allowed_values => $self->{_allowed_values},179 configdir => $self->_configdir,180 _la => $self,181 ) or do {182 la_log(LA_WARN, "Cannot instanciate base $section ($type)");183 return;184 };185 $base->load or return;186 $base;187 }188 189 =head2 list_synchro190 191 List synchronisation setup in L<latmos-accounts.ini>192 193 =cut194 195 sub list_synchro {196 my ($self) = @_;197 grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections198 }199 200 =head2 default_synchro_name201 202 Return de default synchronisation name203 204 =cut205 206 sub default_synchro_name {207 my ($self) = @_;208 $self->val('_default_', 'sync');209 }210 211 =head2 default_synchro212 213 Return a reference to default synchronisation object214 215 =cut216 217 sub default_synchro {218 my ($self, %options) = @_;219 my $syncname = $self->default_synchro_name or do {220 la_log(LA_ERR, 'Cannot find default synchro in config');221 return;222 };223 $self->create_synchro($syncname, %options);224 }225 226 =head2 create_synchro($name, %options)227 228 Return a reference to synchronisation object for C<$name> synchronisation.229 230 =cut231 232 sub create_synchro {233 my ($self, $name, %options) = @_;234 235 # taking options from config236 if ($name) {237 foreach my $param ($self->Parameters("sync:$name")) {238 if (!defined($options{$param})) {239 my @args = $self->val("sync:$name", $param);240 $options{$param} = ($args[1] || $param eq 'to')241 ? [ @args ]242 : $args[0];243 }244 }245 }246 247 my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;248 249 my @labto =250 grep { $_ }251 map { $self->base($_) }252 @{ $options{to} || []}253 or do {254 la_log(LA_ERR, "No destination base load in this synchro");255 return;256 };257 258 my $sync = LATMOS::Accounts::Synchro->new(259 $labfrom, [ @labto ],260 state_dir => ($self->val('_default_', 'state_dir') || undef),261 %options,262 name => $name,263 );264 }265 266 =head2 sync_access($name, %options)267 268 Return a L<LATMOS::Accounts::SynchAccess> object over C<$name> synchronisation.269 270 =cut271 272 sub sync_access {273 my ($self, $name, %options) = @_;274 275 my @obases;276 if ($name) {277 @obases =278 (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));279 } elsif(@{ $options{bases} || []}) {280 @obases = map { $self->base($_) } @{ $options{bases} || []};281 } elsif (my $sname = $self->default_synchro_name) {282 @obases = (map { $self->base($_) }283 ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))284 );285 }286 287 LATMOS::Accounts::SynchAccess->new([ @obases ]);288 }289 290 290 1; 291 291
Note: See TracChangeset
for help on using the changeset viewer.