New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Config.pm in branches/UKMO/dev_merge_2017_GC_couple_pkg/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Keyword – NEMO

source: branches/UKMO/dev_merge_2017_GC_couple_pkg/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Keyword/Config.pm @ 9677

Last change on this file since 9677 was 9677, checked in by dancopsey, 6 years ago

Strip out SVN keywords.

File size: 4.1 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) Crown copyright Met Office. All rights reserved.
3# For further details please refer to the file COPYRIGHT.txt
4# which you should have received as part of this distribution.
5# ------------------------------------------------------------------------------
6use strict;
7use warnings;
8
9package Fcm::Keyword::Config;
10
11use Carp;
12use Fcm::Keyword::Entries;
13use Fcm::Keyword::Exception;
14use Fcm::Util::ClassLoader;
15
16our %CONFIG_OF = (
17    LOCATION_ENTRIES => {
18        entry_class => 'Fcm::Keyword::Entry::Location',
19        loaders => [
20            {
21                class => 'Fcm::Keyword::Loader::Config::Location',
22            },
23        ],
24    },
25    REVISION_ENTRIES => {
26        entry_class => 'Fcm::Keyword::Entry',
27        loaders => [
28            {
29                class   => 'Fcm::Keyword::Loader::Config::Revision',
30                options => [{key => 'namespace', valuekey => 'key'}],
31            },
32            {
33                class   => 'Fcm::Keyword::Loader::VC::Revision',
34                options => [{key => 'source', valuekey => 'value'}],
35            },
36        ],
37    },
38);
39
40################################################################################
41# Returns a Fcm::Keyword::Entries object for given configuration
42sub get_entries {
43    my ($context, $args_ref) = @_;
44    if (!exists($CONFIG_OF{$context})) {
45        croak(Fcm::Keyword::Exception->new({message => sprintf(
46            "%s: keyword configuration not found", $context,
47        )}));
48    }
49    my $config_ref = $CONFIG_OF{$context};
50    my @loaders;
51    if (exists($config_ref->{loaders})) {
52        for my $loader_config (@{$config_ref->{loaders}}) {
53            my $class = $loader_config->{class};
54            Fcm::Util::ClassLoader::load($class);
55            my %options;
56            if (exists($loader_config->{options})) {
57                for my $option_ref (@{$loader_config->{options}}) {
58                    my $key = $option_ref->{key};
59                    my $value;
60                    if (exists($option_ref->{value})) {
61                        $value = $option_ref->{value};
62                    }
63                    elsif (
64                           exists($option_ref->{valuekey})
65                        && $args_ref
66                        && ref($args_ref) eq 'HASH'
67                        && exists($args_ref->{$option_ref->{valuekey}})
68                    ) {
69                        $value = $args_ref->{$option_ref->{valuekey}};
70                    }
71                    $options{$key} = $value;
72                }
73            }
74            push @loaders, $class->new(\%options);
75        }
76    }
77    my %entries_options = (
78        (@loaders ? (loaders => \@loaders) : ()),
79        (
80            exists($config_ref->{entry_class})
81            ? (entry_class => $config_ref->{entry_class})
82            : ()
83        ),
84    );
85    return Fcm::Keyword::Entries->new(\%entries_options);
86}
87
881;
89__END__
90
91=head1 NAME
92
93Fcm::Keyword::Config
94
95=head1 SYNOPSIS
96
97    use Fcm::Keyword::Config;
98
99=head1 DESCRIPTION
100
101This module stores the default configuration used by modules in the
102L<Fcm::Keyword> family.
103
104=head1 FUNCTIONS
105
106=over 4
107
108=item get_entries($context,$args_ref)
109
110Returns a L<Fcm::Keyword::Entries|Fcm::Keyword::Entries> object for a given
111$context. If there is no matching $context in the configuration, croak() with a
112L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>. $args_ref is an optional
113argument, which should be a reference to a hash containing a I<key> and a
114I<value> element. It can be used by this function to set up the constructor
115options in the loaders of the returned
116L<Fcm::Keyword::Entries|Fcm::Keyword::Entries> object.
117
118=back
119
120=head1 DIAGNOSTICS
121
122=head1 TO DO
123
124Allow configuration to be changed in runtime.
125
126Convert this module to OO?
127
128Separate configuration from logic if this module becomes any bigger.
129
130Unit tests.
131
132=head1 SEE ALSO
133
134L<Fcm::Keyword|Fcm::Keyword>,
135L<Fcm::Keyword::Entries|Fcm::Keyword::Entries>,
136L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>,
137L<Fcm::Keyword::Entry::Location|Fcm::Keyword::Entry::Location>
138
139=head1 COPYRIGHT
140
141E<169> Crown copyright Met Office. All rights reserved.
142
143=cut
Note: See TracBrowser for help on using the repository browser.