| 1 | #============================================================= -*-Perl-*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Template::Plugin::Directory | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Plugin for encapsulating information about a file system directory. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # AUTHORS | ||||||||||||||||||||||||||||
| 9 | # Michael Stevens <michael@etla.org>, with some mutilations from | ||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org>. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # COPYRIGHT | ||||||||||||||||||||||||||||
| 13 | # Copyright (C) 2000-2022 Michael Stevens, Andy Wardley. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 16 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | #============================================================================ | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Template::Plugin::Directory; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Template::Plugin::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use base 'Template::Plugin::File'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | our $VERSION = '3.106'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 33 | # new(\%config) | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | # Constructor method. | ||||||||||||||||||||||||||||
| 36 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 36 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 36 | ▶ | my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 36 | my ($class, $context, $path) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 36 | ▶ | return $class->throw('no directory specified') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and length $path
MC/DC50%1/2
defined $path and length $path
defined $path
length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 43 | unless defined $path and length $path; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 35 | my $self = $class->SUPER::new($context, $path, $config); | |||||||||||||||||||||||||||
| 46 | 34 | my ($dir, @files, $name, $item, $abs, $rel, $check); | |||||||||||||||||||||||||||
| 47 | 34 | $self->{ files } = [ ]; | |||||||||||||||||||||||||||
| 48 | 34 | $self->{ dirs } = [ ]; | |||||||||||||||||||||||||||
| 49 | 34 | $self->{ list } = [ ]; | |||||||||||||||||||||||||||
| 50 | 34 | $self->{ _dir } = { }; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # don't read directory if 'nostat' or 'noscan' set | ||||||||||||||||||||||||||||
| 53 | 34 | ▶ | return $self if $config->{ nostat } || $config->{ noscan }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config->{"nostat"} or $config->{"noscan"}
MC/DC100%2/2
$config->{"nostat"} or $config->{"noscan"}
$config->{"nostat"}
$config->{"noscan"}
Truth table100%3/3
A or B
A= $config->{"nostat"}B= $config->{"noscan"}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | $self->throw("$path: not a directory") | ||||||||||||||||||||||||||||
| 56 | 19 | ▶ | unless $self->{ isdir }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 19 | $self->scan($config); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 19 | return $self; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 65 | # scan(\%config) | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | # Scan directory for files and sub-directories. | ||||||||||||||||||||||||||||
| 68 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 23 | ▶ | sub scan { | ||||||||||||||||||||||||||
|
Subroutine
scan: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 23 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 72 | 23 | ▶ | $config ||= { }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config ||= {}
MC/DC100%1/1
$config ||= {}
$config
Truth table100%2/2
A
A= $config
| |||||||||||||||||||||||||||||
| 73 | 23 | local *DH; | |||||||||||||||||||||||||||
| 74 | 23 | my ($dir, @files, $name, $abs, $rel, $item); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # set 'noscan' in config if recurse isn't set, to ensure Directories | ||||||||||||||||||||||||||||
| 77 | # created don't try to scan deeper | ||||||||||||||||||||||||||||
| 78 | 23 | ▶ | $config->{ noscan } = 1 unless $config->{ recurse }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 23 | $dir = $self->{ abs }; | |||||||||||||||||||||||||||
| 81 | 23 | ▶ | opendir(DH, $dir) or return $self->throw("$dir: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 23 | @files = readdir DH; | |||||||||||||||||||||||||||
| 84 | 23 | ▶ | closedir(DH) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | or return $self->throw("$dir close: $!"); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 23 | my ($path, $files, $dirs, $list) = @$self{ qw( path files dirs list ) }; | |||||||||||||||||||||||||||
| 88 | 23 | @$files = @$dirs = @$list = (); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 23 | foreach $name (sort @files) { | |||||||||||||||||||||||||||
| 91 | 125 | ▶ | next if $name =~ /^\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 79 | $abs = File::Spec->catfile($dir, $name); | |||||||||||||||||||||||||||
| 93 | 79 | $rel = File::Spec->catfile($path, $name); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 79 | ▶ | if (-d $abs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 22 | $item = Template::Plugin::Directory->new(undef, $rel, $config); | |||||||||||||||||||||||||||
| 97 | 22 | push(@$dirs, $item); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 57 | $item = Template::Plugin::File->new(undef, $rel, $config); | |||||||||||||||||||||||||||
| 101 | 57 | push(@$files, $item); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 79 | push(@$list, $item); | |||||||||||||||||||||||||||
| 104 | 79 | $self->{ _dir }->{ $name } = $item; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 23 | return ''; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 112 | # file($filename) | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # Fetch a named file from this directory. | ||||||||||||||||||||||||||||
| 115 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub file { | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 119 | 1 | return $self->{ _dir }->{ $name }; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 124 | # present($view) | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | # Present self to a Template::View | ||||||||||||||||||||||||||||
| 127 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | ▶ | sub present { | ||||||||||||||||||||||||||
|
Subroutine
present: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 3 | my ($self, $view) = @_; | |||||||||||||||||||||||||||
| 131 | 3 | $view->view_directory($self); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 136 | # content($view) | ||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||
| 138 | # Present directory content to a Template::View. | ||||||||||||||||||||||||||||
| 139 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | ▶ | sub content { | ||||||||||||||||||||||||||
|
Subroutine
content: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 3 | my ($self, $view) = @_; | |||||||||||||||||||||||||||
| 143 | 3 | ▶ | return $self->{ list } unless $view; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 3 | my $output = ''; | |||||||||||||||||||||||||||
| 145 | 3 | foreach my $file (@{ $self->{ list } }) { | |||||||||||||||||||||||||||
| 146 | 9 | $output .= $file->present($view); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 3 | return $output; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 153 | # throw($msg) | ||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||
| 155 | # Throw a 'Directory' exception. | ||||||||||||||||||||||||||||
| 156 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | ▶ | sub throw { | ||||||||||||||||||||||||||
|
Subroutine
throw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 2 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 160 | 2 | die (Template::Exception->new('Directory', $error)); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | 1; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||