IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 13459


Ignore:
Timestamp:
May 21, 2007, 6:22:00 PM (19 years ago)
Author:
Paul Price
Message:

Pulling out the URI module, since it's darn annoying.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-Config/lib/PS/IPP/Config.pm

    r13317 r13459  
    11# Copyright (c) 2006  Paul Price, Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.45 2007-05-10 02:35:36 price Exp $
     3# $Id: Config.pm,v 1.46 2007-05-22 04:22:00 price Exp $
    44
    55package PS::IPP::Config;
     
    1515use PS::IPP::Metadata::Config 1.00;
    1616use Getopt::Long 2.35 qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV
    17 use URI 1.35;
    1817
    1918use base qw( Class::Accessor::Fast Exporter );
     
    253252    my $name = shift;           # File name to check
    254253
    255     my $uri = URI->new( $name );# URI parser
    256     my $scheme = $uri->scheme();# Scheme for file name
     254    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    257255
    258256    return $name unless defined $scheme; # Probably a file name instead of a URI
    259257
    260258    if ($scheme eq 'neb') {
    261         $name = _strip_scheme( $name );
     259        $name =~ s|^(\S+):/*||;
    262260        $self->_neb_start();
    263261        my $neb = $self->{nebulous}; # Nebulous handle
     
    279277    $self->file_prepare( $name );
    280278
    281     my $uri = URI->new( $name );# URI parser
    282     my $scheme = $uri->scheme();# Scheme for file name
     279    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    283280    if (defined $scheme) {
    284281        if ($scheme eq 'neb') {
    285             $name = _strip_scheme( $name );
     282            $name =~ s|^(\S+):/*||;
    286283            $self->_neb_start();
    287284            return $self->{nebulous}->open_create( $name );
     
    313310    $self->file_prepare( $name );
    314311
    315     my $uri = URI->new( $name );# URI parser
    316     my $scheme = $uri->scheme();# Scheme for file name
     312    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    317313    if (defined $scheme and $scheme eq 'neb') {
    318         $name = _strip_scheme( $name );
     314        $name =~ s|^(\S+):/*||;
    319315        $self->_neb_start();
    320316        $name = $self->{nebulous}->create( $name );
     
    330326    my $name = shift;           # File name to check
    331327
    332     my $uri = URI->new( $name );# URI parser
    333     my $scheme = $uri->scheme();# Scheme for file name
     328    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    334329    if (defined $scheme and $scheme eq 'neb') {
    335         $name = _strip_scheme( $name );
     330        $name =~ s|^(\S+):/*||;
    336331        $self->_neb_start();
    337332        return (defined $self->{nebulous}->find_instances( $name ) ? 1 : 0);
     
    350345    $self->file_prepare( $target );
    351346
    352     my $uri = URI->new( $target );# URI parser
    353     my $scheme = $uri->scheme();# Scheme for file name
     347    my ($scheme) = $target =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    354348    if (defined $scheme and $scheme eq 'neb') {
    355         $target = _strip_scheme( $target );
     349        $target =~ s|^(\S+):/*||;
    356350        $self->_neb_start();
    357351        $target = $self->{nebulous}->create( $target );
     
    361355
    362356    system "cp $source $target";
    363 }
    364 
    365 # Strip the scheme (e.g., "neb://") off a URI name
    366 sub _strip_scheme
    367 {
    368     my $name = shift;           # File name of interest
    369     $name =~ s|^\S+:/*||;
    370     return $name;
    371357}
    372358
     
    388374    my $template = shift;       # Template filename from which to get working directory if
    389375
    390     my $resolved = $name;       # Resolved version of file name
    391376    if (defined $workdir) {
    392         $resolved = caturi( $workdir, $name );
     377        $name = caturi( $workdir, $name );
    393378    } elsif (defined $template) {
    394379        # Take directory from template and apply to name
    395380        my $dir = _strip_filename( $template );
    396         $resolved = caturi( $dir, $name );
    397     }
    398 
    399     my $uri = URI->new( $name ); # URI parser
    400     my $scheme = $uri->scheme(); # Scheme for file name
     381        $name = caturi( $dir, $name );
     382    }
     383
     384    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file://, path://
    401385    return $name if defined $scheme and $scheme eq 'neb'; # Nothing to be done: Nebulous handles it all
    402386
    403387    # Might need to create a directory
    404     $resolved = $self->convert_filename_absolute( $resolved );
     388    my $resolved = $self->convert_filename_absolute( $name );
    405389    my ( $vol, $dirs, $file ) = File::Spec->splitpath( $resolved );
    406390    system "mkdir -p $dirs";
     
    421405    }
    422406
    423     my $uri = URI->new($name) or die "Unable to parse URI: $name\n"; # URI
    424     my $scheme = $uri->scheme(); # Scheme, e.g., file, path
    425     if (lc($scheme) eq 'file') {
    426         my $host = $uri->host();
    427         my $path;
    428         if (defined $host) {
    429             # The user has file://some/directory/ but *probably* wants
    430             # /some/directory/ i.e., the URI is technically wrong
    431             # (should be one or three leading slashes, not two).  We
    432             # choose to fix it and give the user what we think they
    433             # want, rather than what they asked for.
    434             $path = File::Spec->catfile( undef, $host, $uri->path() );
    435         } else {
    436             $path = $uri->path();
    437         }
    438 
    439         return File::Spec->canonpath( $path );
    440     }
     407    $name =~ s|/$||;
     408    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file, path
     409    $name =~ s|^\S+:/*||;
     410    return $name if lc($scheme) eq 'file';
    441411
    442412    if (lc($scheme) eq 'path') {
    443         my $authority = $uri->authority();
    444         my $path = $self->datapath( $authority );
    445         my @segments = $uri->path_segments();
    446         return File::Spec->catfile( $path, @segments );
     413        my @dirs = split('/', $name);
     414        my $pathName = shift @dirs;
     415        my $path = $self->datapath( $pathName );
     416        $path =~ s|/*$||;
     417        return File::Spec->catfile( $path, @dirs );
    447418    }
    448419
     
    455426    my $self = shift;           # Configuration object
    456427    my $name = shift;           # raw name
    457 
     428   
    458429    unless (defined $self and defined $name) {
    459430        carp "Programming error";
    460431        exit($PS_EXIT_PROG_ERROR);
    461432    }
    462 
     433   
    463434    # First, check to see if it's already in a relative form
    464     my $scheme = URI->new( $name )->scheme();
    465     if ($scheme eq 'path' or $scheme eq 'file') {
    466         # We may as well search for a 'better' path
    467         $name = $self->convert_filename_absolute( $name );
    468     } elsif ($scheme eq 'neb') {
    469         # No chance of changing anything --- move along
    470         return $name;
    471     }
    472 
     435    my ($scheme) = $name =~ m|^(\S+):|; # The scheme, e.g., file, path
     436    if (defined $scheme) {
     437        if ($scheme eq 'path' or $scheme eq 'file') {
     438            # We may as well search for a 'better' path
     439            $name = $self->convert_filename_absolute( $name );
     440        } elsif ($scheme eq 'neb') {
     441            # No chance of changing anything --- move along
     442            return $name;
     443        }
     444    }
     445   
    473446    $name = File::Spec->canonpath( $name); # Clean up
    474     my ($vol, $dir, $file) = File::Spec->splitpath( $name );
    475     my @dirs = File::Spec->splitdir( $dir );
    476     pop @dirs;                  # Get rid of filename
    477 
     447    my @dirs = File::Spec->splitdir( $name );
     448   
    478449    my $path_list = metadataLookupMD($self->{_ipprc}, 'DATAPATH'); # List of paths
    479450    my $best_path;
     
    483454      my $path_name = $path_item->{name}; # Name of the path
    484455      my $path = File::Spec->canonpath( $path_item->{value} ); # The path
     456      $path =~ s|/*$||;
    485457      my @path_dirs = File::Spec->splitdir( $path );
    486 
     458     
    487459      # Check if the path is suitable
    488460      next if scalar @path_dirs > scalar @dirs;
     
    498470      }
    499471  }
    500     my $uri = URI->new();       # URI to return
     472   
     473    $name =~ s|^/||;
     474    $name =~ s|/$||;
    501475    if (defined $best_score) {
    502         my $rel = File::Spec->abs2rel( $name, $best_path );
    503         my @segments = File::Spec->splitdir( $rel );
    504         $uri->scheme( 'path' );
    505         $uri->authority( $best_name );
    506         $uri->path_segments( @segments );
    507     } else {
    508         $uri->scheme( 'file' );
    509         $uri->path( $name );
    510     }
    511 
    512     return $uri->as_string();
     476        $best_path =~ s|^/||;
     477        $best_path =~ s|/$||;
     478        $name =~ s|^$best_path|$best_name|;
     479        return 'path://' . $name;
     480    }
     481    return 'file://' . $name;
    513482}
    514483
Note: See TracChangeset for help on using the changeset viewer.