1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
|
package Demeter::Dispose;
=for Copyright
.
Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
All rights reserved.
.
This file is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See The Perl
Artistic License.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
use autodie qw(open close);
use Moose::Role;
use Demeter::Constants qw($ENDOFLINE);
use subs qw(BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE ON_RED RESET);
my $ANSIColor_exists = (eval "require Term::ANSIColor");
if ($ANSIColor_exists) {
import Term::ANSIColor qw(:constants);
} else {
## this eval works when Term::ANSIColor is not available AND when running tests
eval '
sub BOLD {q{}};
sub BLACK {q{}};
sub RED {q{}};
sub GREEN {q{}};
sub YELLOW {q{}};
sub BLUE {q{}};
sub MAGENTA {q{}};
sub CYAN {q{}};
sub WHITE {q{}};
sub ON_RED {q{}};
sub RESET {q{}};';
};
=for LiteratureReference (_ansify)
“Has anyone really been far even as decided to use even go want to
do look more like?”
-- The internets
=cut
sub _ansify {
my ($self, $thisline, $kind) = @_;
my ($start, $end) = (q{}, q{});
my %color_of = (
black => BLACK,
red => RED,
green => GREEN,
yellow => YELLOW,
blue => BLUE,
magenta => MAGENTA,
cyan => CYAN,
white => WHITE,
);
COLOR:{
($kind eq 'comment') and do {
if ($thisline =~ m{\A\#\#\|}) {
($start, $end) = (BOLD.$color_of{$self->co->default("screen", "comment")}, RESET);
} elsif ($thisline =~ m{\A\#\#\#__}) {
($start, $end) = (BOLD.$color_of{$self->co->default("screen", "hashes" )}, RESET);
} elsif ($thisline =~ m{\A\#}) {
($start, $end) = (BOLD.$color_of{$self->co->default("screen", "other" )}, RESET);
} else {
#($start, $end) = (BOLD.$color_of{$self->co->default("screen", "other" )}, RESET);
($start, $end) = (q{}, q{});
};
last COLOR;
};
($kind eq 'feedback') and do {
if ($thisline =~ m{\A\s*\*}) {
print STDOUT WHITE, ON_RED, $thisline, RESET;
} else {
print STDOUT $color_of{$self->co->default("screen", "feedback")}, $thisline, RESET;
};
};
($kind eq 'peakfit') and do {
($start, $end) = (BOLD.GREEN, RESET);
};
($kind eq 'fefferr') and do {
print STDOUT WHITE, ON_RED, $thisline, RESET;
};
($kind eq 'feffout') and do {
print $thisline
};
};
return ($start, $end);
};
sub dispense {
my ($self, $set, $template, $args) = @_;
$self->dispose($self->template($set, $template, $args));
};
sub chart {
my ($self, $set, $template, $args) = @_;
$self->dispose($self->template($set, $template, $args), 'plotting');
};
##-----------------------------------------------------------------
## dispose commands to ifeffit, larch, and elsewhere
sub dispose {
my ($self, $command, $plotting) = @_;
return if not $command;
Demeter->set_mode( echo=>q{} );
my $echo_buffer = q{};
$command =~ s{\+ *-}{-}g; # suppress "+-" in command strings math expressions
$command =~ s{- *-}{+}g; # suppress "--" in command strings math expressions
return 0 if ($command =~ m{\A\s*\z});
($command .= "\n") if ($command !~ m{\n$});
## -------- spit everything to the screen, use ANSI colors if available and ui=screen
if (Demeter->get_mode("screen")) {
local $| = 1;
foreach my $thisline (split(/\n/, $command)) {
my ($start, $end) = (Demeter->mo->ui eq 'screen') ? $self->_ansify($thisline, 'comment') : (q{}, q{});
print STDOUT $start, $thisline, $end, $/;
};
};
## -------- spit plot commands to the screen, use ANSI colors if available and ui=screen
if ((Demeter->get_mode("plotscreen")) and $plotting) {
local $| = 1;
foreach my $thisline (split(/\n/, $command)) {
my ($start, $end) = (Demeter->mo->ui eq 'screen') ? $self->_ansify($thisline, 'comment') : (q{}, q{});
print STDOUT $start, $thisline, $end, $/;
};
};
## -------- dump everything to a file
if (Demeter->get_mode("file")) {
local $| = 1;
open my $FH, ">".Demeter->get_mode("file");
print $FH $command;
close $FH;
};
## -------- dump plot commands to a file
if ((Demeter->get_mode("plotfile")) and $plotting) {
#if ($self->mo->template_plot eq 'gnuplot') {
# my $crstring = $self->po->copyright_text; ## insert the copyright statement in a plot made with gnuplot
# $command =~ s{(unset label)}{$1\n$crstring}g;
#};
local $| = 1;
open my $FH, ">".Demeter->get_mode("plotfile");
print $FH $command;
close $FH;
};
## -------- use a disposal callback
if (Demeter->get_mode("callback")) {
my $coderef = Demeter->get_mode("callback");
if (not $plotting) {
&$coderef($command);
} elsif ($plotting and not Demeter->get_mode("plotcallback")) {
&$coderef($command);
} else {
1;
};
};
if ($plotting and Demeter->get_mode("plotcallback")) {
my $coderef = Demeter->get_mode("plotcallback");
&$coderef($command);
};
## -------- concatinate to a scalar buffer
if ((Demeter->get_mode("buffer")) and (ref(Demeter->get_mode("buffer")) eq 'SCALAR')) {
if (not $plotting) {
${ Demeter->get_mode("buffer") } .= $command;
} elsif ($plotting and not $self->get_mode("plotbuffer")) {
${ Demeter->get_mode("buffer") } .= $command;
} else {
1;
};
};
if ($plotting and Demeter->get_mode("plotbuffer") and (ref(Demeter->get_mode("plotbuffer")) eq 'SCALAR')) {
${ Demeter->get_mode("plotbuffer") } .= $command;
};
## -------- unknown buffer type
if ( (Demeter->get_mode("buffer"))
and (ref(Demeter->get_mode("buffer")) ne 'SCALAR')
and (ref(Demeter->get_mode("buffer")) ne 'ARRAY') ) {
carp("Demeter::Dispose: string mode value is not a scalar or array reference\n\n");
};
if ($plotting and (Demeter->mo->template_plot eq 'gnuplot')) {
#my $crstring = $self->po->copyright_text; ## insert the copyright statement in a plot made with gnuplot
#$command =~ s{(unset label)}{$1\n$crstring}g;
Demeter->mo->external_plot_object->gnuplot_cmd($command);
# $self->mo->external_plot_object->gnuplot_pause(-1);
my $gather = Demeter->po->lastplot;
$gather .= $command;
Demeter -> po -> lastplot($gather);
return 0; ## need to short-circuit this so the gnuplot commands do not go to Ifeffit/Larch
};
## -------- don't bother reprocessing unless an output channel that
## requires looping over every line
return 0 unless (
(Demeter->get_mode("buffer") and (ref(Demeter->get_mode("buffer")) eq 'ARRAY'))
or Demeter->get_mode("ifeffit")
or Demeter->get_mode("repscreen")
or Demeter->get_mode("repfile")
);
my ($reprocessed, $eol) = (q{}, $ENDOFLINE);
foreach my $thisline (split(/\n/, $command)) {
if ((Demeter->get_mode("buffer")) and (ref(Demeter->get_mode("buffer")) eq 'ARRAY')) {
push @{ Demeter->get_mode("buffer") }, $thisline;
};
## this next bit of insanity is an ifeffit optimization. it is
## considerably faster to have perl process multi-line commands
## into long (up to 2048 characters) individual commands than to
## use ifeffit and the swig wrapper. the point here is to
## recognize parens-bound commands and concatinate them onto a
## single line
next if ($thisline =~ m{^\s*\#});
next if ($thisline =~ m{^\s*$});
$thisline =~ s{^\s+}{};
$thisline =~ s{\s+$}{};
$thisline =~ s{\s+=}{ =};
my $re = $Demeter::StrTypes::command_regexp;
$eol = ($thisline =~ m{^$re\s*\(}) ? " " : $eol;
$eol = $ENDOFLINE if ($thisline =~ m{\)$});
$reprocessed .= $thisline . $eol;
};
local $SIG{ALRM} = sub { 1; } if not $SIG{ALRM}; # very cryptic!
## -------- send reprocessed command text to ifeffit
if (Demeter->get_mode("backend")) {
if (Demeter->is_larch) {
Larch::dispose($command);
} else {
if (Demeter->is_windows) {
Ifeffit::ifeffit($_) foreach (split(/$ENDOFLINE/, $reprocessed)); # WTF!
} else {
Ifeffit::ifeffit($reprocessed);
};
};
Demeter -> po -> copyright_text if ($plotting and (Demeter->mo->template_plot eq 'pgplot')); ## insert the copyright statement in a plot made with pgplot
## this mess parses Ifeffit's feedback and sends it either to the feedback code ref or to the screen
my $coderef = Demeter->get_mode("feedback");
if ($coderef or Demeter->get_mode("screen") or Demeter->get_mode("plotscreen")) {
if (Demeter->is_larch) {
my $response = Larch::get_messages();
## send to feedback code ref
if ($coderef) {
($response) and &$coderef($response."\n");
## send to the screen with ANSI colorization
} elsif (Demeter->get_mode("screen") or (Demeter->get_mode("plotscreen") and $plotting)) {
$self->_ansify($response.$/, "feedback")
};
} else {
my ($lines, $response) = (Ifeffit::get_scalar('&echo_lines')||0, "");
if ($lines) { # is there feedback?
foreach my $i (1 .. $lines) {
my $response = Ifeffit::get_echo();
## send to feedback code ref
if ($coderef) {
($response) and &$coderef($response."\n");
## send to the screen with ANSI colorization
} elsif (Demeter->get_mode("screen") or (Demeter->get_mode("plotscreen") and $plotting)) {
$self->_ansify($response.$/, "feedback")
};
};
};
};
};
};
## -------- send reprocessed command text to the screen
print STDOUT $reprocessed if Demeter->get_mode("repscreen");
## -------- send reprocessed command text to a file
if (Demeter->get_mode("repfile")) {
open my $FH, ">".Demeter->get_mode("file");
print $FH $reprocessed;
close $FH;
};
Demeter->set_mode(echo=>$echo_buffer);
return 0;
};
sub nl {
my ($self) = @_;
$self->dispose("\n");
return $self;
};
sub Reset {
my ($self) = @_;
$self->dispose("reset");
return $self;
};
sub cursor {
my ($self) = @_;
$self->dispose("cursor(show, cross-hair)");
return(Ifeffit::get_scalar("cursor_x"), Ifeffit::get_scalar("cursor_y"));
};
1;
=head1 NAME
Demeter::Dispose - Process Ifeffit, Larch, and plotting command strings
=head1 VERSION
This documentation refers to Demeter version 0.9.26.
=head1 SYNOPSIS
my $data_object = Demeter::Data -> new();
$data_object -> set_mode(backend=>1, buffer=>\@buffer, screen=>1);
$data_object -> dispose($ifeffit_command);
=head1 DESCRIPTION
This module contains contains the dispose method, which is used to
dispatch Ifeffit, Larch, and Gunplot command strings. This is part of
the base of all other objects in the Demeter system, thus any object
can dispose text.
The command strings which are handled by the C<dispose> method are
typically generated using the command templating system, which is
described in L<Demeter/TEMPLATES>.
=head2 Reprocessed commands
Command strings are typically generated by Demeter in a manner which
is designed to be human readable. Unfortunately, human-readable and
Ifeffit-efficient tend to be at odds. In the interest of speed and
efficiency, Demeter processes Ifeffit commands into a form that is
harder for a human to read, but faster for Ifeffit to process.
The main change made to Ifeffit command strings is to concatinate
multi-line commands into a single line and to squeeze white space into
a single space. This significantly reduces the number of calls to the
C<iff_exec> function in Ifeffit, which is one of the most
time-consuming parts of the Demeter/Ifeffit stack. Given that Ifeffit
allows command strings to be as much as 2048 characters long, it is
rare that an command generated by Demeter cannot be handled in this
way.
Also, the preprocessed text does not contain any lines that are only
whitespace or are entirely commented out.
=head1 METHODS
=head2 C<dispose>
This method dispatches command strings to various places. Many
methods in the Demeter system are command generators. The intent is
that to accumulate text through successive method calls and then
dispose of the text using this method. The method takes two
arguments, a scalar containing all the text that you have accumulates,
and an optional argument that, when true, indicates that the commands
are specifically plotting commands.
Demeter is very careful to segregate plotting commands from data
processing commands and to dispose of them separately. This allows
transparent use of different plotting backends. The C<backend>
disposal channel is used to indicate disposal of commands either to
ifeffit or to another plotting backend. That is, if you want to
dispose of plotting commands to a gnuplot process, you must have the
C<backend> disposal channel enabled. C<ifeffit> and C<larch> are
aliases for C<backend>.
Use the C<set_mode> class method to establish the disposal channels.
Demeter->set_mode(backend=>1, screen=>1, file=>0, buffer=>0);
$dataobject -> dispose($commands);
When disposing a plotting command, use the plotting flag:
$dataobject -> dispose($commands, "plotting");
There are several disposal channels:
=over 6
=item backend (ifeffit or larch are aliases)
This channel sends reprocessed command strings to ifeffit for
processing. This is a boolean. By default, this channel is on and
all the rest are off.
=item screen
This channel sends command strings to standard output. This is a
boolean. If the UI is set to screen (see L<Demeter::Mode>) and the
L<Term::ANSIColor> package is installed, then comments in the screen
output will be colored red, pink, or yellow depending on the comment
character.
The default colors are:
red data processing comments
pink plotting comments
yellow fitting comments
light blue feedback from Ifeffit/Larch
white on red error messages from Ifeffit/Larch
These colors are configurable in the screen group.
=item file
This channel writes command strings to a file. If the value of file
evaluates false, then this channel is not used. If it is true, the
value is taken to be the name of the output file. At this time, IO
control is extremely simple. If you want to append command strings to
an existing file, simply append C<E<gt>> to the beginning of the file
name:
# clobber foo and start a new file by that name
$dataobject -> set_mode(file=>"foo");
$dataobject -> dispose($commands);
#
# append to foo
$dataobject -> set_mode(file=>">foo");
$dataobject -> dispose($commands);
=item plotscreen
This behaves exactly like the C<screen> parameter, but applies only to
commands disposed using the plotting flag. If the UI is set to screen
(see L<Demeter::Mode>) and the L<Term::ANSIColor> package is
installed, then comments in the screen output will be colored red,
pink, or yellow depending on the comment character.
=item plotfile
This behaves exactly like the C<file> parameter, but applies only to
commands disposed using the plotting flag. This allows you to
accumulate plotting commands into plotting scripts, which is handy for
the gnuplot backend.
=item buffer
This channel pushes each command line onto an in-memory buffer. The buffer
can be either a string or an array. The buffer attribute is a reference to
the scalar or array.
If a scalar reference is used, the scalar is treated as a string and each
command line is concatinated to the end of the string.
$buffer = q{};
$dataobject -> set_mode(buffer=>\$buffer);
$dataobject -> dispose($commands);
print $buffer;
If an array reference is used, each command line is pushed onto the end of the
array.
@buffer = ();
$dataobject -> set_mode(buffer=>\@buffer);
$dataobject -> dispose($commands);
map {print $_} @buffer;
An obvious improvement to this would be to allow the buffer attribute
to be a reference to an arbitrary object which can be used in some
domain-specific, user-defined manner.
=item plotbuffer
This is an optional output channel for the plotting commands. If
unset, plotting commands go to the same channel as specified by
C<buffer>. This channel works identically to C<buffer>, albeit
redirected to a different place.
=item callback
This channel sends disposed text to a user supplied code reference.
For instance, in Artemis, this is a subroutine that prints the
disposed lines to the command buffer. This code ref takes a single
argument, which is the text to be disposed.
=item plotcallback
This optional channel redirects plotting commands to a differnt code
reference from C<callback>. If unset, plotting commands are disposed
to C<callback>'s code ref.
=item feedback
This channel sends feedback from Ifeffit or Larch to a user supplied
code reference. Note that lines indicating a problem in Ifeffit's or
Larch's output start with a star (*). Information lines start with
text.
=item repscreen
This channel sends reprocessed command strings to standard output.
The value of screen in the hash is interpreted as a boolean. The main
use of this channel is to debug the text actually sent to Ifeffit. No
colorizing is done because comments are stripped from the reprocessed
commands.
=item repfile
This channel send reprocessed command strings to a file. This channel
is handled in the same manner as the normal file channel. The main
use of this channel is to debug the text actually sent to Ifeffit.
=back
Note that the C<dispose> method is also used to place a copyright
statement on every plot. This behavior can be suppressed by setting
the C<plot->showcopyright> configuration parameter to a false
value.
=head2 other methods
=over 4
=item C<dispense>
This wraps calls to C<template> and C<dispose>. This:
$self->dispense('process', 'deriv');
is the same as
$self->dispose($self->template('process', 'deriv'));
which was such a common idiom in Demeter that it merited this method.
=item C<chart>
This wraps calls to C<template> and to C<dispose> with the C<plotting>
argument. This:
$self->chart('process', 'deriv');
is the same as
$self->dispose($self->template('process', 'deriv'), 'plotting');
which was such a common idiom in Demeter that it merited this method.
=item C<Reset>
This method sends the C<reset> command to ifeffit. The method name is
capitalized to avoid confusion with the perl built-in function.
=item C<cursor>
This method sends the C<cursor> command to ifeffit with the C<show> and
C<cross-hair> arguments. It returns the x and y coordinates of the
cursor click.
my ($xclick, $yclick) = $object->cursor;
Note that this is a blocking operation. Your program will pause until
a click event happens in the plot window.
=back
For more information about effecting command generation and disposal,
see L<Demeter::Mode>.
=head1 CONFIGURATION
See L<Demeter::Config> for a description of the configuration system.
=head1 DEPENDENCIES
Demeter's dependencies are in the F<Build.PL> file.
=head1 BUGS AND LIMITATIONS
=over 4
=item *
The file and repfile disposal channels is handled in the most primitive
manner that remains functional. It may be useful to consider using
IO::File or something similar. Further, it is only possible to
specify a single file for this channel. Something clever with "tee"
can be done to dispatch to multiple files.
=item *
The buffer disposal channel currently only works with normal
scalars and normal arrays. It would be reasonable for a user to want
the buffer to be, say, a tied array or some particular object. That
will be dealt with should it ever come up.
=item *
The screen, plotscreen, and repscreen disposal channels currently
write to STDOUT. The user can direct STDOUT elsewhere. It may be
useful to have the option of specifying a filehandle for this channel.
=back
Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)
Patches are welcome.
=head1 AUTHOR
Bruce Ravel, L<http://bruceravel.github.io/home>
L<http://bruceravel.github.io/demeter/>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
|