source: main/trunk/package-kits/linux/perllib/Greenstone/Config.pm@ 29546

Last change on this file since 29546 was 29546, checked in by Jeremy Symon, 9 years ago

Arch package now automatically sets the owner of the web directory to the tomcat user. The functionality is also implemented for the other package managers, but not yet tested.

File size: 1.6 KB
RevLine 
[29536]1package Greenstone::Config;
2
3use strict;
4use warnings;
5use utf8;
[29546]6use Greenstone::Helpers;
[29536]7
8# Formats an array
9sub replacement_array {
10 die "Array handling must be defined by implementation";
11}
12
13# Determines the value to replace a variable with
14sub replacement {
15 my ($self, $var) = @_;
16 my $val = $self->{config}->{$var};
17 die "'$var' was not defined!" unless (defined $val);
18 if (ref $val eq 'ARRAY') {
19 return $self->replacement_array ($val);
20 } else {
21 return $val;
22 }
23}
24
25# Substitutes variables with their value
26sub subst {
27 my $self = shift;
28 for (@_) {
29 s/@([\w]+)@/$self->replacement($1)/ge;
30 }
31 return @_;
32}
33
[29546]34# Reads a config file into the config hashmap
35sub readconf {
36 my ($self, $conf) = @_;
37 $self->{config} = {} unless exists $self->{config};
38 open CONF, '<', $conf
39 or die "Failed to open '$conf' for reading: $!";
40 my $var;
41 while (my $line = <CONF>) {
42 if (empty $line or comment $line) {
43 $var = undef;
44 } elsif (defined $var and $line =~ /^\s/) {
45 trim $line;
46 $self->subst ($line);
47 escape $line;
48 push @{$self->{config}->{$var}}, $line;
49 } else {
50 ($var, my $val) = trim (split ":", $line, 2);
51 defined $var and defined $val or die "Invalid variable assignment: '$line'";
52 if (empty $val) {
53 $self->{config}->{$var} = [];
54 } else {
55 $self->subst ($val);
56 escape $val;
57 $self->{config}->{$var} = $val;
58 }
59 }
60 }
61 close CONF;
62}
63
[29536]641;
Note: See TracBrowser for help on using the repository browser.