Archived
1
0

Trace: can export as string, can addId

This commit is contained in:
Mercier Pierre-Olivier 2013-11-13 00:09:54 +01:00
parent 5d2b1e80fb
commit 81fd3a04e2
2 changed files with 103 additions and 28 deletions

View File

@ -340,13 +340,15 @@ sub compute ($$$;$$$)
my $login = shift;
my $ref = $self->{ref};
if ($login && $ref) {
$ref =~ s/\$LOGIN/$login/;
}
# Handle $LOGIN in ref
$ref =~ s/\$LOGIN/$login/ if ($login && $ref);
# Handle globbing in ref
if (defined $ref)
{
eval {
eval
{
my $glob = Tinyglob::tinyglob($ref);
if ($glob ne $ref)
{

View File

@ -97,6 +97,38 @@ sub getInfos ($)
return $self->{infos};
}
sub addId
{
my $self = shift;
my $key = shift;
my $value = shift;
my $e = Trace::Eval->new($key);
$e->addId(undef, $value);
push @{ $self->{groups} }, $e;
}
sub delId
{
my $self = shift;
my $key = shift;
my $value = shift;
foreach my $group (@{ $self->{groups} })
{
if ($group->{id} eq $key)
{
if (!$value || $value == $group->getValue())
{
#$self->{groups} = \{ grep { ! } @{ $self->{groups} } };
}
last;
}
$group->delId($key, $value);
}
}
sub getIds
{
my $self = shift;
@ -156,30 +188,19 @@ sub getWhos
return $ret;
}
sub toString ($;$)
sub toString ($)
{
my $self = shift;
my $main_grp = shift // "bonus_malus";
my $doc = XML::LibXML::Document->new('1.0');
my $root = $doc->createElement("trace");
my $group = $doc->createElement("group");
$group->addChild( $doc->createAttribute("id", $main_grp) );
for my $k (keys %{ $self->{ids} }) {
my $e = $doc->createElement("eval");
my $v = $doc->createElement("value");
$e->addChild( $doc->createAttribute("id", $k) );
$v->appendText( $self->{ids}{$k} );
$e->appendChild( $v );
$group->appendChild( $e );
foreach my $group (@{ $self->{groups} })
{
$root->appendChild( $group->toString($doc) );
}
$root->appendChild( $group );
$doc->setDocumentElement( $root );
return $doc->toString();
@ -214,6 +235,19 @@ sub append ($@)
push @{ $self->{groups} }, @_;
}
sub delId
{
my $self = shift;
my $key = shift;
my $value = shift;
foreach my $item (@{ $self->{groups} })
{
if ($item->{id} eq )
$group->delId(@_);
}
}
sub getIds
{
my $self = shift;
@ -276,6 +310,21 @@ sub getWhos
return $ret;
}
sub toString($$)
{
my $self = shift;
my $doc = shift;
my $gr = $doc->createElement("group");
foreach my $item (@{ $self->{groups} })
{
$gr->appendChild( $item->toString() );
}
return $gr;
}
package Trace::Eval;
@ -316,15 +365,8 @@ sub parseEval
if ($node->nodeName eq "value")
{
my $key;
if ($node->hasAttribute("id")) {
$key = $node->getAttribute("id");
} else {
$key = "";
}
$self->{values}{ $key } = 0 if (!exists $self->{values}{ $key });
$self->{values}{ $key } += $val;
$self->addValue($node->getAttribute("id"),
$val);
}
elsif ($node->nodeName eq "name")
{
@ -369,6 +411,16 @@ sub getIds
return %ids;
}
sub addValue
{
my $self = shift;
my $key = shift // "";
my $val = shift;
$self->{values}{ $key } = 0 if (!exists $self->{values}{ $key });
$self->{values}{ $key } += $val;
}
sub getValue
{
my $self = shift;
@ -393,4 +445,25 @@ sub getWhos
return { $self->{id} => $self->{who} };
}
sub toString($$)
{
my $self = shift;
my $doc = shift;
my $e = $doc->createElement("eval");
$e->setAttribute("id", $self->{id});
$e->setAttribute("type", $self->{type});
for my $k (keys %{ $self->{values} })
{
my $v = $doc->createElement("value");
$v->setAttribute("id", $k) if ($k);
$v->appendTextNode( $self->{values}{$k} );
$e->appendChild( $v );
}
return $e;
}
1;