1
package GenTest::Properties;
8
use GenTest::Constants;
12
use constant PROPS_NAME => 0;
13
use constant PROPS_DEFAULTS => 1; ## Default values
14
use constant PROPS_OPTIONS => 2; ## Legal options to check for
15
use constant PROPS_HELP => 3; ## Help text
16
use constant PROPS_LEGAL => 4; ## List of legal properies
17
use constant PROPS_LEGAL_HASH => 5; ## Hash of legal propertis
18
use constant PROPS_PROPS => 6; ## the actual properties
24
my $name = our $AUTOLOAD;
27
return unless $name =~ /[^A-Z]/;
29
if (defined $self->[PROPS_LEGAL]) {
30
croak("Illegal property '$name' for ". $self->[PROPS_NAME])
31
if not $self->[PROPS_LEGAL_HASH]->{$name};
34
return $self->[PROPS_PROPS]->{$name};
40
my $props = $class->SUPER::new({
42
'defaults' => PROPS_DEFAULTS,
43
'options' => PROPS_OPTIONS,
44
'legal' => PROPS_LEGAL,
45
'help' => PROPS_HELP}, @_);
47
## List of legal properties, if no such list,
48
## all properties are legal
50
if (defined $props->[PROPS_LEGAL]) {
51
foreach my $legal (@{$props->[PROPS_LEGAL]}) {
52
$props->[PROPS_LEGAL_HASH]->{$legal}=1;
56
if (defined $props->[PROPS_OPTIONS]) {
57
foreach my $legal (keys %{$props->[PROPS_OPTIONS]}) {
58
$props->[PROPS_LEGAL_HASH]->{$legal}=1;
61
if (defined $props->[PROPS_DEFAULTS]) {
62
foreach my $legal (keys %{$props->[PROPS_DEFAULTS]}) {
63
$props->[PROPS_LEGAL_HASH]->{$legal}=1;
67
my $defaults = $props->[PROPS_DEFAULTS];
68
$defaults = {} if not defined $defaults;
70
my $from_cli = $props->[PROPS_OPTIONS];
71
$from_cli = {} if not defined $from_cli;
75
if ($from_cli->{config}) {
76
$from_file = _readProps($from_cli->{config},$props->[PROPS_LEGAL_HASH]);
79
$props->[PROPS_PROPS] = _mergeProps($defaults, $from_file);
80
$props->[PROPS_PROPS] = _mergeProps($props->[PROPS_PROPS], $from_cli);
86
my ($file,$legal) = @_;
87
open(PFILE, $file) or die "Unable read properties file '$file': $!";
88
read(PFILE, my $propfile, -s $file);
90
my $props = eval($propfile);
91
croak "Unable to load $file: $@" if $@;
93
foreach my $p (keys %$props) {
94
if (not $legal->{$p}) {
95
carp "'$p' is not a legal property";
100
croak "Illegal properties";
108
# First recursively deal with hashes
109
my $mergedHashes = {};
110
foreach my $h (keys %$a) {
111
if (UNIVERSAL::isa($a->{$h},"HASH")) {
112
if (defined $b->{$h}) {
113
$mergedHashes->{$h} = _mergeProps($a->{$h},$b->{$h});
118
my $result = {%$a, %$b};
119
$result = {%$result, %$mergedHashes};
125
_printProps($self->[PROPS_PROPS]);
129
my ($props,$indent) = @_;
130
$indent = 1 if not defined $indent;
131
my $x = join(" ", map {undef} (1..$indent*3));
132
foreach my $p (sort keys %$props) {
133
if (UNIVERSAL::isa($props->{$p},"HASH")) {
135
_printProps($props->{$p}, $indent+1);
136
} elsif (UNIVERSAL::isa($props->{$p},"ARRAY")) {
137
say ($x .$p." => ['".join("', '",@{$props->{$p}})."']");
139
say ($x.$p." => ".$props->{$p});
147
foreach my $key (keys %$props) {
148
$purged->{$key} = $props->{$key} if defined $props->{$key};
154
my ($props, @list) = @_;
155
foreach my $p (@list) {
156
croak "Required property '$p' not set" if not exists $props->{$p};
161
my ($self, $prefix, $options) = @_;
164
if (UNIVERSAL::isa($options,"HASH")) {
167
$hash = $self->$options;
170
return join(' ', map {$prefix.$_.(defined $hash->{$_}?
172
'':'='.$hash->{$_}):'')} keys %$hash);