Sticker/Scribe/Yaml.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Sticker/Scribe/Xattr.pm ↑ Summary Sticker/Scribe/YamlPrefix.pm »
1 package File::Sticker::Scribe::Yaml;
2 $File::Sticker::Scribe::Yaml::VERSION = '4.9';
3 =head1 NAME
4  
5 File::Sticker::Scribe::Yaml - read, write and standardize meta-data from YAML file
6  
7 =head1 VERSION
8  
9 version 4.9
10  
11 =head1 SYNOPSIS
12  
13 use File::Sticker::Scribe::Yaml;
14  
15 my $obj = File::Sticker::Scribe::Yaml->new(%args);
16  
17 my %meta = $obj->read_meta($filename);
18  
19 $obj->write_meta(%args);
20  
21 =head1 DESCRIPTION
22  
23 This will read and write meta-data from YAML files, and standardize it to a common
24 nomenclature, such as "tags" for things called tags, or Keywords or Subject etc.
25  
26 =cut
27  
28 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
30 use YAML::Any qw(Dump LoadFile DumpFile);
31  
32 use parent qw(File::Sticker::Scribe);
33  
34 # FOR DEBUGGING
35 =head1 DEBUGGING
36  
37 =head2 whoami
38  
39 Used for debugging info
40  
41 =cut
42 sub whoami { ( caller(1) )[3] }
43  
44 =head1 METHODS
45  
46 =head2 priority
47  
48 The priority of this scribe. Scribes with higher priority get tried first.
49  
50 =cut
51  
52 sub priority {
53 my $class = shift;
54 return 2;
55 } # priority
56  
57 =head2 allowed_file
58  
59 If this scribe can be used for the given file, then this returns true.
60 File must be plain text and end with '.yml'
61 Howwever, if the file DOES NOT EXIST, it CAN be WRITTEN TO, so return true then as well.
62 This is the only case where the file doesn't need to exist beforehand.
63 Note that if the file exists and is a directory, then it is not an allowed file!
64 If the file exists and is empty, that's okay too.
65  
66 =cut
67  
68 sub allowed_file {
69 my $self = shift;
70 my $file = shift;
71 say STDERR whoami() if $self->{verbose} > 2;
72  
73 if ($file !~ /\.yml$/)
74 {
75 say STDERR "$file does not end with .yml" if $self->{verbose} > 2;
76 return 0;
77 }
78 if (-d $file)
79 {
80 return 0;
81 }
82 if (! -r $file)
83 {
84 say STDERR "$file does not exist, but that's okay" if $self->{verbose} > 2;
85 return 1;
86 }
87 # Perhaps the file exists and is empty
88 if (-z $file)
89 {
90 say STDERR "$file is empty, but that's okay" if $self->{verbose} > 2;
91 return 1;
92 }
93  
94 my $ft = $self->{file_magic}->info_from_filename($file);
95 # For some unfathomable reason, not every YAML file is recognised as text/plain
96 # so just check for text
97 if ($ft->{mime_type} =~ m{^text/})
98 {
99 return 1;
100 }
101 return 0;
102 } # allowed_file
103  
104 =head2 allowed_fields
105  
106 If this scribe can be used for the known and wanted fields, then this returns true.
107 For YAML, this always returns true.
108  
109 if ($scribe->allowed_fields())
110 {
111 ....
112 }
113  
114 =cut
115  
116 sub allowed_fields {
117 my $self = shift;
118  
119 return 1;
120 } # allowed_fields
121  
122 =head2 known_fields
123  
124 Returns the fields which this scribe knows about.
125 This scribe has no limitations.
126  
127 my $known_fields = $scribe->known_fields();
128  
129 =cut
130  
131 sub known_fields {
132 my $self = shift;
133  
134 if ($self->{wanted_fields})
135 {
136 return $self->{wanted_fields};
137 }
138 return {};
139 } # known_fields
140  
141 =head2 read_meta
142  
143 Read the meta-data from the given file.
144  
145 my $meta = $obj->read_meta($filename);
146  
147 =cut
148  
149 sub read_meta {
150 my $self = shift;
151 my $filename = shift;
152 say STDERR whoami() if $self->{verbose} > 2;
153  
154 my ($info) = LoadFile($filename);
155 my %meta = ();
156 foreach my $key (sort keys %{$info})
157 {
158 my $val = $info->{$key};
159 if ($val)
160 {
161 if ($key eq 'tags')
162 {
163 $meta{tags} = $val;
164 # If there are no commas, change spaces to commas. This is
165 # because if we are using commas to separate, we allow
166 # multi-word tags with spaces in them, so we don't want to turn
167 # those spaces into commas!
168 if ($meta{tags} !~ /,/)
169 {
170 $meta{tags} =~ s/ /,/g; # spaces to commas
171 }
172 }
173 elsif ($key eq 'dublincore.source')
174 {
175 $meta{'url'} = $val;
176 }
177 elsif ($key eq 'dublincore.title')
178 {
179 $meta{'title'} = $val;
180 }
181 elsif ($key eq 'dublincore.creator')
182 {
183 $meta{'creator'} = $val;
184 }
185 elsif ($key eq 'dublincore.description')
186 {
187 $meta{'description'} = $val;
188 }
189 elsif ($key eq 'private')
190 {
191 # deal with this after tags
192 }
193 else
194 {
195 $meta{$key} = $val;
196 }
197 }
198 }
199 if ($info->{private})
200 {
201 $meta{tags} .= ",private";
202 }
203 return \%meta;
204 } # read_meta
205  
206 =head2 delete_field_from_file
207  
208 Completely remove the given field.
209 This does no checking for multi-valued fields, it just deletes the whole thing.
210  
211 $scribe->delete_field_from_file(filename=>$filename,field=>$field);
212  
213 =cut
214  
215 sub delete_field_from_file {
216 my $self = shift;
217 my %args = @_;
218 say STDERR whoami() if $self->{verbose} > 2;
219  
220 my $filename = $args{filename};
221 my $field = $args{field};
222  
223 my ($info) = LoadFile($filename);
224 delete $info->{$field};
225 DumpFile($filename, $info);
226 } # delete_field_from_file
227  
228 =head2 replace_all_meta
229  
230 Overwrite the existing meta-data with that given.
231  
232 (This supercedes the parent method because we can do it more efficiently this way)
233  
234 $scribe->replace_all_meta(filename=>$filename,meta=>\%meta);
235  
236 =cut
237  
238 sub replace_all_meta {
239 my $self = shift;
240 my %args = @_;
241 say STDERR whoami() if $self->{verbose} > 2;
242  
243 my $filename = $args{filename};
244 my $meta = $args{meta};
245  
246 DumpFile($filename, $meta);
247 } # replace_all_meta
248  
249 =head1 Helper Functions
250  
251 Private interface.
252  
253 =head2 replace_one_field
254  
255 Overwrite the given field. This does no checking.
256  
257 $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value);
258  
259 =cut
260  
261 sub replace_one_field {
262 my $self = shift;
263 my %args = @_;
264 say STDERR whoami() if $self->{verbose} > 2;
265  
266 my $filename = $args{filename};
267 my $field = $args{field};
268 my $value = $args{value};
269  
270 my ($info) = LoadFile($filename);
271 $info->{$field} = $value;
272 DumpFile($filename, $info);
273 } # replace_one_field
274  
275 =head1 BUGS
276  
277 Please report any bugs or feature requests to the author.
278  
279 =cut
280  
281 1; # End of File::Sticker::Scribe
282 __END__
« Sticker/Scribe/Xattr.pm ↑ Summary Sticker/Scribe/YamlPrefix.pm »