line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SPOPS::Import; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Import.pm,v 3.8 2004/06/02 00:48:21 lachoy Exp $ |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
25
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
147
|
|
6
|
4
|
|
|
4
|
|
17
|
use base qw( Class::Accessor Class::Factory ); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
4421
|
|
7
|
4
|
|
|
4
|
|
39061
|
use SPOPS::Exception qw( spops_error ); |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
37
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$SPOPS::Import::VERSION = sprintf("%d.%02d", q$Revision: 3.8 $ =~ /(\d+)\.(\d+)/); |
10
|
|
|
|
|
|
|
|
11
|
4
|
|
|
4
|
|
321
|
use constant AKEY => '_attrib'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
12928
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my @FIELDS = qw( object_class data extra_metadata DEBUG ); |
14
|
|
|
|
|
|
|
SPOPS::Import->mk_accessors( @FIELDS ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
0
|
|
|
0
|
|
|
my ( $pkg, $type, $params ) = @_; |
18
|
0
|
|
|
|
|
|
my $class = eval { $pkg->get_factory_class( $type ) }; |
|
0
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
spops_error $@ if ( $@ ); |
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
|
|
|
0
|
|
|
sub initialize { return $_[0] } |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
0
|
|
|
sub get_fields { return @FIELDS } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Class::Accessor stuff |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
0
|
|
|
sub get { return $_[0]->{ AKEY() }{ $_[1] } } |
34
|
0
|
|
|
0
|
|
|
sub set { return $_[0]->{ AKEY() }{ $_[1] } = $_[2] } |
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 = ; |
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__ |