r54326 - in /trunk/dh-make-perl: lib/Debian/Control/Stanza.pm lib/Debian/Control/Stanza/CommaSeparated.pm t/CommaSeparated.t

dmn at users.alioth.debian.org dmn at users.alioth.debian.org
Sun Mar 14 17:31:17 UTC 2010


Author: dmn
Date: Sun Mar 14 17:31:07 2010
New Revision: 54326

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=54326
Log:
abstract comma-separated lists for Debian::Control

Added:
    trunk/dh-make-perl/lib/Debian/Control/Stanza/CommaSeparated.pm
    trunk/dh-make-perl/t/CommaSeparated.t
Modified:
    trunk/dh-make-perl/lib/Debian/Control/Stanza.pm

Modified: trunk/dh-make-perl/lib/Debian/Control/Stanza.pm
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/dh-make-perl/lib/Debian/Control/Stanza.pm?rev=54326&op=diff
==============================================================================
--- trunk/dh-make-perl/lib/Debian/Control/Stanza.pm (original)
+++ trunk/dh-make-perl/lib/Debian/Control/Stanza.pm Sun Mar 14 17:31:07 2010
@@ -27,6 +27,7 @@
 use base qw( Class::Accessor Tie::IxHash );
 
 use Carp qw(croak);
+use Debian::Control::Stanza::CommaSeparated;
 use Debian::Dependencies;
 
 =head1 FIELDS
@@ -90,11 +91,16 @@
     }
 
     # initialize any dependency lists with empty placeholders
+    # same for comma-separated lists
     for( $self->fields ) {
-        if( $self->is_dependency_list($_) and not $self->$_ ) {
+        if ( $self->is_dependency_list($_) and not $self->$_ ) {
             $self->$_( Debian::Dependencies->new );
         }
+        elsif ( $self->is_comma_separated($_) and not $self->$_ ) {
+            $self->$_( Debian::Control::Stanza::CommaSeparated->new );
+        }
     }
+
 
     return $self;
 }
@@ -218,6 +224,9 @@
     $value = Debian::Dependencies->new($value)
         if not ref($value) and $self->is_dependency_list($field);
 
+    $value = Debian::Control::Stanza::CommaList->new($value)
+        if not ref($value) and $self->is_comma_separated($value);
+
     return ( tied %$self )->STORE( $field,  $value );
 }
 
@@ -243,6 +252,7 @@
     while( my($k,$v) = each %$self ) {
         next unless defined($v);
         next if $self->is_dependency_list($k) and "$v" eq "";
+        next if $self->is_comma_separated($k) and "$v" eq "";
 
         my $line = "$k: $v";
 

Added: trunk/dh-make-perl/lib/Debian/Control/Stanza/CommaSeparated.pm
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/dh-make-perl/lib/Debian/Control/Stanza/CommaSeparated.pm?rev=54326&op=file
==============================================================================
--- trunk/dh-make-perl/lib/Debian/Control/Stanza/CommaSeparated.pm (added)
+++ trunk/dh-make-perl/lib/Debian/Control/Stanza/CommaSeparated.pm Sun Mar 14 17:31:07 2010
@@ -1,0 +1,116 @@
+package Debian::Control::Stanza::CommaSeparated;
+
+=head1 NAME
+
+Debian::Control::Stanza::CommaSeparated - comma separated debian/control field abstraction
+
+=cut
+
+use strict;
+use warnings;
+
+use Array::Unique;
+use Text::ParseWords qw(quotewords);
+
+use overload '""' => \&as_string;
+
+=head1 SYNOPSYS
+
+    my $f = Debian::Control::Stanza::CommaSeparated->new(
+        'Joe M <joem at there.not>');
+    $f->add('"Smith, Agent" <asmith at hasyou.not>, Joe M <joem at there.not>');
+    print $f->as_string;
+        # 'Joe M <joem at there.not>, "Smith, Agent" <asmith at hasyou.not>'
+    print "$f";     # the same
+    $f->sort;
+
+=head1 DESCRIPTION
+
+Debian::Control::Stanza::CommaSeparated abstracts handling of comma-separated
+list of values, often found in F<debian/control> file fields like I<Uploaders>.
+Note that the various dependency fields in F<debian/control> also use
+comma-separated values, but the L<Debian::Dependencies> class is more suitable
+for these as it is for example also capable of finding overlapping dependency
+declarations.
+
+=head1 CONSTRUCTOR
+
+=over
+
+=item new (initial values)
+
+The initial values list is parsed and may contain strings that are in fact
+comma-separated lists. These are split appropriately using L<Text::ParseWords>'
+I<quotewords> routine.
+
+=back
+
+=cut
+
+sub new {
+    my $self = bless [], shift;
+
+    tie @$self, 'Array::Unique';
+
+    $self->add(@_) if @_;
+
+    $self;
+}
+
+=head1 METHODS
+
+=over
+
+=item as_string
+
+Returns text representation of the list. A simple join of the elements by C<, >.
+
+The same function is used for overloading the stringification operation.
+
+=cut
+
+sub as_string
+{
+    return join( ', ', @{ $_[0] } );
+}
+
+sub _parse {
+    my $self = shift;
+
+    my @output;
+
+    for (@_) {
+        my @items = quotewords( qr/\s*,\s*/, 1, $_ );
+        push @output, @items;
+    }
+
+    return @output;
+}
+
+=item add I<@items>
+
+Adds the ginen items to the list. Items that are already present are not added,
+keeping the list unique.
+
+=cut
+
+sub add {
+    my ( $self, @items ) = @_;
+
+    push @$self, $self->_parse(@items);
+}
+
+=item sort
+
+A handy method for sorting the list.
+
+=cut
+
+sub sort {
+    my $self = shift;
+
+    @$self = sort @$self;
+}
+
+
+1;

Added: trunk/dh-make-perl/t/CommaSeparated.t
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/dh-make-perl/t/CommaSeparated.t?rev=54326&op=file
==============================================================================
--- trunk/dh-make-perl/t/CommaSeparated.t (added)
+++ trunk/dh-make-perl/t/CommaSeparated.t Sun Mar 14 17:31:07 2010
@@ -1,0 +1,44 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+use Test::Exception;
+use Test::Differences;
+
+use Debian::Control::Stanza::CommaSeparated;
+
+my $s = Debian::Control::Stanza::CommaSeparated->new('foo bar, "one, two", three');
+
+is_deeply(
+    [@$s],
+    [ 'foo bar', '"one, two"', 'three' ],
+    'constructor parses ok'
+);
+
+$s->add("three");
+
+is( scalar(@$s), 3, 'ignores duplicates' );
+
+$s->add('"smith, agent" <asmith at hasyou.yes>, five');
+
+is( scalar(@$s), 5, 'add splits correctly' );
+
+is( $s->[3], '"smith, agent" <asmith at hasyou.yes>', 'add honours quotes' );
+is( $s->[4], 'five', 'fifth is five' );
+
+$s->sort;
+
+is_deeply(
+    [@$s],
+    [   '"one, two"', '"smith, agent" <asmith at hasyou.yes>',
+        'five', 'foo bar', 'three',
+    ],
+    'sort works'
+);
+
+is( "$s",
+    '"one, two", "smith, agent" <asmith at hasyou.yes>, five, foo bar, three',
+    "stringification works"
+);




More information about the Pkg-perl-cvs-commits mailing list