| 1 | package Cikl::Smrt::FeedParserConfig; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Config::Simple; | ||
| 7 | use Try::Tiny; | ||
| 8 | use Storable qw/dclone/; | ||
| 9 | |||
| 10 | use constant FIELDS => { | ||
| 11 | group => 'everyone', | ||
| 12 | parser => undef, | ||
| 13 | detecttime_format => undef, | ||
| 14 | detecttime_zone => undef, | ||
| 15 | feed => undef, | ||
| 16 | source => undef, | ||
| 17 | feed_limit => undef, | ||
| 18 | feed_user => undef, | ||
| 19 | feed_password => undef, | ||
| 20 | values => undef, | ||
| 21 | skipfirst => undef, | ||
| 22 | delimiter => undef, | ||
| 23 | fields => undef, | ||
| 24 | fields_map => undef, | ||
| 25 | mirror => undef, | ||
| 26 | zip_filename => undef, | ||
| 27 | regex => undef, | ||
| 28 | regex_values => undef, | ||
| 29 | node => undef, | ||
| 30 | node_xpath => undef, | ||
| 31 | subnode => undef, | ||
| 32 | period => undef, | ||
| 33 | disabled => undef, | ||
| 34 | elements => undef, | ||
| 35 | elements_map => undef, | ||
| 36 | attributes => undef, | ||
| 37 | attributes_map => undef | ||
| 38 | }; | ||
| 39 | |||
| 40 | use constant REQUIRED_FIELDS => { | ||
| 41 | group => 1, | ||
| 42 | feed => 1, | ||
| 43 | source => 1, | ||
| 44 | parser => 1 | ||
| 45 | }; | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $class = shift; | ||
| 49 | my $config_file = shift; | ||
| 50 | my $feed_name = shift; | ||
| 51 | |||
| 52 | my $config; | ||
| 53 | my $err; | ||
| 54 | try { | ||
| 55 | $config = Config::Simple->new($config_file); | ||
| 56 | } catch { | ||
| 57 | $err = shift; | ||
| 58 | }; | ||
| 59 | |||
| 60 | if (defined($err)) { | ||
| 61 | die "Syntax error while parsing $config_file: $err"; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $config_data = $config->param(-block => 'default'); | ||
| 65 | my $feed_rules = $config->param(-block => $feed_name); | ||
| 66 | |||
| 67 | if (!%$feed_rules) { | ||
| 68 | die "Unknown section '$feed_name' for $config_file"; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Override any configuration with sub config. | ||
| 72 | map { $config_data->{$_} = $feed_rules->{$_} } keys (%$feed_rules); | ||
| 73 | |||
| 74 | my $self = {}; | ||
| 75 | |||
| 76 | my @feed_config_fields = keys(FIELDS); | ||
| 77 | |||
| 78 | # Catch any dynamically named fields. | ||
| 79 | foreach my $name (keys(%$config_data)) { | ||
| 80 | if ($name =~ /^(regex_|xpath)/) { | ||
| 81 | push(@feed_config_fields, $name); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | foreach my $name (@feed_config_fields) { | ||
| 86 | my $v = delete($config_data->{$name}) || FIELDS->{$name}; | ||
| 87 | |||
| 88 | if (defined($v)) { | ||
| 89 | $self->{$name} = $v; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | foreach my $required (keys(REQUIRED_FIELDS)) { | ||
| 95 | if (!exists($self->{$required})) { | ||
| 96 | die "Missing required configuration '$required' from '$config_file' - [$feed_name]"; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | bless($self,$class); | ||
| 101 | |||
| 102 | $config_data->{group} = $self->{group}; | ||
| 103 | $config_data->{source} = $self->{source}; | ||
| 104 | # The remaining fields will act as defaults for events, when they are created. | ||
| 105 | $self->{event_fields} = $config_data; | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub feed { | ||
| 111 | my $self = shift; | ||
| 112 | return $self->{feed}; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub parser { | ||
| 116 | my $self = shift; | ||
| 117 | return $self->{parser}; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub default_event_data { | ||
| 121 | my $self = shift; | ||
| 122 | return (dclone($self->{event_fields})); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub values { | ||
| 126 | my $self = shift; | ||
| 127 | if (!defined($self->{values})) { | ||
| 128 | return undef; | ||
| 129 | } | ||
| 130 | return split(',',$self->{values}); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub fields { | ||
| 134 | my $self = shift; | ||
| 135 | return split(',',$self->{fields}); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub fields_map { | ||
| 139 | my $self = shift; | ||
| 140 | return split(',',$self->{fields_map}); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _split { | ||
| 144 | my $self = shift; | ||
| 145 | my $field = shift; | ||
| 146 | if (defined($self->{$field})) { | ||
| 147 | return split(',',$self->{$field}); | ||
| 148 | } | ||
| 149 | return(undef); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub elements { | ||
| 153 | my $self = shift; | ||
| 154 | return $self->_split("elements"); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub elements_map { | ||
| 158 | my $self = shift; | ||
| 159 | return $self->_split("elements_map"); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub attributes { | ||
| 163 | my $self = shift; | ||
| 164 | return $self->_split("attributes"); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub attributes_map { | ||
| 168 | my $self = shift; | ||
| 169 | return $self->_split("attributes_map"); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub regex_values { | ||
| 173 | my $self = shift; | ||
| 174 | if (ref($self->{'regex_values'}) eq 'ARRAY') { | ||
| 175 | return $self->{'regex_values'}; | ||
| 176 | } | ||
| 177 | return split(',',$self->{'regex_values'}); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub regex_for { | ||
| 181 | my $self = shift; | ||
| 182 | my $name = shift; | ||
| 183 | return $self->{"regex_$name"}; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub keyed_regex { | ||
| 187 | my $self = shift; | ||
| 188 | my $key = shift; | ||
| 189 | return $self->{"regex_" . $key}; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub keyed_regex_values { | ||
| 193 | my $self = shift; | ||
| 194 | my $key = shift; | ||
| 195 | my $v = $self->{"regex_" . $key . "_values"}; | ||
| 196 | return split(',', $v); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub delimiter { return($_[0]->{delimiter}); } | ||
| 200 | sub feed_limit { return($_[0]->{feed_limit}); } | ||
| 201 | sub skipfirst { return($_[0]->{skipfirst}); } | ||
| 202 | sub regex { return($_[0]->{regex}); } | ||
| 203 | sub node { return($_[0]->{node}); } | ||
| 204 | sub subnode { return($_[0]->{subnode}); } | ||
| 205 | |||
| 206 | 1; |