| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | package Metabrik::Core::Context; | ||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | # Breaking.Feature.Fix | ||
| 9 | our $VERSION = '1.41'; | ||
| 10 | our $FIX = '0'; | ||
| 11 | |||
| 12 | 2 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | sub brik_properties { | ||
| 15 | return { | ||
| 16 | revision => '$Revision$', | ||
| 17 | tags => [ qw(main core) ], | ||
| 18 | attributes => { | ||
| 19 | _lp => [ qw(INTERNAL) ], | ||
| 20 | }, | ||
| 21 | commands => { | ||
| 22 | new_brik_run => [ qw(Brik Command Args) ], | ||
| 23 | use => [ qw(Brik) ], | ||
| 24 | set => [ qw(Brik Attribute Value) ], | ||
| 25 | get => [ qw(Brik Attribute) ], | ||
| 26 | run => [ qw(Brik Command) ], | ||
| 27 | do => [ qw(Code) ], | ||
| 28 | call => [ qw(Code) ], | ||
| 29 | variables => [ ], | ||
| 30 | find_available => [ ], | ||
| 31 | update_available => [ ], | ||
| 32 | available => [ ], | ||
| 33 | is_available => [ qw(Brik) ], | ||
| 34 | used => [ ], | ||
| 35 | get_used => [ qw(Brik) ], | ||
| 36 | is_used => [ qw(Brik) ], | ||
| 37 | not_used => [ ], | ||
| 38 | is_not_used => [ qw(Brik) ], | ||
| 39 | status => [ ], | ||
| 40 | reuse => [ ], | ||
| 41 | save_state => [ qw(Brik) ], | ||
| 42 | restore_state => [ qw(Brik) ], | ||
| 43 | }, | ||
| 44 | require_modules => { | ||
| 45 | 'Data::Dump' => [ qw(dump) ], | ||
| 46 | 'File::Find' => [ ], | ||
| 47 | 'Lexical::Persistence' => [ ], | ||
| 48 | 'Module::Reload' => [ ], | ||
| 49 | 'Metabrik::Core::Global' => [ ], | ||
| 50 | 'Metabrik::Core::Log' => [ ], | ||
| 51 | 'Metabrik::Core::Shell' => [ ], | ||
| 52 | }, | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Only used to avoid compile-time errors | ||
| 57 | my $CON; | ||
| 58 | my $SHE; | ||
| 59 | my $LOG; | ||
| 60 | my $GLO; | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $self = shift->SUPER::new( | ||
| 64 | @_, | ||
| 65 | ); | ||
| 66 | |||
| 67 | eval { | ||
| 68 | my $lp = Lexical::Persistence->new; | ||
| 69 | $lp->set_context(_ => { | ||
| 70 | '$CON' => 'undef', | ||
| 71 | '$SHE' => 'undef', | ||
| 72 | '$LOG' => 'undef', | ||
| 73 | '$GLO' => 'undef', | ||
| 74 | '$USE' => 'undef', | ||
| 75 | '$SET' => 'undef', | ||
| 76 | '$GET' => 'undef', | ||
| 77 | '$RUN' => 'undef', | ||
| 78 | '$ERR' => 'undef', | ||
| 79 | '$MSG' => 'undef', | ||
| 80 | '$REF' => 'undef', | ||
| 81 | }); | ||
| 82 | $lp->call(sub { | ||
| 83 | my %args = @_; | ||
| 84 | |||
| 85 | $CON = $args{self}; | ||
| 86 | |||
| 87 | $CON->{used} = { | ||
| 88 | 'core::context' => $CON, | ||
| 89 | 'core::global' => Metabrik::Core::Global->new, | ||
| 90 | 'core::log' => Metabrik::Core::Log->new, | ||
| 91 | 'core::shell' => Metabrik::Core::Shell->new, | ||
| 92 | }; | ||
| 93 | $CON->{available} = { }; | ||
| 94 | $CON->{set} = { }; | ||
| 95 | |||
| 96 | $CON->{log} = $CON->{used}->{'core::log'}; | ||
| 97 | $CON->{global} = $CON->{used}->{'core::global'}; | ||
| 98 | $CON->{shell} = $CON->{used}->{'core::shell'}; | ||
| 99 | $CON->{context} = $CON->{used}->{'core::context'}; | ||
| 100 | |||
| 101 | $SHE = $CON->{shell}; | ||
| 102 | $LOG = $CON->{log}; | ||
| 103 | $GLO = $CON->{global}; | ||
| 104 | |||
| 105 | # When new() was done, some Attributes were empty. We fix that here. | ||
| 106 | for (qw(core::context core::global core::shell core::log)) { | ||
| 107 | $CON->{used}->{$_}->{context} = $CON; | ||
| 108 | $CON->{used}->{$_}->{log} = $CON->{log}; | ||
| 109 | $CON->{used}->{$_}->{global} = $CON->{global}; | ||
| 110 | $CON->{used}->{$_}->{shell} = $CON->{shell}; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $ERR = 0; | ||
| 114 | |||
| 115 | return 1; | ||
| 116 | }, self => $self); | ||
| 117 | $self->_lp($lp); | ||
| 118 | }; | ||
| 119 | if ($@) { | ||
| 120 | chomp($@); | ||
| 121 | die("[F] core::context: new: unable to create context: $@\n"); | ||
| 122 | } | ||
| 123 | |||
| 124 | return $self->brik_preinit; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub new_brik_run { | ||
| 128 | my $self = shift; | ||
| 129 | my ($brik, $command, @args) = @_; | ||
| 130 | |||
| 131 | my $con = Metabrik::Core::Context->new or return; | ||
| 132 | # We have to init because some Briks like brik::tool will search context information | ||
| 133 | # like available Briks, for instance. | ||
| 134 | $con->brik_init or return; | ||
| 135 | |||
| 136 | $con->use($brik) or return; | ||
| 137 | my $data = $con->run($brik, $command, @args) or return; | ||
| 138 | $con->brik_fini; | ||
| 139 | |||
| 140 | # Compatibility with file::dump Brik | ||
| 141 | print Data::Dump::dump($data)."\n"; | ||
| 142 | |||
| 143 | return $con; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub brik_init { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $r = $self->update_available; | ||
| 150 | if (! defined($r)) { | ||
| 151 | return $self->log->error("brik_init: unable to init Brik [core::context]: ". | ||
| 152 | "update_available failed" | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | |||
| 156 | return $self->SUPER::brik_init(@_); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub do { | ||
| 160 | my $self = shift; | ||
| 161 | my ($code) = @_; | ||
| 162 | |||
| 163 | if (! defined($code)) { | ||
| 164 | return $self->log->error($self->brik_help_run('do')); | ||
| 165 | } | ||
| 166 | |||
| 167 | my $lp = $self->_lp; | ||
| 168 | |||
| 169 | my $res; | ||
| 170 | eval { | ||
| 171 | $res = $lp->do($code); | ||
| 172 | }; | ||
| 173 | if ($@) { | ||
| 174 | chomp($@); | ||
| 175 | return $self->log->error("do: $@"); | ||
| 176 | } | ||
| 177 | |||
| 178 | $self->log->debug("do: returned[".(defined($res) ? $res : 'undef')."]"); | ||
| 179 | |||
| 180 | return defined($res) ? $res : 'undef'; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub call { | ||
| 184 | my $self = shift; | ||
| 185 | my ($subref, %args) = @_; | ||
| 186 | |||
| 187 | if (! defined($subref)) { | ||
| 188 | return $self->log->error($self->brik_help_run('call')); | ||
| 189 | } | ||
| 190 | |||
| 191 | my $lp = $self->_lp; | ||
| 192 | |||
| 193 | my $res; | ||
| 194 | eval { | ||
| 195 | $res = $lp->call($subref, %args); | ||
| 196 | }; | ||
| 197 | if ($@) { | ||
| 198 | chomp($@); | ||
| 199 | my @list = caller(); | ||
| 200 | my $file = $list[1]; | ||
| 201 | my $line = $list[2]; | ||
| 202 | if ($self->log->level > 2) { | ||
| 203 | return $self->log->debug("call: $@ (source file [$file] at line [$line])"); | ||
| 204 | } | ||
| 205 | return $self->log->error("call: $@"); | ||
| 206 | } | ||
| 207 | |||
| 208 | return $res; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub variables { | ||
| 212 | my $self = shift; | ||
| 213 | |||
| 214 | my $res = $self->call(sub { | ||
| 215 | my @__ctx_variables = (); | ||
| 216 | |||
| 217 | for my $__ctx_variable (keys %{$CON->_lp->{context}->{_}}) { | ||
| 218 | next if $__ctx_variable !~ /^\$/; | ||
| 219 | next if $__ctx_variable =~ /^\$_/; | ||
| 220 | |||
| 221 | push @__ctx_variables, $__ctx_variable; | ||
| 222 | } | ||
| 223 | |||
| 224 | return \@__ctx_variables; | ||
| 225 | }); | ||
| 226 | |||
| 227 | return $res; | ||
| 228 | } | ||
| 229 | |||
| 230 | # Extracted from file::find Brik | ||
| 231 | sub _file_find { | ||
| 232 | my $self = shift; | ||
| 233 | my ($path_list) = @_; | ||
| 234 | |||
| 235 | # With these patterns, we include baseclass Briks like Metabrik/Baseclass.pm | ||
| 236 | my $dirpattern = 'Metabrik'; | ||
| 237 | my $filepattern = '.pm$'; | ||
| 238 | |||
| 239 | # Escape if we are searching for a directory hierarchy | ||
| 240 | $dirpattern =~ s/\//\\\//g; | ||
| 241 | |||
| 242 | my $dir_regex = qr/$dirpattern/; | ||
| 243 | my $file_regex = qr/$filepattern/; | ||
| 244 | my $dot_regex = qr/^\.$/; | ||
| 245 | my $dot2_regex = qr/^\.\.$/; | ||
| 246 | |||
| 247 | my @files = (); | ||
| 248 | |||
| 249 | my $sub = sub { | ||
| 250 | my $dir = $File::Find::dir; | ||
| 251 | my $file = $_; | ||
| 252 | # Skip dot and double dot directories | ||
| 253 | if ($file =~ $dot_regex || $file =~ $dot2_regex) { | ||
| 254 | } | ||
| 255 | elsif ($dir =~ $dir_regex && $file =~ $file_regex) { | ||
| 256 | push @files, "$dir/$file"; | ||
| 257 | } | ||
| 258 | }; | ||
| 259 | |||
| 260 | { | ||
| 261 | no warnings; | ||
| 262 | File::Find::find($sub, @$path_list); | ||
| 263 | }; | ||
| 264 | |||
| 265 | my %uniq_files = map { $_ => 1 } @files; | ||
| 266 | @files = map { s/^\.\///; $_ } @files; # Remove leading dot slash | ||
| 267 | @files = sort { $a cmp $b } keys %uniq_files; | ||
| 268 | |||
| 269 | return \@files; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub find_available { | ||
| 273 | my $self = shift; | ||
| 274 | |||
| 275 | # Read from @INC, exclude current directory | ||
| 276 | my @path_list = (); | ||
| 277 | for (@INC) { | ||
| 278 | next if /^\.$/; | ||
| 279 | push @path_list, $_; | ||
| 280 | } | ||
| 281 | |||
| 282 | my $found = $self->_file_find(\@path_list); | ||
| 283 | |||
| 284 | my %available = (); | ||
| 285 | for my $this (@$found) { | ||
| 286 | my $brik = $this; | ||
| 287 | $brik =~ s{/}{::}g; | ||
| 288 | $brik =~ s/^.*::Metabrik::(.*?)$/$1/; | ||
| 289 | $brik =~ s/.pm$//; | ||
| 290 | if (length($brik)) { | ||
| 291 | my $module = "Metabrik::$brik"; | ||
| 292 | $brik = lc($brik); | ||
| 293 | $available{$brik} = $module; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | return \%available; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub update_available { | ||
| 301 | my $self = shift; | ||
| 302 | |||
| 303 | my $h = $self->find_available; | ||
| 304 | |||
| 305 | my $r = $self->call(sub { | ||
| 306 | my %args = @_; | ||
| 307 | |||
| 308 | my $__ctx_available = $args{available}; | ||
| 309 | |||
| 310 | for my $__ctx_this (keys %$__ctx_available) { | ||
| 311 | eval("require ".$__ctx_available->{$__ctx_this}); | ||
| 312 | } | ||
| 313 | |||
| 314 | return $CON->{available} = $args{available}; | ||
| 315 | }, available => $h); | ||
| 316 | |||
| 317 | return $r; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub use { | ||
| 321 | my $self = shift; | ||
| 322 | my ($brik) = @_; | ||
| 323 | |||
| 324 | if (! defined($brik)) { | ||
| 325 | return $self->log->error($self->brik_help_run('use')); | ||
| 326 | } | ||
| 327 | |||
| 328 | my $r = $self->call(sub { | ||
| 329 | my %args = @_; | ||
| 330 | |||
| 331 | my $__ctx_brik = $args{brik}; | ||
| 332 | |||
| 333 | my $ERR = 0; | ||
| 334 | my $USE = 'undef'; | ||
| 335 | |||
| 336 | my $__ctx_brik_repository = ''; | ||
| 337 | my $__ctx_brik_category = ''; | ||
| 338 | my $__ctx_brik_module = ''; | ||
| 339 | |||
| 340 | if ($__ctx_brik =~ /^[a-z0-9]+::[a-z0-9]+$/) { | ||
| 341 | ($__ctx_brik_category, $__ctx_brik_module) = split('::', $__ctx_brik); | ||
| 342 | } | ||
| 343 | elsif ($__ctx_brik =~ /^[a-z0-9]+::[a-z0-9]+::[a-z0-9]+$/) { | ||
| 344 | ($__ctx_brik_repository, $__ctx_brik_category, $__ctx_brik_module) = split('::', $__ctx_brik); | ||
| 345 | } | ||
| 346 | else { | ||
| 347 | $ERR = 1; | ||
| 348 | my $MSG = "use: invalid format for Brik [$__ctx_brik]"; | ||
| 349 | die("$MSG\n"); | ||
| 350 | } | ||
| 351 | |||
| 352 | $CON->log->debug("repository[$__ctx_brik_repository]"); | ||
| 353 | $CON->log->debug("category[$__ctx_brik_category]"); | ||
| 354 | $CON->log->debug("module[$__ctx_brik_module]"); | ||
| 355 | |||
| 356 | $__ctx_brik_repository = ucfirst($__ctx_brik_repository); | ||
| 357 | $__ctx_brik_category = ucfirst($__ctx_brik_category); | ||
| 358 | $__ctx_brik_module = ucfirst($__ctx_brik_module); | ||
| 359 | |||
| 360 | my $__ctx_module = 'Metabrik::'.(length($__ctx_brik_repository) | ||
| 361 | ? $__ctx_brik_repository.'::' | ||
| 362 | : '').$__ctx_brik_category.'::'.$__ctx_brik_module; | ||
| 363 | |||
| 364 | $CON->log->debug("module2[$__ctx_brik_module]"); | ||
| 365 | |||
| 366 | if ($CON->is_used($__ctx_brik)) { | ||
| 367 | $ERR = 1; | ||
| 368 | my $MSG = "use: Brik [$__ctx_brik] already used"; | ||
| 369 | die("$MSG\n"); | ||
| 370 | } | ||
| 371 | |||
| 372 | eval("require $__ctx_module;"); | ||
| 373 | if ($@) { | ||
| 374 | chomp($@); | ||
| 375 | $ERR = 1; | ||
| 376 | my $MSG = "use: unable to use Brik [$__ctx_brik]: $@"; | ||
| 377 | die("$MSG\n"); | ||
| 378 | } | ||
| 379 | |||
| 380 | $USE = $__ctx_brik; | ||
| 381 | |||
| 382 | my $__ctx_new = $__ctx_module->new( | ||
| 383 | context => $CON, | ||
| 384 | global => $CON->{global}, | ||
| 385 | shell => $CON->{shell}, | ||
| 386 | log => $CON->{log}, | ||
| 387 | ); | ||
| 388 | #$__ctx_new->brik_init; # No init now. We wait first run() to let set() actions | ||
| 389 | if (! defined($__ctx_new)) { | ||
| 390 | $ERR = 1; | ||
| 391 | my $MSG = "use: unable to use Brik [$__ctx_brik]"; | ||
| 392 | die("$MSG\n"); | ||
| 393 | } | ||
| 394 | |||
| 395 | return $CON->{used}->{$__ctx_brik} = $__ctx_new; | ||
| 396 | }, brik => $brik); | ||
| 397 | |||
| 398 | return $r; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub reuse { | ||
| 402 | my $self = shift; | ||
| 403 | |||
| 404 | my %stat = (); | ||
| 405 | my @reloaded = (); | ||
| 406 | # Taken from Module::Reload | ||
| 407 | for my $entry (map { [ $_, $INC{$_} ] } keys %INC) { | ||
| 408 | my ($module, $file) = @$entry; | ||
| 409 | |||
| 410 | # Some entries don't have a file (XS related) | ||
| 411 | next unless defined($file); | ||
| 412 | |||
| 413 | if ($file eq $INC{"Module/Reload.pm"}) { | ||
| 414 | next; # Too confusing | ||
| 415 | } | ||
| 416 | |||
| 417 | local $^W = 0; # Disable 'use warnings'; | ||
| 418 | my $mtime = (stat $file)[9]; | ||
| 419 | if (! defined($stat{$file})) { | ||
| 420 | $stat{$file} = $^T; | ||
| 421 | } | ||
| 422 | |||
| 423 | next unless defined($mtime); | ||
| 424 | |||
| 425 | if ($mtime > $stat{$file}) { | ||
| 426 | delete $INC{$module}; | ||
| 427 | eval { | ||
| 428 | $SIG{__WARN__} = sub {}; | ||
| 429 | require $module; | ||
| 430 | }; | ||
| 431 | if ($@) { | ||
| 432 | chomp($@); | ||
| 433 | if ($self->log->level > 2) { | ||
| 434 | $self->log->debug("reuse: reloading module [$module] failed: [$@]"); | ||
| 435 | } | ||
| 436 | else { | ||
| 437 | $self->log->error("reuse: reloading module [$module] failed"); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | else { | ||
| 441 | push @reloaded, $module; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | $stat{$file} = $mtime; | ||
| 445 | } | ||
| 446 | |||
| 447 | for (@reloaded) { | ||
| 448 | $self->log->info("reuse: module [$_] successfully reloaded"); | ||
| 449 | } | ||
| 450 | |||
| 451 | return 1; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub available { | ||
| 455 | my $self = shift; | ||
| 456 | |||
| 457 | my $r = $self->call(sub { | ||
| 458 | return $CON->{available}; | ||
| 459 | }); | ||
| 460 | |||
| 461 | return $r; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub is_available { | ||
| 465 | my $self = shift; | ||
| 466 | my ($brik) = @_; | ||
| 467 | |||
| 468 | if (! defined($brik)) { | ||
| 469 | return $self->log->error($self->brik_help_run('is_available')); | ||
| 470 | } | ||
| 471 | |||
| 472 | my $available = $self->available; | ||
| 473 | if (exists($available->{$brik})) { | ||
| 474 | return 1; | ||
| 475 | } | ||
| 476 | |||
| 477 | return 0; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub used { | ||
| 481 | my $self = shift; | ||
| 482 | |||
| 483 | my $r = $self->call(sub { | ||
| 484 | return $CON->{used}; | ||
| 485 | }); | ||
| 486 | |||
| 487 | return $r; | ||
| 488 | } | ||
| 489 | |||
| 490 | sub get_used { | ||
| 491 | my $self = shift; | ||
| 492 | my ($brik) = @_; | ||
| 493 | |||
| 494 | if (! defined($brik)) { | ||
| 495 | return $self->log->error($self->brik_help_run('get_used')); | ||
| 496 | } | ||
| 497 | |||
| 498 | my $used = $self->used; | ||
| 499 | |||
| 500 | my $get = $used->{$brik}; | ||
| 501 | if (! defined($get)) { | ||
| 502 | return $self->log->error("get_used: Brik [$brik] not used"); | ||
| 503 | } | ||
| 504 | |||
| 505 | return $get; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub is_used { | ||
| 509 | my $self = shift; | ||
| 510 | my ($brik) = @_; | ||
| 511 | |||
| 512 | if (! defined($brik)) { | ||
| 513 | return $self->log->error($self->brik_help_run('is_used')); | ||
| 514 | } | ||
| 515 | |||
| 516 | my $used = $self->used; | ||
| 517 | if (exists($used->{$brik})) { | ||
| 518 | return 1; | ||
| 519 | } | ||
| 520 | |||
| 521 | return 0; | ||
| 522 | } | ||
| 523 | |||
| 524 | sub not_used { | ||
| 525 | my $self = shift; | ||
| 526 | |||
| 527 | my $status = $self->status; | ||
| 528 | |||
| 529 | my $r = {}; | ||
| 530 | my @not_used = @{$status->{not_used}}; | ||
| 531 | for my $this (@not_used) { | ||
| 532 | my @toks = split('::', $this); | ||
| 533 | |||
| 534 | my $repository = ''; | ||
| 535 | my $category = ''; | ||
| 536 | my $name = ''; | ||
| 537 | |||
| 538 | # Only baseclass Brik is considered | ||
| 539 | if (@toks == 1) { | ||
| 540 | $category = $this; | ||
| 541 | } | ||
| 542 | # No repository defined | ||
| 543 | elsif (@toks == 2) { | ||
| 544 | ($category, $name) = $this =~ /^(.*?)::(.*)/; | ||
| 545 | } | ||
| 546 | elsif (@toks > 2) { | ||
| 547 | ($repository, $category, $name) = $this =~ /^(.*?)::(.*?)::(.*)/; | ||
| 548 | } | ||
| 549 | |||
| 550 | my $class = 'Metabrik::'; | ||
| 551 | if (length($repository)) { | ||
| 552 | $class .= ucfirst($repository).'::'; | ||
| 553 | } | ||
| 554 | $class .= ucfirst($category).'::'; | ||
| 555 | $class .= ucfirst($name); | ||
| 556 | |||
| 557 | $class =~ s{::$}{}; | ||
| 558 | |||
| 559 | $r->{$this} = $class; | ||
| 560 | } | ||
| 561 | |||
| 562 | return $r; | ||
| 563 | } | ||
| 564 | |||
| 565 | sub is_not_used { | ||
| 566 | my $self = shift; | ||
| 567 | my ($brik) = @_; | ||
| 568 | |||
| 569 | if (! defined($brik)) { | ||
| 570 | return $self->log->error($self->brik_help_run('is_not_used')); | ||
| 571 | } | ||
| 572 | |||
| 573 | my $used = $self->not_used; | ||
| 574 | if (exists($used->{$brik})) { | ||
| 575 | return 1; | ||
| 576 | } | ||
| 577 | |||
| 578 | return 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | sub status { | ||
| 582 | my $self = shift; | ||
| 583 | |||
| 584 | my $available = $self->available; | ||
| 585 | my $used = $self->used; | ||
| 586 | |||
| 587 | my @used = (); | ||
| 588 | my @not_used = (); | ||
| 589 | |||
| 590 | for my $k (sort { $a cmp $b } keys %$available) { | ||
| 591 | exists($used->{$k}) ? push @used, $k : push @not_used, $k; | ||
| 592 | } | ||
| 593 | |||
| 594 | return { | ||
| 595 | used => \@used, | ||
| 596 | not_used => \@not_used, | ||
| 597 | }; | ||
| 598 | } | ||
| 599 | |||
| 600 | sub set { | ||
| 601 | my $self = shift; | ||
| 602 | my ($brik, $attribute, $value) = @_; | ||
| 603 | |||
| 604 | if (! defined($brik) || ! defined($attribute) || ! defined($value)) { | ||
| 605 | return $self->log->error($self->brik_help_run('set')); | ||
| 606 | } | ||
| 607 | |||
| 608 | my $r = $self->call(sub { | ||
| 609 | my %args = @_; | ||
| 610 | |||
| 611 | my $__ctx_brik = $args{brik}; | ||
| 612 | my $__ctx_attribute = $args{attribute}; | ||
| 613 | my $__ctx_value = $args{value}; | ||
| 614 | |||
| 615 | my $ERR = 0; | ||
| 616 | |||
| 617 | if (! $CON->is_used($__ctx_brik)) { | ||
| 618 | $ERR = 1; | ||
| 619 | my $MSG = "set: Brik [$__ctx_brik] not used"; | ||
| 620 | die("$MSG\n"); | ||
| 621 | } | ||
| 622 | |||
| 623 | if (! $CON->used->{$__ctx_brik}->brik_has_attribute($__ctx_attribute)) { | ||
| 624 | $ERR = 1; | ||
| 625 | my $MSG = "set: Brik [$__ctx_brik] has no Attribute [$__ctx_attribute]"; | ||
| 626 | die("$MSG\n"); | ||
| 627 | } | ||
| 628 | |||
| 629 | # Support variable lookups like '$array' as an Argument | ||
| 630 | # Example: set <Brik> <Attribute> $Arg | ||
| 631 | if ($__ctx_value =~ /^\$\w+/ || $__ctx_value =~ /^\@\$\w+/ | ||
| 632 | || $__ctx_value =~ /^\@\w+/ || $__ctx_value =~ /^\%\$\w+/ | ||
| 633 | || $__ctx_value =~ /^\%\w+/) { | ||
| 634 | eval { | ||
| 635 | $__ctx_value = $CON->_lp->do($__ctx_value); | ||
| 636 | }; | ||
| 637 | if ($@) { | ||
| 638 | chomp($@); | ||
| 639 | $ERR = 1; | ||
| 640 | my $MSG = "set: Brik [$__ctx_brik] has invalid Argument [$__ctx_value]"; | ||
| 641 | die("$MSG\n"); | ||
| 642 | } | ||
| 643 | } | ||
| 644 | # Support passing ARRAYs or HASHs or Perl code as an Argument | ||
| 645 | # Example: set <Brik> <Attribute> "[ qw(a b c) ]" | ||
| 646 | elsif ($__ctx_value =~ /^\[.*\]$/ || $__ctx_value =~ /^\{.*\}$/) { | ||
| 647 | eval { | ||
| 648 | $__ctx_value = $CON->_lp->do($__ctx_value); | ||
| 649 | }; | ||
| 650 | if ($@) { | ||
| 651 | chomp($@); | ||
| 652 | $ERR = 1; | ||
| 653 | my $MSG = "set: Brik [$__ctx_brik] has invalid Argument [$__ctx_value]"; | ||
| 654 | die("$MSG\n"); | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | $CON->{used}->{$__ctx_brik}->$__ctx_attribute($__ctx_value); | ||
| 659 | |||
| 660 | my $SET = $CON->{set}->{$__ctx_brik}->{$__ctx_attribute} = $__ctx_value; | ||
| 661 | |||
| 662 | my $REF = \$SET; | ||
| 663 | |||
| 664 | return $SET; | ||
| 665 | }, brik => $brik, attribute => $attribute, value => $value); | ||
| 666 | |||
| 667 | return $r; | ||
| 668 | } | ||
| 669 | |||
| 670 | sub get { | ||
| 671 | my $self = shift; | ||
| 672 | my ($brik, $attribute) = @_; | ||
| 673 | |||
| 674 | if (! defined($brik) || ! defined($attribute)) { | ||
| 675 | return $self->log->error($self->brik_help_run('get')); | ||
| 676 | } | ||
| 677 | |||
| 678 | my $r = $self->call(sub { | ||
| 679 | my %args = @_; | ||
| 680 | |||
| 681 | my $__ctx_brik = $args{brik}; | ||
| 682 | my $__ctx_attribute = $args{attribute}; | ||
| 683 | |||
| 684 | my $ERR = 0; | ||
| 685 | |||
| 686 | if (! $CON->is_used($__ctx_brik)) { | ||
| 687 | $ERR = 1; | ||
| 688 | my $MSG = "get: Brik [$__ctx_brik] not used"; | ||
| 689 | die("$MSG\n"); | ||
| 690 | } | ||
| 691 | |||
| 692 | if (! $CON->used->{$__ctx_brik}->brik_has_attribute($__ctx_attribute)) { | ||
| 693 | $ERR = 1; | ||
| 694 | my $MSG = "get: Brik [$__ctx_brik] has no Attribute [$__ctx_attribute]"; | ||
| 695 | die("$MSG\n"); | ||
| 696 | } | ||
| 697 | |||
| 698 | if (! defined($CON->{used}->{$__ctx_brik}->$__ctx_attribute)) { | ||
| 699 | return my $GET = 'undef'; | ||
| 700 | } | ||
| 701 | |||
| 702 | my $GET = $CON->{used}->{$__ctx_brik}->$__ctx_attribute; | ||
| 703 | |||
| 704 | my $REF = \$GET; | ||
| 705 | |||
| 706 | return $GET; | ||
| 707 | }, brik => $brik, attribute => $attribute); | ||
| 708 | |||
| 709 | return $r; | ||
| 710 | } | ||
| 711 | |||
| 712 | sub run { | ||
| 713 | my $self = shift; | ||
| 714 | my ($brik, $command, @args) = @_; | ||
| 715 | |||
| 716 | if (! defined($brik) || ! defined($command)) { | ||
| 717 | return $self->log->error($self->brik_help_run('run')); | ||
| 718 | } | ||
| 719 | |||
| 720 | if ($self->log->level > 2) { | ||
| 721 | my ($module, $file, $line) = caller(); | ||
| 722 | $self->log->debug("run: called by module [$module] from [$file] line[$line]"); | ||
| 723 | } | ||
| 724 | |||
| 725 | my $r = $self->call(sub { | ||
| 726 | my %args = @_; | ||
| 727 | |||
| 728 | my $__ctx_brik = $args{brik}; | ||
| 729 | my $__ctx_command = $args{command}; | ||
| 730 | my @__ctx_args = @{$args{args}}; | ||
| 731 | |||
| 732 | my $ERR = 0; | ||
| 733 | |||
| 734 | if (! $CON->is_used($__ctx_brik)) { | ||
| 735 | $ERR = 1; | ||
| 736 | my $MSG = "run: Brik [$__ctx_brik] not used"; | ||
| 737 | die("$MSG\n"); | ||
| 738 | } | ||
| 739 | |||
| 740 | if (! $CON->used->{$__ctx_brik}->brik_has_command($__ctx_command)) { | ||
| 741 | $ERR = 1; | ||
| 742 | my $MSG = "run: Brik [$__ctx_brik] has no Command [$__ctx_command]"; | ||
| 743 | die("$MSG\n"); | ||
| 744 | } | ||
| 745 | |||
| 746 | my $__ctx_run = $CON->{used}->{$__ctx_brik}; | ||
| 747 | |||
| 748 | # Will brik_init() only if not already done | ||
| 749 | # And only for Brik's Commands, not base class Commands | ||
| 750 | if (! $__ctx_run->init_done && $__ctx_command !~ /^brik_/) { | ||
| 751 | if (! $__ctx_run->brik_init) { | ||
| 752 | $ERR = 1; | ||
| 753 | my $MSG = "run: Brik [$__ctx_brik] init failed"; | ||
| 754 | die("$MSG\n"); | ||
| 755 | } | ||
| 756 | } | ||
| 757 | |||
| 758 | for (@__ctx_args) { | ||
| 759 | # Support variable lookups like '$array' as an Argument | ||
| 760 | # Example: run <Brik> <Command> $Arg1 Arg2 | ||
| 761 | if (/^\$\w+/ || /^\@\$\w+/ || /^\@\w+/ || /^\%\$\w+/ || /^\%\w+/) { | ||
| 762 | eval { | ||
| 763 | $_ = $CON->_lp->do($_); | ||
| 764 | }; | ||
| 765 | if ($@) { | ||
| 766 | chomp($@); | ||
| 767 | $ERR = 1; | ||
| 768 | my $MSG = "run: Brik [$__ctx_brik] has invalid Argument [$_]"; | ||
| 769 | die("$MSG\n"); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | # Support passing ARRAYs or HASHs or Perl code as an Argument | ||
| 773 | # Example: run <Brik> <Command> "[ qw(a b c) ]" | ||
| 774 | elsif (/^\[.*\]$/ || /^\{.*\}$/) { | ||
| 775 | eval { | ||
| 776 | $_ = $CON->_lp->do($_); | ||
| 777 | }; | ||
| 778 | if ($@) { | ||
| 779 | chomp($@); | ||
| 780 | $ERR = 1; | ||
| 781 | my $MSG = "run: Brik [$__ctx_brik] has invalid Argument [$_]"; | ||
| 782 | die("$MSG\n"); | ||
| 783 | } | ||
| 784 | } | ||
| 785 | } | ||
| 786 | |||
| 787 | my $RUN; | ||
| 788 | my $__ctx_return = $__ctx_run->$__ctx_command(@__ctx_args); | ||
| 789 | if (! defined($__ctx_return)) { | ||
| 790 | $ERR = 1; | ||
| 791 | return; | ||
| 792 | } | ||
| 793 | |||
| 794 | $RUN = $__ctx_return; | ||
| 795 | |||
| 796 | my $REF = \$RUN; | ||
| 797 | |||
| 798 | return $RUN; | ||
| 799 | }, brik => $brik, command => $command, args => \@args); | ||
| 800 | |||
| 801 | return $r; | ||
| 802 | } | ||
| 803 | |||
| 804 | sub save_state { | ||
| 805 | my $self = shift; | ||
| 806 | my ($brik) = @_; | ||
| 807 | |||
| 808 | if (! defined($brik)) { | ||
| 809 | return $self->log->error($self->brik_help_run('save_state')); | ||
| 810 | } | ||
| 811 | |||
| 812 | my $r = $self->call(sub { | ||
| 813 | my %args = @_; | ||
| 814 | |||
| 815 | my $__ctx_brik = $args{brik}; | ||
| 816 | |||
| 817 | my $ERR = 0; | ||
| 818 | |||
| 819 | if (! $CON->is_used($__ctx_brik)) { | ||
| 820 | $ERR = 1; | ||
| 821 | my $MSG = "save_state: Brik [$__ctx_brik] not used"; | ||
| 822 | die("$MSG\n"); | ||
| 823 | } | ||
| 824 | |||
| 825 | my $__ctx_state; | ||
| 826 | my $__ctx_attributes = $CON->{used}->{$__ctx_brik}->brik_attributes || {}; | ||
| 827 | for my $__ctx_this (keys %$__ctx_attributes) { | ||
| 828 | $__ctx_state->{$__ctx_this} = $CON->{used}->{$__ctx_brik}->$__ctx_this; | ||
| 829 | } | ||
| 830 | $CON->{used}->{$__ctx_brik}->{"__ctx_state"} = $__ctx_state; | ||
| 831 | |||
| 832 | return 1; | ||
| 833 | }, brik => $brik); | ||
| 834 | |||
| 835 | return $r; | ||
| 836 | } | ||
| 837 | |||
| 838 | sub restore_state { | ||
| 839 | my $self = shift; | ||
| 840 | my ($brik) = @_; | ||
| 841 | |||
| 842 | if (! defined($brik)) { | ||
| 843 | return $self->log->error($self->brik_help_run('restore_state')); | ||
| 844 | } | ||
| 845 | |||
| 846 | my $r = $self->call(sub { | ||
| 847 | my %args = @_; | ||
| 848 | |||
| 849 | my $__ctx_brik = $args{brik}; | ||
| 850 | |||
| 851 | my $ERR = 0; | ||
| 852 | |||
| 853 | if (! $CON->is_used($__ctx_brik)) { | ||
| 854 | $ERR = 1; | ||
| 855 | my $MSG = "restore_state: Brik [$__ctx_brik] not used"; | ||
| 856 | die("$MSG\n"); | ||
| 857 | } | ||
| 858 | |||
| 859 | my $__ctx_state = $CON->{used}->{$__ctx_brik}->{"__ctx_state"}; | ||
| 860 | if (defined($__ctx_state)) { | ||
| 861 | for my $__ctx_this (keys %$__ctx_state) { | ||
| 862 | $CON->{used}->{$__ctx_brik}->$__ctx_this($__ctx_state->{$__ctx_this}); | ||
| 863 | } | ||
| 864 | } | ||
| 865 | |||
| 866 | return 1; | ||
| 867 | }, brik => $brik); | ||
| 868 | |||
| 869 | return $r; | ||
| 870 | } | ||
| 871 | |||
| 872 | sub brik_fini { | ||
| 873 | my $self = shift; | ||
| 874 | |||
| 875 | my $used = $self->used; | ||
| 876 | for my $brik (keys %$used) { | ||
| 877 | next if $brik eq 'core::context'; # Avoid recursive loop | ||
| 878 | $used->{$brik}->brik_fini; | ||
| 879 | } | ||
| 880 | |||
| 881 | return 1; | ||
| 882 | } | ||
| 883 | |||
| 884 | 1; | ||
| 885 | |||
| 886 | __END__ | ||