Sticker/Scribe/Xattr.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/Mp3.pm ↑ Summary Sticker/Scribe/Yaml.pm »
1 package File::Sticker::Scribe::Xattr;
2 $File::Sticker::Scribe::Xattr::VERSION = '4.9';
3 =head1 NAME
4  
5 File::Sticker::Scribe::Xattr - read, write and standardize meta-data from ExtAttr file
6  
7 =head1 VERSION
8  
9 version 4.9
10  
11 =head1 SYNOPSIS
12  
13 use File::Sticker::Scribe::Xattr;
14  
15 my $obj = File::Sticker::Scribe::Xattr->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 extended user attributes of 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 File::ExtAttr ':all';
31 use File::Basename;
32  
33 use parent qw(File::Sticker::Scribe);
34  
35 # FOR DEBUGGING
36 =head1 DEBUGGING
37  
38 =head2 whoami
39  
40 Used for debugging info
41  
42 =cut
43 sub whoami { ( caller(1) )[3] }
44  
45 =head1 METHODS
46  
47 =head2 priority
48  
49 The priority of this scribe. Scribes with higher priority get tried first.
50 Xattr is a low-priority scribe.
51  
52 =cut
53  
54 sub priority {
55 my $class = shift;
56 return 0;
57 } # priority
58  
59 =head2 allowed_file
60  
61 If this scribe can be used for the given file, then this returns true.
62 This can be used with any file, if the filesystem supports extended attributes.
63 I don't know how to test for that, so I'll just assume "yes".
64  
65 =cut
66  
67 sub allowed_file {
68 my $self = shift;
69 my $file = shift;
70 say STDERR whoami() if $self->{verbose} > 2;
71  
72 if (-r $file)
73 {
74 return 1;
75 }
76 return 0;
77 } # allowed_file
78  
79 =head2 allowed_fields
80  
81 If this scribe can be used for the known and wanted fields, then this returns true.
82 For Xattr, this always returns true.
83  
84 if ($scribe->allowed_fields())
85 {
86 ....
87 }
88  
89 =cut
90  
91 sub allowed_fields {
92 my $self = shift;
93  
94 return 1;
95 } # allowed_fields
96  
97 =head2 known_fields
98  
99 Returns the fields which this scribe knows about.
100 This scribe has no limitations.
101  
102 my $known_fields = $scribe->known_fields();
103  
104 =cut
105  
106 sub known_fields {
107 my $self = shift;
108  
109 if ($self->{wanted_fields})
110 {
111 return $self->{wanted_fields};
112 }
113 return {};
114 } # known_fields
115  
116 =head2 read_meta
117  
118 Read the meta-data from the given file.
119  
120 my $meta = $obj->read_meta($filename);
121  
122 =cut
123  
124 sub read_meta {
125 my $self = shift;
126 my $filename = shift;
127 say STDERR whoami() if $self->{verbose} > 2;
128  
129 my %meta = ();
130 foreach my $key (listfattr($filename))
131 {
132 if ($key eq 'dublincore.source' or $key eq 'xdg.referrer.url')
133 {
134 $meta{url} = getfattr($filename, $key);
135 }
136 elsif ($key eq 'dublincore.creator')
137 {
138 $meta{creator} = getfattr($filename, $key);
139 }
140 elsif ($key eq 'dublincore.title')
141 {
142 $meta{title} = getfattr($filename, $key);
143 }
144 elsif ($key eq 'dublincore.alternative')
145 {
146 $meta{alt_title} = getfattr($filename, $key);
147 }
148 elsif ($key eq 'dublincore.description')
149 {
150 $meta{description} = getfattr($filename, $key);
151 }
152 else
153 {
154 $meta{$key} = getfattr($filename, $key);
155 }
156 }
157  
158 return \%meta;
159 } # read_meta
160  
161 =head2 delete_field_from_file
162  
163 Completely remove the given field.
164 For multi-value fields, it removes ALL the values.
165  
166 $scribe->delete_field_from_file(filename=>$filename,field=>$field);
167  
168 =cut
169  
170 sub delete_field_from_file {
171 my $self = shift;
172 my %args = @_;
173 say STDERR whoami() if $self->{verbose} > 2;
174  
175 my $filename = $args{filename};
176 my $field = $args{field};
177  
178 if (-w $filename)
179 {
180 if ($field eq 'url')
181 {
182 delfattr($filename, 'dublincore.source');
183 }
184 elsif ($field eq 'title')
185 {
186 delfattr($filename, 'dublincore.title');
187 }
188 elsif ($field eq 'alt_title')
189 {
190 delfattr($filename, 'dublincore.alternative');
191 }
192 elsif ($field eq 'creator')
193 {
194 delfattr($filename, 'dublincore.creator');
195 }
196 elsif ($field eq 'description')
197 {
198 delfattr($filename, 'dublincore.description');
199 }
200 else
201 {
202 delfattr($filename, $field);
203 }
204 }
205 } # delete_field_from_file
206  
207 =head1 Helper Functions
208  
209 Private interface.
210  
211 =cut
212  
213 =head2 replace_one_field
214  
215 Overwrite the given field.
216 This does no checking for multi-value fields.
217  
218 $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value);
219  
220 =cut
221  
222 sub replace_one_field {
223 my $self = shift;
224 my %args = @_;
225 say STDERR whoami() if $self->{verbose} > 2;
226  
227 my $filename = $args{filename};
228 my $field = $args{field};
229 my $value = $args{value};
230 say STDERR "field=$field value=$value" if $self->{verbose} > 2;
231  
232 if (-w $filename)
233 {
234 if ($field eq 'url')
235 {
236 setfattr($filename, 'dublincore.source', $value);
237 }
238 elsif ($field eq 'title')
239 {
240 setfattr($filename, 'dublincore.title', $value);
241 }
242 elsif ($field eq 'alt_title')
243 {
244 setfattr($filename, 'dublincore.alternative', $value);
245 }
246 elsif ($field eq 'creator')
247 {
248 setfattr($filename, 'dublincore.creator', $value);
249 }
250 elsif ($field eq 'description')
251 {
252 setfattr($filename, 'dublincore.description', $value);
253 }
254 else
255 {
256 if (ref $value eq 'ARRAY')
257 {
258 $value = join(',', @{$value});
259 }
260 setfattr($filename, $field, $value);
261 }
262 }
263 } # replace_one_field
264  
265 =head1 BUGS
266  
267 Please report any bugs or feature requests to the author.
268  
269 =cut
270  
271 1; # End of File::Sticker::Scribe
272 __END__
« Sticker/Scribe/Mp3.pm ↑ Summary Sticker/Scribe/Yaml.pm »