blib/lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 179 CC 14 SCAR 27.8
CC
14
Cov
0%
new
56.0
_clean
20.0
_endpoint
6.0
CRAP 16.2
1 #-----------------------------------------------------------------
2 # MOBY::RDF::Ontologies::Cache::Cache
3 # Author: Edward Kawas <edward.kawas@gmail.com>,
4 #
5 # For copyright and disclaimer see below.
6 #
7 # $Id: CacheUtils.pm,v 1.3 2008/09/02 13:12:33 kawas Exp $
8 #-----------------------------------------------------------------
9 package MOBY::RDF::Ontologies::Cache::CacheUtils;
10  
11 #imports
12 use XML::LibXML;
13 use File::Spec;
14 use strict;
15 use DirHandle;
16  
17 # names of cache directories/files/locks
18 use constant LIST_FILE => '__L__I__S__T__';
19 use constant RDF_FILE => '__R__D__F__';
20 use constant UPDATE_FILE => '__U__P__D__A__T__E__F__I__L__E__';
21 use constant DATATYPES_CACHE => 'dataTypes';
22 use constant SERVICES_CACHE => 'services';
23 use constant NAMESPACES_CACHE => 'namespaces';
24 use constant SERVICETYPES_CACHE => 'serviceTypes';
25  
26 use vars qw /$VERSION/;
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
28  
29 =head1 NAME
30  
31 MOBY::RDF::Ontologies::Cache::CacheUtils - Utility module that aids in caching
32  
33 =head1 SYNOPSIS
34  
35 use MOBY::RDF::Ontologies::Cache::CacheUtils;
36 my $cachedir = "C:/tmp/";
37 my $url = "http://moby.ucalgary.ca/moby/MOBY-Central.pl";
38 my $uri = "http://moby.ucalgary.ca/MOBY/Central";
39  
40 my $x = MOBY::RDF::Ontologies::Cache::CacheUtils->new(
41  
42 endpoint => $url,
43 namespace => $uri,
44 cache => $cachedir,
45  
46 );
47  
48 # create the cache directory
49 $x->create_cache_dirs;
50  
51 # check if the cache exists
52 print "Cache exists!\n" if $x->cache_exists();
53 print "Cache doesnt exist!\n" unless $x->cache_exists();
54  
55 # get the cache dir
56 print "The cache dir is: " . $x->cachedir . "\n";
57  
58 # get the exact location of all cache dirs
59 my $dirs = $x->get_cache_dirs();
60 while ( ( $key, $value ) = each( %{$dirs} ) ) {
61 print "$key is stored in $value\n";
62 }
63  
64  
65 =head1 DESCRIPTION
66  
67 This module aids in the creation and maintainence of cache directories
68  
69 =cut
70  
71 =head1 AUTHORS
72  
73 Edward Kawas (edward.kawas [at] gmail [dot] com)
74  
75 =cut
76  
77 #-----------------------------------------------------------------
78  
79 =head1 SUBROUTINES
80  
81 =cut
82  
83 =head2 new
84  
85 Instantiate a CacheUtils object.
86  
87 Parameters:
88 * A Hash with keys:
89 -> endpoint => the BioMOBY registry endpoint to use <required>
90 -> namespace => the BioMOBY registry namespace to use <required>
91 -> cache => the directory to store the cache <required>
92  
93 If endpoint or cache are not specified, then new fails (dies).
94  
95 =cut
96  
97 sub new {
98 my ( $class, %args ) = @_;
99  
100 # create an object
101 my $self = bless {}, ref($class) || $class;
102  
103 # set various variables
104 $self->{endpoint} = $args{endpoint} if $args{endpoint};
105 $self->{namespace} = $args{namespace} if $args{namespace};
106 $self->{cachedir} = $args{cache} if $args{cache};
107  
108 # this variable isnt that important ...
109 $self->{namespace} = "http://moby.ucalgary.ca/MOBY/Central"
110 unless $self->{endpoint};
111  
112 # die if endpoint or cachedir is missing
113 die
114 "You neglected to specify a cache destination. Please specify a 'cache' location ...\n"
115 unless $self->{cachedir};
116 die
117 "You neglected to provide a BioMOBY registry endpoint. Please specify an 'endpoint'\n"
118 unless $self->{endpoint};
119  
120 # done
121 return $self;
122 }
123  
124 #-----------------------------------------------------------------
125 # _endpoint
126 # Return and/or set an endpoint of a given registry.
127  
128 #-----------------------------------------------------------------
129 sub _endpoint {
130 my ( $self, $registry ) = @_;
131  
132 # set the endpoint if necessary
133 $self->{endpoint} = $registry if $registry and $registry =~ m"^http://";
134  
135 # return the endpoint
136 return $self->{endpoint};
137 }
138  
139 #-----------------------------------------------------------------
140 # _namespace
141 # Return and/or set an namespace of a given registry.
142  
143 #-----------------------------------------------------------------
144 sub _namespace {
145 my ( $self, $registry ) = @_;
146  
147 # set the namespace if necessary
148 $self->{namespace} = $registry if $registry and $registry =~ m"^http://";
149  
150 # return the namespace
151 return $self->{namespace};
152 }
153  
154 #-----------------------------------------------------------------
155 # _clean
156 # Returns a string that has all of the non-digits and letters
157 # converted to a numerical ASCII representation. Used for creating a
158 # directory name from the registry URL.
159 #-----------------------------------------------------------------
160 sub _clean {
161 my $self = shift;
162 my $lastOneWasDigitalized = 0;
163  
164 if (@_) {
165 my $toBeCleaned = shift;
166 my $string = '';
167 my @array = split( //, $toBeCleaned );
168 foreach my $char (@array) {
169 if ( not $char =~ /[a-zA-z0-9]/ ) {
170 $string = $string
171 . ( ( $lastOneWasDigitalized == 1 ? "." : "" ) . ord($char) );
172 $lastOneWasDigitalized = 1;
173 } else {
174 $lastOneWasDigitalized = 0;
175 $string = $string . "$char";
176 }
177 }
178 return $string;
179 }
180 return '';
181 }
182  
183 #-----------------------------------------------------------------
184 # cache_exists
185 #-----------------------------------------------------------------
186  
187 =head2 cache_exists
188  
189 Return true if a local cache for the given registry exists (or
190 probably exists). An argument is a an endpoint of a
191 registry.
192  
193 =cut
194  
195 sub cache_exists {
196 my ( $self, $registry ) = @_;
197 my $pathToList =
198 File::Spec->catfile( $self->cachedir,
199 $self->_clean( $self->_endpoint($registry) ),
200 SERVICES_CACHE );
201 return -e $pathToList;
202 }
203  
204 #-----------------------------------------------------------------
205 # cache_exists
206 #-----------------------------------------------------------------
207  
208 =head2 cachedir
209  
210 Return the cache dir
211  
212 =cut
213  
214 sub cachedir {
215 my ( $self, $dir ) = @_;
216 $self->{cachedir} = $dir if $dir and -d $dir;
217 return $self->{cachedir};
218 }
219  
220 #-----------------------------------------------------------------
221 # create_cache_dirs
222 #-----------------------------------------------------------------
223  
224 =head2 create_cache_dirs
225  
226 Creates the cache directories needed for generating datatypes and services.
227  
228 Throws an exception if there are problems creating the directories.
229  
230 =cut
231  
232 sub create_cache_dirs {
233 my ($self) = @_;
234 my @dirs = (
235 File::Spec->catfile(
236 $self->cachedir,
237 $self->_clean( $self->_endpoint ),
238 DATATYPES_CACHE
239 ),
240 File::Spec->catdir(
241 $self->cachedir,
242 $self->_clean( $self->_endpoint ),
243 SERVICES_CACHE
244 ),
245 File::Spec->catdir(
246 $self->cachedir,
247 $self->_clean( $self->_endpoint ),
248 NAMESPACES_CACHE
249 ),
250 File::Spec->catdir(
251 $self->cachedir,
252 $self->_clean( $self->_endpoint ),
253 SERVICETYPES_CACHE
254 ),
255 );
256  
257 foreach my $file (@dirs) {
258 my ( $v, $d, $f ) = File::Spec->splitpath($file);
259 my $dir = File::Spec->catdir($v);
260 foreach my $part ( File::Spec->splitdir( ( $d . $f ) ) ) {
261 $dir = File::Spec->catdir( $dir, $part );
262 next if -d $dir or -e $dir;
263 mkdir($dir)
264 || die( "Error creating caching directory '" . $dir . "':\n$!" );
265 }
266 }
267 }
268  
269 #-----------------------------------------------------------------
270 # get_cache_dirs
271 #-----------------------------------------------------------------
272  
273 =head2 get_cache_dirs
274  
275 Gets the cache directories used for a specific cache as a hash.
276  
277 =cut
278  
279 sub get_cache_dirs {
280 my ($self) = @_;
281 return {
282 DATATYPES_CACHE => File::Spec->catfile(
283 $self->cachedir,
284 $self->_clean( $self->_endpoint ),
285 DATATYPES_CACHE
286 ),
287 SERVICES_CACHE => File::Spec->catdir(
288 $self->cachedir,
289 $self->_clean( $self->_endpoint ),
290 SERVICES_CACHE
291 ),
292 NAMESPACES_CACHE => File::Spec->catdir(
293 $self->cachedir,
294 $self->_clean( $self->_endpoint ),
295 NAMESPACES_CACHE
296 ),
297 SERVICETYPES_CACHE => File::Spec->catdir(
298 $self->cachedir,
299 $self->_clean( $self->_endpoint ),
300 SERVICETYPES_CACHE
301 ),
302 };
303  
304 }
305  
306  
307 sub plainfiles {
308 my ($self, $dir )= @_;
309 my $dh = DirHandle->new($dir) or die "can't opendir $dir: $!";
310 return sort # sort pathnames
311 grep { -f } # choose only "plain" files
312 map { "$dir/$_" } # create full paths
313 grep { !/^\./ } # filter out dot files
314 $dh->read(); # read all entries
315 }
316  
317  
318 1;
319 __END__