IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 9, 2008, 1:32:35 PM (18 years ago)
Author:
jhoblitt
Message:

create a Nebulous::Keys object to represent a parsed neb key and impliment other changes to abstract out the handling of keys in order to support "soft" volume requests and the "any" volume

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/lib/Nebulous/Keys.pm

    r17072 r18451  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: Keys.pm,v 1.1 2008-03-20 21:13:28 jhoblitt Exp $
     3# $Id: Keys.pm,v 1.2 2008-07-09 23:32:35 jhoblitt Exp $
    44
    55package Nebulous::Keys;
     
    1010our $VERSION = '0.01';
    1111
    12 use base qw( Exporter );
    13 
    14 use File::Spec::Functions;
     12use base qw( Exporter Class::Accessor::Fast );
     13
     14use File::Spec;
    1515use URI::file;
    1616use URI;
    17 
    18 our @EXPORT_OK      = qw( parse_neb_key );
     17use overload '""' => \&_stringify_key;
     18
     19our @EXPORT_OK = qw(
     20    parse_neb_key
     21    parse_neb_volume
     22);
     23
     24__PACKAGE__->mk_ro_accessors(qw( path volume soft_volume ));
    1925
    2026sub parse_neb_key
    2127{
    22     my $text = shift;
    23     return unless defined $text;
     28    my ($key, $volume) = @_;
     29    return unless defined $key;
    2430
    2531    # white space is not allowed
    26     if ($text =~ qr/\s+/) {
     32    if ($key =~ qr/\s+/) {
    2733        die "keys and URIs may not contain whitespace";
    2834    }
    2935
    30     my $uri = URI->new($text);
     36    my $uri = URI->new($key);
    3137    my $scheme = $uri->scheme;
    3238    my $path = $uri->opaque;
    3339   
    34     my $volume;
     40    my $volume_name;
     41    my $soft_volume;
    3542    # if this is a valid uri
    3643    if (defined $scheme) {
     
    4754        # strip off volume component if it exists
    4855        $path =~ s|^//([^/]+)||;
    49         $volume = $1;
     56       
     57        # ignore key supplied volume name if one is passed as a parameter
     58        $volume ||= $1;
     59        my $volume_info = parse_neb_volume($volume);
     60
     61        # magically eat the "any" volume
     62        if (defined $volume_info->{volume} and $volume_info->{volume} ne 'any') {
     63            $volume_name = $volume_info->{volume};
     64            $soft_volume = $volume_info->{soft_volume};
     65        }
    5066
    5167        # require a leading slash if there is no volume name
    52         if ((not defined $volume) and (not $path =~ m|^/|)) {
     68        if ((not defined $volume_name) and (not $path =~ m|^/|)) {
    5369            die "neb URI scheme requires a leading slash, eg. neb:/";
    5470        }
     71    } else {
     72        my $volume_info = parse_neb_volume($volume);
     73
     74        # magically eat the "any" volume
     75        if (defined $volume_info->{volume} and $volume_info->{volume} ne 'any') {
     76            $volume_name = $volume_info->{volume};
     77            $soft_volume = $volume_info->{soft_volume};
     78        }
    5579    }
    5680
     
    5983
    6084    # remove multiple /'s and trailing slashes
    61     $path = canonpath($path);
    62 
    63     return ($volume, $path);
     85    $path = File::Spec->canonpath($path);
     86
     87    return __PACKAGE__->new({
     88        volume      => $volume_name,
     89        soft_volume => $soft_volume,
     90        path        => $path,
     91    });
     92}
     93
     94sub parse_neb_volume
     95{
     96    my $volume = shift;
     97    return unless defined $volume;
     98
     99    my $soft_volume;
     100    # check to see if there is a tilde and remove it if found
     101    if (defined $volume and $volume =~ s/^~//) {
     102        $soft_volume = 1;
     103    }
     104
     105    return({ volume => $volume, soft_volume => $soft_volume });
     106}
     107
     108sub _stringify_key
     109{
     110    my $self = shift;
     111
     112    my $path        = $self->path;
     113    my $volume      = $self->volume || "";
     114    my $soft_volume = $self->soft_volume ? '~' : "";
     115
     116    return "neb://${soft_volume}${volume}/$path";
    64117}
    65118
Note: See TracChangeset for help on using the changeset viewer.