| 1 | package SPOPS::Import; | ||||||||||
| 2 | |||||||||||
| 3 | # $Id: Import.pm,v 3.8 2004/06/02 00:48:21 lachoy Exp $ | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use base qw( Class::Accessor Class::Factory ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use SPOPS::Exception qw( spops_error ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | $SPOPS::Import::VERSION = sprintf("%d.%02d", q$Revision: 3.8 $ =~ /(\d+)\.(\d+)/); | ||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use constant AKEY => '_attrib'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | my @FIELDS = qw( object_class data extra_metadata DEBUG ); | ||||||||||
| 14 | SPOPS::Import->mk_accessors( @FIELDS ); | ||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
| |||||||||||
| 17 | 0 | my ( $pkg, $type, $params ) = @_; | |||||||||
| 18 | 0 | my $class = eval { $pkg->get_factory_class( $type ) }; | |||||||||
| 19 | 0 | ▶ | spops_error $@ if ( $@ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | my $self = bless( {}, $class );; | |||||||||
| 21 | 0 | foreach my $field ( $self->get_fields ) { | |||||||||
| 22 | 0 | $self->$field( $params->{ $field } ); | |||||||||
| 23 | } | ||||||||||
| 24 | 0 | return $self->initialize( $params ); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub initialize { return $_[0] } | ||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub get_fields { return @FIELDS } | ||||||||
|
Subroutine
get_fields: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | |||||||||||
| 31 | # Class::Accessor stuff | ||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub get { return $_[0]->{ AKEY() }{ $_[1] } } | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||
| 34 | 0 | ▶ | sub set { return $_[0]->{ AKEY() }{ $_[1] } = $_[2] } | ||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | |||||||||||
| 36 | sub run { | ||||||||||
| 37 | my $class = ref $_[0] || $_[0]; | ||||||||||
| 38 | spops_error "SPOPS::Import subclass [$class] must implement run()"; | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | ######################################## | ||||||||||
| 43 | # I/O | ||||||||||
| 44 | ######################################## | ||||||||||
| 45 | |||||||||||
| 46 | # Read import data from a file; the first item is metadata, the | ||||||||||
| 47 | # remaining ones are data. Subclasses should override and call | ||||||||||
| 48 | |||||||||||
| 49 | sub raw_data_from_file { | ||||||||||
| 50 | my ( $class, $filename ) = @_; | ||||||||||
| 51 | my $raw_data = $class->read_perl_file( $filename ); | ||||||||||
| 52 | unless ( ref $raw_data eq 'ARRAY' ) { | ||||||||||
| 53 | spops_error "Raw data must be in arrayref format."; | ||||||||||
| 54 | } | ||||||||||
| 55 | return $raw_data; | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | sub raw_data_from_fh { | ||||||||||
| 60 | my ( $class, $fh ) = @_; | ||||||||||
| 61 | no strict 'vars'; | ||||||||||
| 62 | my $raw = $class->read_fh( $fh ); | ||||||||||
| 63 | my $data = eval $raw; | ||||||||||
| 64 | if ( $@ ) { | ||||||||||
| 65 | spops_error "Cannot parse data from filehandle: [$@]"; | ||||||||||
| 66 | } | ||||||||||
| 67 | unless ( ref $data eq 'ARRAY' ) { | ||||||||||
| 68 | spops_error "Data must be in arrayref format"; | ||||||||||
| 69 | } | ||||||||||
| 70 | return $data; | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | # Read in a file and evaluate it as perl. | ||||||||||
| 75 | |||||||||||
| 76 | sub read_perl_file { | ||||||||||
| 77 | my ( $class, $filename ) = @_; | ||||||||||
| 78 | no strict 'vars'; | ||||||||||
| 79 | my $raw = $class->read_file( $filename ); | ||||||||||
| 80 | my $data = eval $raw; | ||||||||||
| 81 | if ( $@ ) { | ||||||||||
| 82 | spops_error "Cannot parse data file ($filename): $@"; | ||||||||||
| 83 | } | ||||||||||
| 84 | return $data; | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | # Read in a file and return the contents | ||||||||||
| 89 | |||||||||||
| 90 | sub read_file { | ||||||||||
| 91 | my ( $class, $filename ) = @_; | ||||||||||
| 92 | |||||||||||
| 93 | unless ( -f $filename ) { | ||||||||||
| 94 | spops_error "Cannot read: [$filename] does not exist"; | ||||||||||
| 95 | } | ||||||||||
| 96 | open( DF, $filename ) || | ||||||||||
| 97 | spops_error "Cannot read data file: $!"; | ||||||||||
| 98 | local $/ = undef; | ||||||||||
| 99 | my $raw = <DF>; | ||||||||||
| 100 | close( DF ); | ||||||||||
| 101 | return $raw; | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | sub read_fh { | ||||||||||
| 106 | my ( $class, $fh ) = @_; | ||||||||||
| 107 | local $/ = undef; | ||||||||||
| 108 | my $raw = <$fh>; | ||||||||||
| 109 | return $raw; | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | ############################## | ||||||||||
| 114 | # INITIALIZE | ||||||||||
| 115 | |||||||||||
| 116 | __PACKAGE__->register_factory_type( object => 'SPOPS::Import::Object' ); | ||||||||||
| 117 | __PACKAGE__->register_factory_type( dbdata => 'SPOPS::Import::DBI::Data' ); | ||||||||||
| 118 | __PACKAGE__->register_factory_type( dbupdate => 'SPOPS::Import::DBI::Update' ); | ||||||||||
| 119 | __PACKAGE__->register_factory_type( dbdelete => 'SPOPS::Import::DBI::Delete' ); | ||||||||||
| 120 | __PACKAGE__->register_factory_type( table => 'SPOPS::Import::DBI::Table' ); | ||||||||||
| 121 | 1; | ||||||||||
| 122 | |||||||||||
| 123 | __END__ | ||||||||||