lib/Python/Decorator.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 77.5% 31 / 40 Conditioncond 58.8% 10 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 85.3% 140 / 164 CC 28 SCAR 24.9
CC
28
Cov
88%
filter
21.0
import
5.3
__ANON__
3.0
CRAP 12.1
↑ Summary
1 #-------------------------------------------------------------------
2 #
3 # Python::Decorator - Python decorators for Perl5
4 #
5 # $Id: Decorator.pm,v 1.6 2008-11-05 20:56:42 erwan Exp $
6 #
7  
8 package Python::Decorator;
9  
10 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use PPI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use PPI::Token::Word;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 our $VERSION = '0.03';
20  
21 # import - just call filter_add from Filter::Util::Call
22 6 ▶sub import {
Subroutine import: called CC=5 SCAR=16.6
23 6 my ($class,%args) = @_;
24 6 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
25  
26 6 my $self = bless({},$class);
27  
28 6 ▶ if (exists $args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"debug"}) 5 1 6
29 croak "import argument debug must be 0 or 1"
30 5 ▶ if ($args{debug} !~ /^(0|1)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"debug"} =~ /^(0|1)$/ 0 5 5
31 5 $self->{debug} = $args{debug};
32 5 delete $args{debug};
33 }
34  
35 6 ▶ croak "unsupported import arguments: ".join(" ", keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args 0 6 6
36 if (scalar keys %args);
37  
38 6 filter_add($self);
39 }
40  
41 #
42 # filter - filter the source
43 #
44  
45 # NOTE: we use PPI to parse the filtered source code instead of using
46 # simple regexps and support a smaller but standard subset of possible
47 # syntaxes for sub declaration. For just playing around with
48 # decorators, regexps would have been enough, but I also wanted to
49 # experiment using PPI in a source filter. Hence the extra headache :)
50  
51 12 ▶sub filter {
Subroutine filter: called CC=20 SCAR=30.4
Pod documented
52 12 my ($self) = @_;
53 12 my $status;
54  
55 # read the whole source at once, accumulate it in $_
56 12 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 or do { do { $status = &filter_read() } } 12 160 172
57 172 $status = filter_read();
58 } until ($status == 0);
59  
60 # TODO: croak here, or let Filter::Util::Call croak for us?
61 12 ▶ croak "source filter error: $!"
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 12 12
62 if ($status < 0);
63  
64 # special case: empty doc. nothing to do.
65 12 ▶ return 0 if (length($_) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 6 6 12
66  
67 # comment out python decorators since they are not parsable perl
68 # and append a magic keyword (here '#DECORATOR:') in front of
69 # them. we later remove all those magic keywords. this is to
70 # avoid commenting out valid perl code by misstake...
71 6 ▶ while (s/^(\@\w+(\(.+\))?\s*)(\#.*)?$/\#DECORATOR:$1/gm) {}
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(\@\w+(\(.+\))?\s*)(\#.*)?$/#DECORATOR:$1/gm) 5 6 11
72  
73 # parse the whole source with PPI
74 6 ▶ my $doc = PPI::Document->new(\$_) ||
Condition50%1/2
'PPI::Document'->new(\$_) || croak('failed to parse source with PPI:' . PPI::Document::errstr())
l!l
60
MC/DC0%0/1
'PPI::Document'->new(\$_) || croak('failed to parse source with PPI:' . PPI::Document::errstr())
'PPI::Document'->new(\$_)
Truth table50%1/2
A
A= 'PPI::Document'->new(\$_)
Aresult
11
00
75 croak "failed to parse source with PPI:".PPI::Document::errstr;
76  
77 # do not look for subs recursively: skip any anonymous sub declared within a sub.
78 869 ▶ my $subs = $doc->find( sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
79 869 ▶ ref $_[1] ne '' && $_[1]->parent == $_[0] && $_[1]->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ne "" and $_[1]->parent == $_[0] and $_[1]->isa("PPI::Statement::Sub") 88 781 869
Condition66%2/3
ref $_[1] ne "" and $_[1]->parent == $_[0]
!ll&&!rl&&r
078188
MC/DC50%1/2
ref $_[1] ne "" and $_[1]->parent == $_[0]
ref $_[1] ne "" $_[1]->parent == $_[0]
Truth table66%2/3
A and B
A= ref $_[1] ne ""B= $_[1]->parent == $_[0]
ABresult
0X0
100
111
80 6 });
81  
82 6 ▶ if (ref $subs eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subs eq "") 1 5 6
83 # no subs declared in the source
84 1 return 1;
85 }
86  
87 # foreach sub declaration in the source file
88 5 foreach my $esub (@$subs) {
89  
90 # find out the 'sub' keyword and the subroutine's name
91 14 my @words = @{$esub->find('PPI::Token::Word')};
92  
93 14 ▶ confess "expected keyword 'sub'"
Branch: 1 branch
BranchTrueFalseTotal
if $words[0]->content ne "sub" 0 14 14
94 if ($words[0]->content ne 'sub');
95  
96 14 my $token_sub = $words[0];
97 14 my $token_name = $words[1];
98 14 my $subname = $token_name->content;
99  
100 14 ▶ confess "failed to parse sub name"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $subname or $subname eq "" 0 14 14
Condition33%1/3
not defined $subname or $subname eq ""
l!l&&r!l&&!r
0014
MC/DC0%0/2
not defined $subname or $subname eq ""
not defined $subname $subname eq ""
Truth table33%1/3
not(A) or B
A= not defined $subnameB= $subname eq ""
ABresult
1X1
011
000
101 if (!defined $subname || $subname eq "");
102  
103 # look at lines just above the sub declaration: they might be
104 # decorators
105 14 my $prev = $esub->previous_token;
106 14 my $before_sub = "";
107 14 my $after_sub = "";
108  
109 14 ▶ while (ref $prev eq 'PPI::Token::Comment' && $prev->content =~ /\#DECORATOR:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prev eq "PPI::Token::Comment" and $prev->content =~ /\#DECORATOR:/) 12 14 26
Condition100%3/3
ref $prev eq "PPI::Token::Comment" and $prev->content =~ /\#DECORATOR:/
!ll&&!rl&&r
13112
MC/DC100%2/2
ref $prev eq "PPI::Token::Comment" and $prev->content =~ /\#DECORATOR:/
ref $prev eq "PPI::Token::Comment" $prev->content =~ /\#DECORATOR:/
Truth table100%3/3
A and B
A= ref $prev eq "PPI::Token::Comment"B= $prev->content =~ /\#DECORATOR:/
ABresult
0X0
100
111
110 12 my $c = $prev->content;
111  
112 12 ▶ if ($c =~ /^\#DECORATOR:\@(\w+)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /^\#DECORATOR:\@(\w+)\s*$/) { } 7 5 12
elsif ($c =~ /^\#DECORATOR:\@(\w+\(.+\))\s*$/) { } 5 0 5
113 # previous line is a decorator that takes no arguments
114 7 $before_sub = $1."(".$before_sub;
115 7 $after_sub .= ")";
116 } elsif ($c =~ /^\#DECORATOR:\@(\w+\(.+\))\s*$/) {
117 # previous line is a decorator that takes arguments
118 5 $before_sub = $1."->(".$before_sub;
119 5 $after_sub .= ")";
120 } else {
121 # previous line looks like a decorator but is not...
122 0 croak "invalid decorator syntax";
123 }
124  
125 # remove the commented decorator but keep the newline to
126 # avoid messing up line-numbers in the source
127 12 $prev->set_content("\n");
128  
129 # move up to previous line
130 12 $prev = $prev->previous_token;
131 }
132  
133 # skip this sub if it has no decorators
134 14 ▶ next if ($after_sub eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $after_sub eq "" 9 5 14
135  
136 # now comes some source text manipulation by way of PPI.
137 # we replace 'sub foo [...]' with something like:
138 #
139 # '{ no strict "refs"; *{__PACKAGE__."::foo"} = bar(bob(babe(sub [...] )))); }'
140 #
141 # the 'no strict "refs"' is needed for the symbol table
142 # assignment '*{__PACKAGE__::foo} =' to work in a 'use strict'
143 # environment.
144 #
145 # all those edits must fit on one line to avoid messing up the
146 # linking between errors and line number.
147  
148 # remove the sub's name
149 5 $token_name->set_content("");
150  
151 # replace the keyword 'sub' with the string below:
152 5 $token_sub->set_content("{ no strict \"refs\"; *{__PACKAGE__.\"::".$subname."\"} = ".$before_sub." sub");
153  
154 # find the PPI block that contains the body of the subroutine
155 5 ▶ my @blocks = @{ $esub->find( sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
156 75 ▶ ref $_[1] ne '' && $_[1]->parent == $_[0] && $_[1]->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ne "" and $_[1]->parent == $_[0] and $_[1]->isa("PPI::Structure::Block") 25 50 75
Condition66%2/3
ref $_[1] ne "" and $_[1]->parent == $_[0]
!ll&&!rl&&r
05025
MC/DC50%1/2
ref $_[1] ne "" and $_[1]->parent == $_[0]
ref $_[1] ne "" $_[1]->parent == $_[0]
Truth table66%2/3
A and B
A= ref $_[1] ne ""B= $_[1]->parent == $_[0]
ABresult
0X0
100
111
157 5 }) };
158  
159 5 ▶ croak "found no or more than 1 sub block for sub ".$self->subname
Branch: 1 branch
BranchTrueFalseTotal
if scalar @blocks != 1 0 5 5
160 if (scalar @blocks != 1);
161  
162 5 my $subbody = $blocks[0];
163  
164 # replace the sub's last '}' with '} $after_sub; }'
165 5 my $brace = $subbody->finish;
166 5 ▶ confess "expected a '}' at the end of sub ".$subname
Branch: 1 branch
BranchTrueFalseTotal
if $brace->content ne "}" 0 5 5
167 if ($brace->content ne "}");
168 5 $brace->set_content("} $after_sub; }");
169 }
170  
171 # serialize back the modified source tree
172 5 $_ = $doc->serialize;
173  
174 # remove left over '#DECORATOR:'s
175 5 ▶ while (s/^(\#DECORATOR:\@)/\@/gm) {}
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(\#DECORATOR:\@)/\@/gm) 2 5 7
176  
177 print "Python::Decorator filtered the source into:\n-------------------------------\n".$_."-------------------------------\n"
178 5 ▶ if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 5 0 5
179  
180 5 return 1;
181 }
182  
183 7 1;
184  
185 __END__
↑ Summary