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.
ConfigEntry.pm in vendors/lib/FCM/Context – NEMO

source: vendors/lib/FCM/Context/ConfigEntry.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 4.0 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
3#
4# This file is part of FCM, tools for managing and building source code.
5#
6# FCM is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# FCM is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with FCM. If not, see <http://www.gnu.org/licenses/>.
18# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22# ------------------------------------------------------------------------------
23package FCM::Context::ConfigEntry;
24use base qw{FCM::Class::HASH};
25
26use Text::ParseWords qw{shellwords};
27
28__PACKAGE__->class({
29    label       => '$',
30    modifier_of => '%',
31    ns_list     => '@',
32    stack       => '@',
33    value       => '$',
34});
35
36# A shorthand for shellwords($entry->get_value()).
37sub get_values {
38    shellwords($_[0]->get_value());
39}
40
41# The config entry's left hand side of the equal sign.
42sub get_lhs {
43    my ($self) = @_;
44    my $modifier = join(
45        q{, },
46        (   map
47            {   my $value = $self->{modifier_of}{$_};
48                join(q{:}, $_, (($value && $value eq 1) ? () : $value));
49            }
50            sort keys(%{$self->{modifier_of}})
51        ),
52    );
53    my $ns = join(
54        q{ },
55        (map {my $s = $_; $s =~ s{(["'\s])}{\\$1}gxms; $s} @{$self->{ns_list}}),
56    );
57    sprintf(
58        '%s%s%s',
59        $self->{label},
60        ($modifier ? "{$modifier}" : q{}),
61        ($ns ? "[$ns]" : q{}),
62    );
63}
64
65# The config entry, as a string.
66sub as_string {
67    my ($self, $in_fcm1) = @_;
68    my $value = $self->{value};
69    $value ||= q{};
70    $value =~ s{(\\)+(\$)}{$1$1\\$2}gxms;
71    sprintf(($in_fcm1 ? '%s %s' : '%s = %s'), $self->get_lhs(), $value);
72}
73
74# ------------------------------------------------------------------------------
75
761;
77__END__
78
79=head1 NAME
80
81FCM::Context::ConfigEntry;
82
83=head1 SYNOPSIS
84
85    my $c_entry = FCM::Context::ConfigEntry->new({
86        label       => 'egg',
87        modifier_of => {fried => 1},
88        ns_list     => [qw{all day breakfast}],
89        stack       => [[$breakfast_menu, 10], [$menu, 20]],
90        value       => 2,
91    });
92
93    # ... some time later
94    $label       = $c_entry->get_label();
95    %modifier_of = %{$c_entry->get_modifier_of()};
96    @ns_list     = @{$c_entry->get_ns_list()};
97    @stack       = @{$c_entry->get_stack()};
98    $value       = $c_entry->get_value();
99
100    print($c_entry->as_string());
101    # should print: egg{fried: 1}[all day breakfast] = 2
102
103=head1 DESCRIPTION
104
105This class is based on L<FCM::Class::HASH|FCM::Class::HASH> for representing an
106entry in a FCM configuration file. All attributes can be read using the
107$instance->get_$attrib() methods.
108
109=head1 ATTRIBUTES
110
111=over 4
112
113=item label
114
115The label of the entry.
116
117=item modifier_of
118
119A HASH containing the modifiers of this entry.
120
121=item ns_list
122
123An ARRAY containing the namespaces of this entry.
124
125=item stack
126
127An ARRAY containing the locator stack that provides this entry. The first
128element represents the top of the stack. Each element should be a reference to a
1292-element array [RESOURCE, LINE_NUMBER].
130
131=item value
132
133The value of this entry.
134
135=back
136
137=head1 METHODS
138
139=over 4
140
141=item $instance->as_string($in_fcm1)
142
143Returns a string representation of the config entry. If the optional argument
144$in_fcm1 is specified, it will return the config entry in FCM 1 format.
145
146=item $instance->get_lhs()
147
148Returns a string representation of the left hand side of the config entry.
149
150=item $instance->get_values()
151
152A shorthand for shellwords($instance->get_value()).
153
154=back
155
156=head1 COPYRIGHT
157
158(C) Crown copyright Met Office. All rights reserved.
159
160=cut
Note: See TracBrowser for help on using the repository browser.