| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | my $self = bless({},$class); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | if (exists $args{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | croak "import argument debug must be 0 or 1" | ||||||||||||||||||||||||||||
| 30 | 5 | ▶ | if ($args{debug} !~ /^(0|1)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | if ($status < 0); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # special case: empty doc. nothing to do. | ||||||||||||||||||||||||||||
| 65 | 12 | ▶ | return 0 if (length($_) == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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())
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(\$_)
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $_[1] ne "" and $_[1]->parent == $_[0]
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]
| |||||||||||||||||||||||||||||
| 80 | 6 | }); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | if (ref $subs eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $subname or $subname eq ""
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 ""
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $prev eq "PPI::Token::Comment" and $prev->content =~ /\#DECORATOR:/
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:/
| |||||||||||||||||||||||||||||
| 110 | 12 | my $c = $prev->content; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | if ($c =~ /^\#DECORATOR:\@(\w+)\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $_[1] ne "" and $_[1]->parent == $_[0]
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]
| |||||||||||||||||||||||||||||
| 157 | 5 | }) }; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 5 | ▶ | croak "found no or more than 1 sub block for sub ".$self->subname | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | print "Python::Decorator filtered the source into:\n-------------------------------\n".$_."-------------------------------\n" | ||||||||||||||||||||||||||||
| 178 | 5 | ▶ | if ($self->{debug}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 5 | return 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 7 | 1; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||