File: Configurable.pm

package info (click to toggle)
algotutor 0.8.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 580 kB
  • sloc: perl: 2,563; makefile: 41; php: 24; sh: 1
file content (72 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Author:  Chao-Kuei Hung
# For more info, including license, please see doc/index.html

package Configurable;
# Configurable objects

use strict;
use Carp;
use vars qw(@ISA);
@ISA = qw();

require "basic.pl";

sub new {
    my ($class, %opts) = @_;
    $class = ref($class) if ref($class);
    return bless ::deep_copy(\%opts), $class;
}

my (@cfg_file_list, $init);
@cfg_file_list = ("$ENV{HOME}/.algotutor", "/etc/algotutor");
foreach $init (@cfg_file_list) {
    if (-r $init) {
	$::UserConfig = require $init;
	print STDERR "using config file $init\n";
	last;
    }
}

sub cget {
    my ($self, $opt) = @_;
    my ($CLASS) = $self;
    if (ref $self) {
	return $self->{$opt} if exists $self->{$opt};
	$CLASS = ref $self;
    }
    return $::UserConfig->{$CLASS}{$opt}
	if exists $::UserConfig->{$CLASS}{$opt};
    while (defined $CLASS) {
#print "C($opt):$CLASS -- ", join(",",keys %{ $::Config->{$CLASS} }), "\n";
	return $::Config->{$CLASS}{$opt}
	    if defined $::Config->{$CLASS}{$opt};
	$CLASS = ::parent_class($CLASS);
    }
}

sub get_all_opts {
    my ($self) = @_;
    my ($CLASS) = $self;
    $CLASS = ref $self if ref $self;
    my (@ANCESTORS) = ();
    while ($CLASS) {
	push @ANCESTORS, $CLASS;
	$CLASS = ::parent_class($CLASS);
    }
    my (%opts) = ();
    while (@ANCESTORS) {
	%opts = (%opts, %{ ::deep_copy($::Config->{pop @ANCESTORS}) } );
    }
    if (ref $self) {
	my (@k) = grep { /^-\w+$/ } keys %$self;
	my ($highest) = { map { $_=>$self->{$_} } @k };
	%opts = (%opts, %{ ::deep_copy($highest) });
    }
    return %opts;
}

$::Config->{Configurable} = {
};

1;