lib/Rex/Template/TT.pm

filter: Statementstmt 81.3% 48 / 59 Branchbran 61.1% 11 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 76.0% 76 / 100 CC 13 SCAR 14.4
CC
13
Cov
76%
validate_vars
8.1
template_toolkit
5.7
__ANON__
3.7
CRAP 4.2
↑ Summary
1 #
2 # Nicolas Leclercq <nicolas.private@gmail.com>
3 #
4 # vim: set ts=2 sw=2 tw=0:
5 # vim: set expandtab:
6  
7 package Rex::Template::TT;
8 2 $Rex::Template::TT::VERSION = '1.0.0';
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Rex -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 require Exporter;
18 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use vars qw (@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 @EXPORT = qw(template_toolkit);
22  
23  
24 8 ▶sub validate_vars {
Subroutine validate_vars: called CC=5 SCAR=20.9
Pod undocumented
25 8 my ( $vars ) = @_;
26  
27 8 foreach my $key (keys %$vars) {
28 144 ▶ if ($key =~ /^[\._]/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /^[\._]/) { } 0 144 144
elsif ($key =~ /([^a-zA-Z0-9_])/) { } 0 144 144
elsif ($key =~ /^(GET|CALL|SET|DEFAULT|INSERT|INCLUDE|PROCESS|WRAPPER|IF|UNLESS|ELSE|ELSIF|FOR|FOREACH|WHILE|SWITCH|CASE|USE|PLUGIN|FILTER|MACRO|PERL|RAWPERL|BLOCK|META|TRY|THROW|CATCH|FINAL|NEXT|LAST|BREAK|RETURN|STOP|CLEAR|TO|STEP|AND|OR|NOT|MOD|DIV|END)$/) { } 0 144 144
29 0 Rex::Logger::info( "variable name '$key' considered private by Template Toolkit",
30 "warn" );
31 } elsif ($key =~ /([^a-zA-Z0-9_])/) {
32 0 Rex::Logger::info( "variable name '$key' contains '$1' unsupported by Template Toolkit",
33 "warn" );
34 } elsif ($key =~ /^(GET|CALL|SET|DEFAULT|INSERT|INCLUDE|PROCESS|WRAPPER|IF|UNLESS|ELSE|ELSIF|FOR|FOREACH|WHILE|SWITCH|CASE|USE|PLUGIN|FILTER|MACRO|PERL|RAWPERL|BLOCK|META|TRY|THROW|CATCH|FINAL|NEXT|LAST|BREAK|RETURN|STOP|CLEAR|TO|STEP|AND|OR|NOT|MOD|DIV|END)$/) {
35 0 Rex::Logger::info( "variable name '$key' clashes with reserved Template Toolkit word",
36 "warn" );
37 }
38 }
39 }
40  
41 4 ▶sub template_toolkit {
Subroutine template_toolkit: called CC=5 SCAR=17.4
Pod undocumented
42 4 my ( $template_path, $vars ) = @_;
43  
44 4 validate_vars( $vars );
45  
46 4 ▶ if ( not ( ref $template_path and ref $template_path eq 'SCALAR') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $template_path and ref $template_path eq "SCALAR") 2 2 4
Condition66%2/3
ref $template_path and ref $template_path eq "SCALAR"
!ll&&!rl&&r
202
MC/DC50%1/2
ref $template_path and ref $template_path eq "SCALAR"
ref $template_path ref $template_path eq "SCALAR"
Truth table66%2/3
A and B
A= ref $template_pathB= ref $template_path eq "SCALAR"
ABresult
0X0
100
111
47 # resolv template path
48 2 $template_path = Rex::Helper::Path::resolv_path($template_path);
49 2 $template_path = Rex::Helper::Path::get_file_path( $template_path, caller() );
50 2 Rex::Logger::debug("Processing template file : $template_path");
51 }
52  
53 # process template
54 4 my $output = '';
55 my $template = Template->new( { ABSOLUTE => 1 } )
56 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $template = "Template"->new({"ABSOLUTE", 1}) or do { Rex::Logger::info($Template::ERROR, "error"); die $Template::ERROR } 4 0 4
57 0 Rex::Logger::info( $Template::ERROR, 'error' );
58 0 die $Template::ERROR;
59 };
60 $template->process( $template_path, $vars, \$output )
61 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$template->process($template_path, $vars, \$output) or do { Rex::Logger::info($template->error, "error"); die $template->error } 4 0 4
62 0 Rex::Logger::info( $template->error(), 'error' );
63 0 die $template->error();
64 };
65  
66 4 return $output;
67 }
68  
69 2 ▶sub import {
Subroutine import: called CC=3 SCAR=11.8
70  
71 2 my ( $class, $tag ) = @_;
72  
73 2 ▶ if ( $tag && $tag eq ":register" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag and $tag eq ":register") 1 1 2
Condition66%2/3
$tag and $tag eq ":register"
!ll&&!rl&&r
101
MC/DC50%1/2
$tag and $tag eq ":register"
$tag $tag eq ":register"
Truth table66%2/3
A and B
A= $tagB= $tag eq ":register"
ABresult
0X0
100
111
74  
75 # register Template::Toolkit for default template processing
76 4 ▶ set template_function => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
77 4 my ( $content, $vars ) = @_;
78  
79 4 validate_vars( $vars );
80  
81 my $template = Template->new()
82 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $template = "Template"->new or do { Rex::Logger::info($Template::ERROR, "error"); die $Template::ERROR } 4 0 4
83 0 Rex::Logger::info( $Template::ERROR, 'error' );
84 0 die $Template::ERROR;
85 };
86 4 my $output;
87 $template->process( \$content, $vars, \$output )
88 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$content, $vars, \$output) or do { Rex::Logger::info($template->error, "error"); die $template->error } 4 0 4
89 0 Rex::Logger::info( $template->error(), 'error' );
90 0 die $template->error();
91 };
92  
93 4 return $output;
94 1 };
95 }
96  
97 2 __PACKAGE__->export_to_level(1);
98 }
99  
100 2 1;
101  
102 =pod
103  
104 =head1 NAME
105  
106 Rex::Template::TT - A module to process templates with template toolkit.
107  
108 =head1 VERSION
109  
110 version 1.0.0
111  
112 =head1 SYNOPSIS
113  
114 use Rex::Template::TT;
115  
116 task "blah", sub {
117 file "/tmp/blah",
118 content => template("path/to/blah.template",
119 { persons => ['bob', 'alice'] }),
120 owner => "root",
121 group => "root",
122 mode => 644
123 };
124  
125 # to use as a default template engine
126 # this will make the template() function use TemplateTookit to render
127 # all the templates. This will also register all the known template variables
128 # like hostname, eth0_ip and so on.
129 use Rex::Template::TT ':register';
130  
131  
132 =head1 DESCRIPTION
133  
134 A Rex extension module to process templates with template toolkit.
135  
136 =head1 AUTHORS
137  
138 This module in its current publication is written by Nicolas Leclerq, based
139 on prior work by Jan Gehring. The original version by Nicolas was published
140 through the now-defunct I<Rex Recipes>. Since Nicolas's version ended up
141 being more feature-rich than Jan's, Jan gave permission to overwrite his
142 version with Nicolas's.
143  
144 =over
145  
146 =item * Nicolas Leclercq <nicolas.private@gmail.com>
147  
148 =item * Jan Gehring <krimdomu@googlemail.com>
149  
150 =back
151  
152 =head1 CONTRIBUTORS
153  
154 =over
155  
156 =item * Erik Huelsmann <ehuels@gmail.com>
157  
158 =back
159  
160 =head1 SEE ALSO
161  
162 =over
163  
164 =item * L<Template>
165  
166 =item * L<Rex>
167  
168 =item * L<http://www.template-toolkit.org/>
169  
170 =back
171  
172 =head1 LICENSE
173  
174 The Apache License, Version 2.0, January 2004
175  
176  
177 =cut
↑ Summary