| 1 | package Filename::KeyValue; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Perinci::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI::Escape qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 14 | 1 | our $DATE = '2026-05-29'; # DATE | |||||||||||||||||||||||||||
| 15 | 1 | our $DIST = 'Filename-KeyValue'; # DIST | |||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 19 | parse_keyvalue_filename | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | # normalize_keyvalue_filename | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _decode_val { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($opts, $kv, $key, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_decode_val: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my @old_vals = exists($kv->{$key}) ? (ref($kv->{$key}) eq 'ARRAY' ? @{ $kv->{$key} } : ($kv->{$key})) : (); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my @new_vals = split /,/, ($opts->{decode_value} ? URI::Escape::uri_unescape($val) : $val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | my @vals = (@old_vals, @new_vals); | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($opts->{array_value} || @vals > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts->{"array_value"} or @vals > 1
MC/DC0%0/2
$opts->{"array_value"} or @vals > 1
$opts->{"array_value"}
@vals > 1
Truth table0%0/3
A or B
A= $opts->{"array_value"}B= @vals > 1
| |||||||||||||||||||||||||||||
| 32 | 0 | $val = \@vals; | |||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||
| 34 | 0 | $val = $vals[0]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $SPEC{parse_keyvalue_filename} = { | ||||||||||||||||||||||||||||
| 39 | 1 | v => 1.1, | |||||||||||||||||||||||||||
| 40 | summary => 'Parse filename using the KeyValue naming scheme', | ||||||||||||||||||||||||||||
| 41 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The KeyValue naming scheme puts key=value pairs at the end of filename. Filename | ||||||||||||||||||||||||||||
| 44 | must match this regex: | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | /\A | ||||||||||||||||||||||||||||
| 47 | (?: | ||||||||||||||||||||||||||||
| 48 | (.+?) # optional prefix (part before the first key) | ||||||||||||||||||||||||||||
| 49 | - | ||||||||||||||||||||||||||||
| 50 | )? | ||||||||||||||||||||||||||||
| 51 | ( | ||||||||||||||||||||||||||||
| 52 | (?: | ||||||||||||||||||||||||||||
| 53 | ([A-Za-z_][A-Za_z0-9_]*) # key | ||||||||||||||||||||||||||||
| 54 | = | ||||||||||||||||||||||||||||
| 55 | ([^-]*) # value | ||||||||||||||||||||||||||||
| 56 | ) | ||||||||||||||||||||||||||||
| 57 | (?: | ||||||||||||||||||||||||||||
| 58 | - | ||||||||||||||||||||||||||||
| 59 | ([A-Za-z_][A-Za_z0-9_]*) | ||||||||||||||||||||||||||||
| 60 | = | ||||||||||||||||||||||||||||
| 61 | ([^-]*) | ||||||||||||||||||||||||||||
| 62 | )* | ||||||||||||||||||||||||||||
| 63 | (\.\w+)? # optional filename extension | ||||||||||||||||||||||||||||
| 64 | \z/x | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | KeyValue naming scheme is used in the AssetView media assets organization scheme | ||||||||||||||||||||||||||||
| 67 | (see <pm:Media::AssetView>). | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | This routine parses a filename and return a structure containing parsed | ||||||||||||||||||||||||||||
| 70 | elements. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | MARKDOWN | ||||||||||||||||||||||||||||
| 73 | args => { | ||||||||||||||||||||||||||||
| 74 | filename => { | ||||||||||||||||||||||||||||
| 75 | schema => 'filename*', | ||||||||||||||||||||||||||||
| 76 | req => 1, | ||||||||||||||||||||||||||||
| 77 | pos => 0, | ||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | array_value => { | ||||||||||||||||||||||||||||
| 80 | summary => 'Always/never/maybe return value as array', | ||||||||||||||||||||||||||||
| 81 | schema => ['int*', in => [0,1,2]], | ||||||||||||||||||||||||||||
| 82 | default => 1, | ||||||||||||||||||||||||||||
| 83 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The default (1) is to return a scalar when there is a single value, or an array | ||||||||||||||||||||||||||||
| 86 | if there are multiple values, for example: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | foo-kw1=val1-kw2=val2,val2b-kw3=val3-kw1=val1b.jpg | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | then: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | kw1 = ['val1', 'val1b'] | ||||||||||||||||||||||||||||
| 93 | kw2 = ['val2', 'val2b'] | ||||||||||||||||||||||||||||
| 94 | kw3 = 'val3' | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | The setting 0 means to never return array, so will return a comma-separated | ||||||||||||||||||||||||||||
| 97 | string instead. However, if the value is URI-decoded then this can potentially | ||||||||||||||||||||||||||||
| 98 | be ambiguous: | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | kw1=val1,val2-kw1=val3%2cval4.jpg | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | under array_value=0 and decode_value=1 will return: | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | kw1 = 'val1,val2,val3,val4' | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | while under array_value=1 or 2 will return 3 elements: | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | kw1 = ['val1', 'val2', 'val3,val4'] | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | MARKDOWN | ||||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | decode_value => { | ||||||||||||||||||||||||||||
| 113 | summary => 'Whether to decode value with URI-encoding', | ||||||||||||||||||||||||||||
| 114 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 115 | default => 1, | ||||||||||||||||||||||||||||
| 116 | }, | ||||||||||||||||||||||||||||
| 117 | # opt: case_insensitive | ||||||||||||||||||||||||||||
| 118 | # opt: check duplicate key | ||||||||||||||||||||||||||||
| 119 | # opt: required_keys | ||||||||||||||||||||||||||||
| 120 | # opt: required_value or schema for values | ||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||
| 122 | examples => [ | ||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | args => {filename=>'foo.jpg'}, | ||||||||||||||||||||||||||||
| 125 | summary => 'No key=value pairs', | ||||||||||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | args => {filename=>'foo-bar-kw1=val1.jpg'}, | ||||||||||||||||||||||||||||
| 129 | summary => 'A single key=value pair', | ||||||||||||||||||||||||||||
| 130 | }, | ||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | args => {filename=>'foo-bar-kw1=val1-kw2=val2.jpg'}, | ||||||||||||||||||||||||||||
| 133 | summary => 'Two key=value pairs', | ||||||||||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | args => {filename=>'foo-bar-kw1=val1,val1b.jpg'}, | ||||||||||||||||||||||||||||
| 137 | summary => 'A single key=value pair containing two values', | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | args => {filename=>'foo-bar-kw1=val1-kw2=val2,val2b,val2c-kw3=val3.jpg'}, | ||||||||||||||||||||||||||||
| 141 | summary => 'Three key=value pairs, one containing multiple values', | ||||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | args => {filename=>'foo-bar-kw1=-kw2=-kw3=val.jpg'}, | ||||||||||||||||||||||||||||
| 145 | summary => 'Empty key=value pairs', | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | args => {filename=>'foo-bar-kw1_containing_dash=containing%2ddash-kw2_also_containing_dash=containing%2Dtwo%2Ddashes.jpg'}, | ||||||||||||||||||||||||||||
| 149 | summary => 'A value containing dash', | ||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||
| 152 | args => {filename=>'foo-bar-kw1=containing%2ccomma.jpg'}, | ||||||||||||||||||||||||||||
| 153 | summary => 'A value containing comma', | ||||||||||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||||||||||
| 155 | ], | ||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | sub parse_keyvalue_filename { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_keyvalue_filename: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | defined(my $filename = $args{filename}) or return [400, "Please specify filename"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | my $res = {}; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | my $opts = {}; | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | $opts->{array_value} = delete($args{array_value}) // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{"array_value"} // 0
MC/DC0%0/1
delete $args{"array_value"} // 0
delete $args{"array_value"}
Truth table0%0/2
A
A= delete $args{"array_value"}
| |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $opts->{decode_value} = delete($args{decode_value}) // 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{"decode_value"} // 1
MC/DC0%0/1
delete $args{"decode_value"} // 1
delete $args{"decode_value"}
Truth table0%0/2
A
A= delete $args{"decode_value"}
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | $filename =~ s!/+\z!!; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | length($filename) or return [400, "Filename cannot be empty"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $filename =~ s/\.(\w+)\z// and $res->{ext} = $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $filename =~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | \A | ||||||||||||||||||||||||||||
| 174 | (?: | ||||||||||||||||||||||||||||
| 175 | (.+?) # optional prefix (part before the first key) | ||||||||||||||||||||||||||||
| 176 | )? | ||||||||||||||||||||||||||||
| 177 | (?: | ||||||||||||||||||||||||||||
| 178 | ( | ||||||||||||||||||||||||||||
| 179 | (?: | ||||||||||||||||||||||||||||
| 180 | (?:[A-Za-z_][A-Za-z0-9_]*) # key | ||||||||||||||||||||||||||||
| 181 | = | ||||||||||||||||||||||||||||
| 182 | (?:[^-]*) # value | ||||||||||||||||||||||||||||
| 183 | ) | ||||||||||||||||||||||||||||
| 184 | (?: | ||||||||||||||||||||||||||||
| 185 | - | ||||||||||||||||||||||||||||
| 186 | (?: | ||||||||||||||||||||||||||||
| 187 | (?:[A-Za-z_][A-Za-z0-9_]*) # key | ||||||||||||||||||||||||||||
| 188 | = | ||||||||||||||||||||||||||||
| 189 | (?:[^-]*) # value | ||||||||||||||||||||||||||||
| 190 | ) | ||||||||||||||||||||||||||||
| 191 | )*? | ||||||||||||||||||||||||||||
| 192 | ) | ||||||||||||||||||||||||||||
| 193 | )? | ||||||||||||||||||||||||||||
| 194 | \z/x | ||||||||||||||||||||||||||||
| 195 | or return [400, "Invalid filename syntax, must be in (PREFIX-)?(KW=VAL)*(.EXT)? format"]; | ||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $res->{prefix} = $1 // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$1 // ""
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
| |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $res->{kv_raw} = $2 // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$2 // ""
MC/DC0%0/1
$2 // ""
$2
Truth table0%0/2
A
A= $2
| |||||||||||||||||||||||||||||
| 198 | 0 | $res->{kv} = {}; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if (length $res->{kv_raw}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | $res->{prefix} =~ /-\z/ or return [400, "Invalid filename syntax, must be in PREFIX-(KW=VAL)*(.EXT)? format (2)"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if (length $res->{kv_raw}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | while ($res->{kv_raw} =~ /([A-Za-z_][^=]*)=([^-]+)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | $res->{kv}{$1} = _decode_val($opts, $res->{kv}, $1, $2); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 0 | [200, "OK", $res]; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub _normalize { | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $parse_res = shift; | ||||||||||||||||||||||||||
|
Subroutine
_normalize: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | my @kv = map { | ||||||||||||||||||||||||||||
| 216 | 0 | my $key = $_; | |||||||||||||||||||||||||||
| 217 | "$key=" . | ||||||||||||||||||||||||||||
| 218 | join( | ||||||||||||||||||||||||||||
| 219 | ",", | ||||||||||||||||||||||||||||
| 220 | 0 | map { URI::Escape::uri_unescape($_) } | |||||||||||||||||||||||||||
| 221 | 0 | @{ $parse_res->[2]{kv}{$key} } | |||||||||||||||||||||||||||
| 222 | 0 | )} sort keys %{ $parse_res->[2]{kv} }; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | return [ | ||||||||||||||||||||||||||||
| 225 | 200, | ||||||||||||||||||||||||||||
| 226 | "OK", | ||||||||||||||||||||||||||||
| 227 | join( | ||||||||||||||||||||||||||||
| 228 | "", | ||||||||||||||||||||||||||||
| 229 | $parse_res->[2]{prefix}, | ||||||||||||||||||||||||||||
| 230 | ($parse_res->[2]{prefix} =~ /-\z/ ? "" : "-"), | ||||||||||||||||||||||||||||
| 231 | join("-", @kv), | ||||||||||||||||||||||||||||
| 232 | (defined $parse_res->[2]{ext} ? "." : ""), | ||||||||||||||||||||||||||||
| 233 | $parse_res->[2]{ext}, | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | ), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 235 | ]; | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | $SPEC{normalize_keyvalue_filename} = { | ||||||||||||||||||||||||||||
| 239 | 1 | v => 1.1, | |||||||||||||||||||||||||||
| 240 | summary => 'Normalize filename that has the key-values following the KeyValue naming scheme', | ||||||||||||||||||||||||||||
| 241 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | This routine will: | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | - sort the key-value using keys asciibetically; | ||||||||||||||||||||||||||||
| 246 | - (NOT YET IMPLEMENTED) optionally sort the values asciibetically; | ||||||||||||||||||||||||||||
| 247 | - (NOT YET IMPLEMENTED) customize key sorting using SortKey. | ||||||||||||||||||||||||||||
| 248 | - (NOT YET IMPLEMENTED) customize value sorting using SortKey. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | MARKDOWN | ||||||||||||||||||||||||||||
| 251 | args => { | ||||||||||||||||||||||||||||
| 252 | filename => { | ||||||||||||||||||||||||||||
| 253 | schema => 'filename*', | ||||||||||||||||||||||||||||
| 254 | req => 1, | ||||||||||||||||||||||||||||
| 255 | pos => 0, | ||||||||||||||||||||||||||||
| 256 | }, | ||||||||||||||||||||||||||||
| 257 | }, | ||||||||||||||||||||||||||||
| 258 | examples => [ | ||||||||||||||||||||||||||||
| 259 | { | ||||||||||||||||||||||||||||
| 260 | args => {filename=>'foo-bar-kw2=val2-kw1=val1.jpg'}, | ||||||||||||||||||||||||||||
| 261 | summary => 'Sort keys', | ||||||||||||||||||||||||||||
| 262 | }, | ||||||||||||||||||||||||||||
| 263 | ], | ||||||||||||||||||||||||||||
| 264 | }; | ||||||||||||||||||||||||||||
| 265 | sub normalize_keyvalue_filename { | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
normalize_keyvalue_filename: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | defined(my $filename = $args{filename}) or return [400, "Please specify filename"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | my $parse_res = parse_keyvalue_filename( | |||||||||||||||||||||||||||
| 270 | filename => $filename, | ||||||||||||||||||||||||||||
| 271 | decode_value => 1, | ||||||||||||||||||||||||||||
| 272 | array_value => 2, | ||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||
| 274 | 0 | ▶ | return [500, "Can't parse filename: $parse_res->[0] - $parse_res->[1]"] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | unless $parse_res->[0] == 200; | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | _normalize($parse_res); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | $SPEC{modify_keyvalue_filename} = { | ||||||||||||||||||||||||||||
| 281 | 1 | v => 1.1, | |||||||||||||||||||||||||||
| 282 | summary => 'Add/remove/replace key-values in filename that follow KeyValue naming scheme', | ||||||||||||||||||||||||||||
| 283 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | This routine will: | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | - sort the key-value using keys asciibetically; | ||||||||||||||||||||||||||||
| 288 | - (NOT YET IMPLEMENTED) optionally sort the values asciibetically; | ||||||||||||||||||||||||||||
| 289 | - (NOT YET IMPLEMENTED) customize key sorting using SortKey. | ||||||||||||||||||||||||||||
| 290 | - (NOT YET IMPLEMENTED) customize value sorting using SortKey. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | MARKDOWN | ||||||||||||||||||||||||||||
| 293 | args => { | ||||||||||||||||||||||||||||
| 294 | filename => { | ||||||||||||||||||||||||||||
| 295 | schema => 'filename*', | ||||||||||||||||||||||||||||
| 296 | req => 1, | ||||||||||||||||||||||||||||
| 297 | pos => 0, | ||||||||||||||||||||||||||||
| 298 | }, | ||||||||||||||||||||||||||||
| 299 | add => { | ||||||||||||||||||||||||||||
| 300 | summary => 'List of key=>value pairs to add', | ||||||||||||||||||||||||||||
| 301 | schema => 'hash*', | ||||||||||||||||||||||||||||
| 302 | }, | ||||||||||||||||||||||||||||
| 303 | ignore => { | ||||||||||||||||||||||||||||
| 304 | summary => 'If set, when filename already has the same key, do nothing', | ||||||||||||||||||||||||||||
| 305 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 306 | }, | ||||||||||||||||||||||||||||
| 307 | remove => { | ||||||||||||||||||||||||||||
| 308 | summary => 'List of keys to remove', | ||||||||||||||||||||||||||||
| 309 | schema => ['array*', of=>'str*'], | ||||||||||||||||||||||||||||
| 310 | }, | ||||||||||||||||||||||||||||
| 311 | replace => { | ||||||||||||||||||||||||||||
| 312 | summary => 'List of key=>value pairs to replace', | ||||||||||||||||||||||||||||
| 313 | schema => ['hash*'], | ||||||||||||||||||||||||||||
| 314 | }, | ||||||||||||||||||||||||||||
| 315 | }, | ||||||||||||||||||||||||||||
| 316 | examples => [ | ||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||
| 318 | args => {filename=>'foo-bar-kw1=val1.jpg', add=>{kw2=>"val2"}}, | ||||||||||||||||||||||||||||
| 319 | summary => 'Add a key', | ||||||||||||||||||||||||||||
| 320 | }, | ||||||||||||||||||||||||||||
| 321 | ], | ||||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||
| 323 | sub modify_keyvalue_filename { | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
modify_keyvalue_filename: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 0 | ▶ | defined(my $filename = $args{filename}) or return [400, "Please specify filename"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | my $parse_res = parse_keyvalue_filename( | |||||||||||||||||||||||||||
| 328 | filename => $filename, | ||||||||||||||||||||||||||||
| 329 | decode_value => 1, | ||||||||||||||||||||||||||||
| 330 | array_value => 2, | ||||||||||||||||||||||||||||
| 331 | ); | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | return [500, "Can't parse filename: $parse_res->[0] - $parse_res->[1]"] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | unless $parse_res->[0] == 200; | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | ▶ | if ($args{add} && keys %{$args{add}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"add"} and keys %{$args{"add"};}
MC/DC0%0/2
$args{"add"} and keys %{$args{"add"};}
$args{"add"}
keys %{$args{"add"};}
Truth table0%0/3
A and B
A= $args{"add"}B= keys %{$args{"add"};}
| |||||||||||||||||||||||||||||
| 336 | 0 | for my $key (sort keys %{ $args{add} }) { | |||||||||||||||||||||||||||
| 337 | 0 | ▶ | next if $args{ignore} && exists($parse_res->[2]{kv}{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"ignore"} and exists $parse_res->[2]{"kv"}{$key}
MC/DC0%0/2
$args{"ignore"} and exists $parse_res->[2]{"kv"}{$key}
$args{"ignore"}
exists $parse_res->[2]{"kv"}{$key}
Truth table0%0/3
A and B
A= $args{"ignore"}B= exists $parse_res->[2]{"kv"}{$key}
| |||||||||||||||||||||||||||||
| 338 | 0 | ▶ | $parse_res->[2]{kv}{$key} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$parse_res->[2]{"kv"}{$key} //= []
MC/DC0%0/1
$parse_res->[2]{"kv"}{$key} //= []
$parse_res->[2]{"kv"}{$key}
Truth table0%0/2
A
A= $parse_res->[2]{"kv"}{$key}
| |||||||||||||||||||||||||||||
| 339 | 0 | push @{ $parse_res->[2]{kv}{$key} }, $args{add}{$key}; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | if ($args{remove} && @{$args{remove}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"remove"} and @{$args{"remove"};}
MC/DC0%0/2
$args{"remove"} and @{$args{"remove"};}
$args{"remove"}
@{$args{"remove"};}
Truth table0%0/3
A and B
A= $args{"remove"}B= @{$args{"remove"};}
| |||||||||||||||||||||||||||||
| 344 | 0 | for my $key (@{ $args{remove} }) { | |||||||||||||||||||||||||||
| 345 | 0 | delete $parse_res->[2]{kv}{$key}; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if ($args{replace} && keys %{$args{replace}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"replace"} and keys %{$args{"replace"};}
MC/DC0%0/2
$args{"replace"} and keys %{$args{"replace"};}
$args{"replace"}
keys %{$args{"replace"};}
Truth table0%0/3
A and B
A= $args{"replace"}B= keys %{$args{"replace"};}
| |||||||||||||||||||||||||||||
| 350 | 0 | for my $key (sort keys %{ $args{replace} }) { | |||||||||||||||||||||||||||
| 351 | 0 | $parse_res->[2]{kv}{$key} = $args{replace}{$key}; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | _normalize($parse_res); | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | my $wrap_code = sub { | ||||||||||||||||||||||||||||
| 359 | 0 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 362 | 0 | my $filenames = delete $args{filename}; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | my $res_multi = envresmulti(); | |||||||||||||||||||||||||||
| 365 | 0 | for my $filename (@$filenames) { | |||||||||||||||||||||||||||
| 366 | 0 | ▶ | unless (-f $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 0 | $res_multi->add_result(412, "Can't find file", {item_id=>$filename}); | |||||||||||||||||||||||||||
| 368 | 0 | next; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | my $res = $orig->(%args, filename=>$filename); | |||||||||||||||||||||||||||
| 372 | 0 | ▶ | unless ($res->[0] == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | $res_multi->add_result(500, "Can't rename: $res->[0] - $res->[1]", {item_id=>$filename}); | |||||||||||||||||||||||||||
| 374 | 0 | next; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | 0 | ▶ | if ($res->[2] eq $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | $res_multi->add_result(304, "Not modified", {item_id=>$filename}); | |||||||||||||||||||||||||||
| 378 | 0 | next; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 0 | log_debug "Renaming %s to %s ...", $filename, $res->[2]; | |||||||||||||||||||||||||||
| 382 | 0 | ▶ | if (rename $filename, $res->[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | 0 | $res_multi->add_result(200, "OK", {item_id=>$filename}); | |||||||||||||||||||||||||||
| 384 | } else { | ||||||||||||||||||||||||||||
| 385 | 0 | $res_multi->add_result(500, "Can't rename $filename to $res->[2]: $!", {item_id=>$filename}); | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | $res_multi->as_struct; | |||||||||||||||||||||||||||
| 390 | 1 | }; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 393 | die => 1, | ||||||||||||||||||||||||||||
| 394 | output_name => 'rename_add_keyvalue_filenames', | ||||||||||||||||||||||||||||
| 395 | base_name => 'modify_keyvalue_filename', | ||||||||||||||||||||||||||||
| 396 | remove_args => [qw/replace remove/], | ||||||||||||||||||||||||||||
| 397 | modify_args => { | ||||||||||||||||||||||||||||
| 398 | filename => sub { | ||||||||||||||||||||||||||||
| 399 | $_[0]{schema} = ["array*", of=>"str*"], | ||||||||||||||||||||||||||||
| 400 | 1 | ▶ | $_[0]{slurpy} = 1; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 401 | }, | ||||||||||||||||||||||||||||
| 402 | }, | ||||||||||||||||||||||||||||
| 403 | modify_meta => sub { | ||||||||||||||||||||||||||||
| 404 | 1 | ▶ | my $meta = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 405 | 1 | delete $meta->{examples}; | |||||||||||||||||||||||||||
| 406 | }, | ||||||||||||||||||||||||||||
| 407 | 1 | summary => 'Rename file by adding key=value pairs', | |||||||||||||||||||||||||||
| 408 | wrap_code => $wrap_code, | ||||||||||||||||||||||||||||
| 409 | ); | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 412 | die => 1, | ||||||||||||||||||||||||||||
| 413 | output_name => 'rename_remove_keyvalue_filenames', | ||||||||||||||||||||||||||||
| 414 | base_name => 'modify_keyvalue_filename', | ||||||||||||||||||||||||||||
| 415 | remove_args => [qw/add replace/], | ||||||||||||||||||||||||||||
| 416 | modify_args => { | ||||||||||||||||||||||||||||
| 417 | filename => sub { | ||||||||||||||||||||||||||||
| 418 | $_[0]{schema} = ["array*", of=>"str*"], | ||||||||||||||||||||||||||||
| 419 | 1 | ▶ | $_[0]{slurpy} = 1; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 420 | }, | ||||||||||||||||||||||||||||
| 421 | }, | ||||||||||||||||||||||||||||
| 422 | modify_meta => sub { | ||||||||||||||||||||||||||||
| 423 | 1 | ▶ | my $meta = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 424 | 1 | delete $meta->{examples}; | |||||||||||||||||||||||||||
| 425 | }, | ||||||||||||||||||||||||||||
| 426 | 1 | summary => 'Rename file by removing keys', | |||||||||||||||||||||||||||
| 427 | wrap_code => $wrap_code, | ||||||||||||||||||||||||||||
| 428 | ); | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 1 | 1; | |||||||||||||||||||||||||||
| 431 | # ABSTRACT: Parse filename using the KeyValue naming scheme | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | __END__ | ||||||||||||||||||||||||||||