TVDB: better handling of first run
[nonametv.git] / lib / NonameTV / Factory.pm
blobcd2edcb5e1ded15b26ce14537b9f98d4aac769a9
1 package NonameTV::Factory;
3 use strict;
5 =head1 NAME
7 NonameTV::Factory
9 =head1 DESCRIPTION
11 Create other NonameTV objects based on the available configuration.
13 =head1 METHODS
15 =over 4
17 =cut
19 BEGIN {
20 use Exporter ();
21 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 @ISA = qw(Exporter);
24 @EXPORT = qw( );
25 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
26 @EXPORT_OK = qw/ CreateAugmenter
27 CreateDataStore CreateDataStoreDummy
28 CreateImporter CreateExporter CreateFileStore
29 InitHttpCache
32 our @EXPORT_OK;
34 use NonameTV::Config qw/ReadConfig/;
36 =item CreateAugmenter( $name, $ds )
38 Create an augmenter from the configuration in $conf->{Augmenter}->{$name}
39 and associate it with the NonameTV::DataStore in $ds.
41 Returns the newly created augmenter or dies if creation fails.
43 =cut
45 sub CreateAugmenter {
46 my( $name, $ds ) = @_;
48 my $conf = ReadConfig();
50 if( not exists( $conf->{Augmenters}->{$name} ) ) {
51 print STDERR "No such augmenter $name\n";
52 exit 1;
55 my $aug_data = $conf->{Augmenters}->{$name};
56 $aug_data->{ConfigName} = $name;
58 my $aug_type = $aug_data->{Type};
60 if( not defined $aug_type ) {
61 print STDERR "Augmenter $name has no Type-field\n";
62 exit 1;
65 my $aug = eval "use NonameTV::Augmenter::$aug_type;
66 NonameTV::Augmenter::${aug_type}->new( \$aug_data, \$ds );"
67 or die $@;
68 return $aug;
71 =item CreateImporter( $name, $ds )
73 Create an importer from the configuration in $conf->{Importer}->{$name}
74 and associate it with the NonameTV::DataStore in $ds.
76 Returns the newly created importer or dies if creation fails.
78 =cut
80 sub CreateImporter {
81 my( $name, $ds ) = @_;
83 my $conf = ReadConfig();
85 if( not exists( $conf->{Importers}->{$name} ) ) {
86 print STDERR "No such importer $name\n";
87 exit 1;
90 my $imp_data = $conf->{Importers}->{$name};
91 $imp_data->{ConfigName} = $name;
93 my $imp_type = $imp_data->{Type};
95 if( not defined $imp_type ) {
96 print STDERR "Importer $name has no Type-field\n";
97 exit 1;
100 my $imp = eval "use NonameTV::Importer::$imp_type;
101 NonameTV::Importer::${imp_type}->new( \$imp_data, \$ds );"
102 or die $@;
103 return $imp;
106 =item CreateExporter( $name, $ds )
108 Create an exporter from the configuration in $conf->{Exporter}->{$name}
109 and associate it with the NonameTV::DataStore in $ds.
111 Returns the newly created exporter or dies if creation fails.
113 =cut
115 sub CreateExporter {
116 my( $name, $ds ) = @_;
118 my $conf = ReadConfig();
120 if( not exists( $conf->{Exporters}->{$name} ) ) {
121 print STDERR "No such exporter $name\n";
122 exit 1;
125 my $exp_data = $conf->{Exporters}->{$name};
126 my $exp_type = $exp_data->{Type};
128 my $exp = eval "use NonameTV::Exporter::$exp_type;
129 NonameTV::Exporter::${exp_type}->new( \$exp_data, \$ds );"
130 or die $@;
131 return $exp;
134 =item CreateFileStore( $importername )
136 Create a NonameTV::FileStore object that matches the configuration
137 for an importer.
139 Returns the newly created filestore or dies if creation fails.
141 =cut
143 sub CreateFileStore {
144 my( $importername ) = @_;
146 require NonameTV::FileStore;
148 my $conf = ReadConfig();
150 if( not exists( $conf->{Importers}->{$importername} ) ) {
151 print STDERR "No such importer $importername\n";
152 exit 1;
155 my $path;
157 if( exists( $conf->{Importers}->{$importername}->{FileStore} ) ) {
158 $path = $conf->{Importers}->{$importername}->{FileStore};
160 elsif( exists( $conf->{FileStore} ) ) {
161 $path = $conf->{FileStore};
163 else {
164 print STDERR "No FileStore found in configuration for " .
165 "importer $importername.";
166 exit 1;
169 return NonameTV::FileStore->new( { Path => $path } );
172 =item CreateDataStore
174 Create a NonameTV::DataStore from the configuration.
176 Returns the newly created datastore or dies if creation fails.
177 If CreateDataStore is called more than once, the same DataStore object
178 will be returned avery time.
180 =cut
182 my $ds;
184 sub CreateDataStore {
185 return $ds if defined $ds;
187 my $conf = ReadConfig();
189 require NonameTV::DataStore;
190 $ds = NonameTV::DataStore->new( $conf->{DataStore} );
192 return $ds;
195 =item CreateDataStoreDummy
197 Create a dummy datastore (see NonameTV::DataStore::Dummy) from the
198 configuration.
200 Returns the newly created datastore or dies if creation fails.
202 =cut
204 sub CreateDataStoreDummy {
205 my $conf = ReadConfig();
207 require NonameTV::DataStore::Dummy;
208 return NonameTV::DataStore::Dummy->new( $conf->{DataStore} );
211 =item InitHttpCache
213 Initialize the HTTP::Cache::Transparent module from the configuration.
215 =cut
217 sub InitHttpCache {
218 require HTTP::Cache::Transparent;
219 my $conf = ReadConfig();
220 HTTP::Cache::Transparent::init( $conf->{Cache} );