2014-03-16 23:39:07 +00:00
|
|
|
package Slic3r::GCode::PlaceholderParser;
|
2014-06-12 07:23:10 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2014-03-16 23:39:07 +00:00
|
|
|
|
2014-05-06 08:07:18 +00:00
|
|
|
sub new {
|
|
|
|
# TODO: move this code to C++ constructor, remove this method
|
|
|
|
my ($class) = @_;
|
|
|
|
my $self = $class->_new;
|
|
|
|
$self->apply_env_variables;
|
|
|
|
$self->update_timestamp;
|
|
|
|
return $self;
|
|
|
|
}
|
2014-03-16 23:39:07 +00:00
|
|
|
|
2014-05-06 08:07:18 +00:00
|
|
|
sub apply_env_variables {
|
2014-03-16 23:39:07 +00:00
|
|
|
my ($self) = @_;
|
2014-05-06 08:07:18 +00:00
|
|
|
$self->_single_set($_, $ENV{$_}) for grep /^SLIC3R_/, keys %ENV;
|
2014-04-25 17:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub update_timestamp {
|
|
|
|
my ($self) = @_;
|
2014-05-06 08:07:18 +00:00
|
|
|
|
2014-03-16 23:39:07 +00:00
|
|
|
my @lt = localtime; $lt[5] += 1900; $lt[4] += 1;
|
2014-05-06 08:07:18 +00:00
|
|
|
$self->_single_set('timestamp', sprintf '%04d%02d%02d-%02d%02d%02d', @lt[5,4,3,2,1,0]);
|
2014-06-10 14:01:57 +00:00
|
|
|
$self->_single_set('year', "$lt[5]");
|
|
|
|
$self->_single_set('month', "$lt[4]");
|
|
|
|
$self->_single_set('day', "$lt[3]");
|
|
|
|
$self->_single_set('hour', "$lt[2]");
|
|
|
|
$self->_single_set('minute', "$lt[1]");
|
|
|
|
$self->_single_set('second', "$lt[0]");
|
2014-05-06 08:07:18 +00:00
|
|
|
$self->_single_set('version', $Slic3r::VERSION);
|
2014-03-16 23:39:07 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 08:07:18 +00:00
|
|
|
# TODO: or this could be an alias
|
2014-03-16 23:39:07 +00:00
|
|
|
sub set {
|
|
|
|
my ($self, $key, $val) = @_;
|
2014-05-06 08:07:18 +00:00
|
|
|
$self->_single_set($key, $val);
|
2014-03-16 23:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process {
|
|
|
|
my ($self, $string, $extra) = @_;
|
|
|
|
|
|
|
|
# extra variables have priority over the stored ones
|
|
|
|
if ($extra) {
|
|
|
|
my $regex = join '|', keys %$extra;
|
|
|
|
$string =~ s/\[($regex)\]/$extra->{$1}/eg;
|
|
|
|
}
|
|
|
|
{
|
2014-05-06 08:07:18 +00:00
|
|
|
my $regex = join '|', @{$self->_single_keys};
|
|
|
|
$string =~ s/\[($regex)\]/$self->_single_get("$1")/eg;
|
2014-03-16 23:39:07 +00:00
|
|
|
}
|
|
|
|
{
|
2014-05-06 08:07:18 +00:00
|
|
|
my $regex = join '|', @{$self->_multiple_keys};
|
|
|
|
$string =~ s/\[($regex)\]/$self->_multiple_get("$1")/egx;
|
2014-04-29 15:06:31 +00:00
|
|
|
|
2014-04-29 15:12:00 +00:00
|
|
|
# unhandled indices are populated using the first value
|
2014-05-06 08:07:18 +00:00
|
|
|
$string =~ s/\[($regex)_\d+\]/$self->_multiple_get("$1")/egx;
|
2014-03-16 23:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|