File: Config.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (1079 lines) | stat: -rw-r--r-- 34,257 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
package Demeter::Config;

=for Copyright
 .
 Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
 All rights reserved.
 .
 This file is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself. See The Perl
 Artistic License.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

use autodie qw(open close);

#use MooseX::Singleton;
use Moose;
extends 'Demeter';
use Moose::Util::TypeConstraints;
use MooseX::Aliases;
#use vars qw($singleton);	# Moose 0.61, MooseX::Singleton 0.12 seem to need this

use Carp;
#use diagnostics;
use Demeter::Constants qw($EPSILON2);
use Demeter::StrTypes qw(FileName);
use Demeter::IniReader;
use Config::INI::Writer;
use Cwd qw(abs_path);
use File::Basename;
use File::Spec;
use JSON qw(decode_json);
use Regexp::Assemble;
#use Demeter::Constants qw($NUMBER);
use Scalar::Util qw(looks_like_number);
use Text::Wrap;

## these do not get inherited as this is imported as compile time, but
## attributes get made at run time.  sigh...
has 'group'     => (is => 'rw', isa => 'Str',  default => sub{shift->_get_group()});
has 'name'      => (is => 'rw', isa => 'Str',  default => q{});
has 'plottable' => (is => 'ro', isa => 'Bool', default => 0);
has 'data'      => (is => 'rw', isa => 'Any',  default => q{});
has 'perl_base' => (is => 'rw', isa => 'Str',  default => q{});

has 'config_file' => (is => 'ro', isa => FileName,
		      default => File::Spec->catfile(dirname($INC{"Demeter.pm"}),
						     "Demeter",
						     "configuration",
						     "config.demeter_conf"));
has 'config_json' => (is => 'ro', isa => FileName,
		      default => File::Spec->catfile(dirname($INC{"Demeter.pm"}),
						     "Demeter",
						     "configuration",
						     "demeter_config.db"));

has 'all_config_files' => (
			     traits    => ['Array'],
			     is        => 'rw',
			     isa       => 'ArrayRef[Str]',
			     default   => sub { [] },
			     handles   => {
					   'push_all_config_files'  => 'push',
					   'pop_all_config_files'   => 'pop',
					   'clear_all_config_files' => 'clear',
					  },
			    );

has 'main_groups' => (
		      traits    => ['Array'],
		      is        => 'rw',
		      isa       => 'ArrayRef[Str]',
		      default   => sub { [] },
		      handles   => {
				    'push_main_groups'  => 'push',
				    'pop_main_groups'   => 'pop',
				    'clear_main_groups' => 'clear',
				   },
		     );

has 'is_configured' => (is => 'rw', isa => 'Bool', default => 0);

my $where = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? "APPDATA" : "HOME";
my $stem  = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? "demeter" : ".horae";
#my $where = ($Demeter::mode->is_windows) ? "USERPROFILE" : "HOME";
our $fname = (defined($ENV{DEMETER_MODE}) and ($ENV{DEMETER_MODE} eq 'web')) ? q{} : File::Spec->catfile($ENV{$where}, $stem, "demeter.ini");
has 'ini_file' => (is => 'ro', isa => FileName, default => $fname);


my %ini;
#tie %ini, 'Config::IniFiles', ();
our %params_of;

#my $one_has_been_created = 0;

sub BUILD {
  my ($self) = @_;
  #return $Demeter::mode->config if $Demeter::mode->config;

  ## we are assuming that gnuplot has been placed in a specific
  ## location relative to the perl executable:
  ##   $^X ($EXECUTABLE_NAME) = C:\strawberry\perl\bin\perl.exe
  ##   gnuplot location =  C:\strawberry\c\bin\gnuplot\bin\perl.exe
  ##   thus perl_base is C:\strawberry, see line 268
  if ($self->is_windows) {
    my ($volume,$directories,$fname) = File::Spec->splitpath( $^X );
    $directories =~ s{\A[/\\]}{}; # remove leading and training slashes from $directories, this
    $directories =~ s{[/\\]\z}{}; # lets the pop pop line below work as expected
    my @dir = File::Spec->splitdir( $directories );
    pop @dir; pop @dir;  # perl\bin
    $self->perl_base(File::Spec->catdir($volume, @dir));
  };

  $self -> dot_folder;
  if (-e $self->config_json) {
    my $rlist = decode_json(Demeter->slurp($self->config_json));
    %params_of = %$rlist;
    foreach my $key (keys %params_of) {
      my $k = lc $key;
      my ($g, $p) = split(/:/, $k);
      $ini{$g}{$p} = $params_of{lc $key}->{default} if $p;
    };

  } else {
    $self -> read_config;
  };
  $self -> read_ini;
  $self -> fix;
  $self -> write_ini;
  $self -> mo -> config($self);
  $self -> mo -> merge($self->default("merge", "weightby"));
  my @groups = $self->groups;
  $self->main_groups(\@groups);
  return $self;
};

sub set {
  my ($self, @list) = @_;
  my %hash = @list;
  foreach my $key (keys %hash) {
    my $k = lc $key;
    $params_of{$k} = $hash{$k};
    my ($g, $p) = split(/:/, $k);
    $ini{$g}{$p} = $params_of{lc $key}->{default} if $p;
  };
  return $self;
};
sub Push {
  my ($self, @list) = @_;
  my %hash = @list;
  my $retval = 0;
  foreach my $key (keys %hash) {
    my $k = lc $key;
    push @{ $params_of{$k} }, $hash{$k};
    $retval = $#{ $params_of{$k} };
  };
  return $retval;
};
sub get {
  my $self = shift;
  croak(ref($self) . ': usage: get($key) or get(@keys)') if @_ < 1;
  my @values = ();
  foreach my $key (@_) {
    my $k = lc $key;
    push @values, $params_of{lc $key} || 0;
  };
  return wantarray ? @values : $values[0];
};

sub groups {
  my ($self) = @_;
  #my @list = @{ $self->get('___groups') };
  my @list = keys(%ini);
  return sort @list;
};

sub parameters {
  my ($self, $group) = @_;
  my $hashref = $ini{$group};
  return sort keys %$hashref;
};

=for LiteratureReference (read_config)
  Before land was and sea -- before air and sky
  Arched over all, all Nature was Chaos.
  ...
  The God or Nature calmed the elements:
  Land fell away from sky and sea from land,
  And aether drew away from cloud and rain.
                      Ovid,
                      Metamorphoses, Book I

=cut

sub read_config {
  my ($self, $file) = @_;
  $self->_read_config_file($file||$self->config_file);
  $self->is_configured(1);
  return $self;
};
sub _read_config_file {
  my ($self, $file) = @_;

  ## need to distinguish between a relative filename and a fully
  ## resolved file.

  #carp("The Demeter configuration file ($file) does not exist\n\n"),
    return 0
    if (not -e $file);
  #carp("The Demeter configuration file ($file) cannot be opened\n\n"),
    return 0
    if (not -r $file);

  my $base = (split(/\./, basename($file)))[0];
  $self -> Push(___groups => $base);
  $self -> push_all_config_files(File::Spec->rel2abs($file));

  my $key_regex = Regexp::Assemble->new()->add(qw(type default minint maxint options windows
						  units onvalue offvalue restart))->re;


  my (%hash, $description, $group, $param, $value);
  my $line;
  open (my $CONFIG, $file);
 CONF: while (my $line = <$CONFIG>) {
    next CONF if ($line =~ m{^\s*\#});
    next CONF if ($line =~ m{^\s*$});

    chomp $line;
    $line =~ s{\s+$}{};
    if ($line =~ m{^\s*include\s*(.+)}) {
      ## handle include files by recursion
      (my $includefile = $1) =~ s{\s+(?:\#.*)?$}{};
      if (not -e $includefile) { # this allows for arbitrary include files
	$includefile = File::Spec->catfile(dirname($file), $includefile);
      };
      $self->_read_config_file($includefile);

    } elsif ($line =~ m{^\s*section\s*=\s*(\w+)}) {
      $group = $1;
      $group =~ s{\s+$}{};
      #$self -> Push(___groups => $group);
      $description = q{};

    } elsif ($line =~ m{^\s*section_description}) {
    SECDESC: while (my $next = <$CONFIG>) {
	$next =~ s{\n}{ };
	$next =~ s{^\s+}{};
	last SECDESC if ($next =~ m{^\s*$});
	$description .= $next;
      };
      $description =~ s{\s+$}{};
      $self -> set($group=>$description);

    } elsif ($line =~ m{^\s*description}) {
    DESC: while (my $next = <$CONFIG>) {
	$next =~ s{\n}{ };
	$next =~ s{^\s+}{};
	last DESC if ($next =~ m{^\s*$});
	if ($next =~ m{^\.}) {
	  $next =~ s{\.\s+}{\%list};
	  $next =~ s{\s}{\%space}g;
	  $next .= '%endlist';
	};
	$description .= $next;
      };
      $description =~ s{\s+$}{};
      $hash{description} = $description;
      $self->set_this_param($group, $param, %hash);
      next CONF;

    } elsif ($line =~ m{^\s*variable\s*=\s*(\w+)}) {
      $param = $1;
      $param =~ s{\s+$}{};
      undef %hash;
      $ini{$group}{$param} = q{};
      $description = q{};

    } elsif ($line =~ m{^\s*($key_regex)\s*=\s*(.+)}) {
      my $key = $1;
      $value = $2;
      $value =~ s{\s+$}{};
      if (($value =~ m{__METIS_BASE__}) and ($INC{"Demeter/UI/Metis.pm"})) {
	my $new = dirname($INC{"Demeter/UI/Metis.pm"});
	$value =~ s{__METIS_BASE__}{$new};
	$value = abs_path(File::Spec->canonpath($value));
      }
      $hash{$key} = $value;
      ($hash{demeter} = $value) if ($key eq 'default');
      if (($self->is_windows) and ($key eq 'windows')) {
	my $relocated = $self->perl_base;       # make paths to executables (feff, gnuplot, etc)
	$value =~ s{__PERL_BASE__}{$relocated}; # tolerant to relocation upon installation
	$hash{default} = $value;
	$hash{demeter} = $value;
	$hash{windows} = $value;
      };
    };
  };
  $self->set_this_param($group, $param, %hash) if %hash;
  close $CONFIG;
  return $self;
};

sub set_this_param {
  my ($self, $group, $param, %hash) = @_;
  my $key = join(":", $group, $param);
  #local $| = 1;
  #print $key, $/;
  $hash{default}     ||= 0;	# sanitize several attributes
  $hash{was}         ||= 0;
  $hash{description} ||= q{};
  $hash{restart}     ||= 0;
  if ($hash{type} eq 'positive integer') {
    $hash{maxint} ||= 1e9;
    $hash{minint} ||= 0;
  } elsif ($hash{type} eq 'boolean') {
    $hash{onvalue}  ||= 1;
    $hash{offvalue} ||= 0;
  };
  if ($hash{type} eq 'real') {
    #$self->_report($group, $param, $hash{default});
    $hash{default} = (looks_like_number($hash{default})) ? $hash{default} : 0;
    $hash{demeter} = $hash{default};
    if (exists $hash{windows}) {
      $hash{windows} = (looks_like_number($hash{windows})) ? $hash{windows} : 0;
    };
  } elsif ($hash{type} eq 'positive integer') {
    $hash{default} = (looks_like_number($hash{default})) ? int($hash{default}) : 0;
    $hash{demeter} = $hash{default};
    if (exists $hash{windows}) {
      $hash{windows} = (looks_like_number($hash{windows})) ? int($hash{windows}) : 0;
    };
  };
  $self -> set($key=>\%hash);
  $ini{$group}{$param} = (($self->is_windows) and (exists $hash{windows})) ? $hash{windows} : $hash{default};
  return $self;
};

sub _report {
  my ($self, $g, $p, $val) = @_;
  Demeter->pjoin($g,$p,$val,looks_like_number($val), '<');
};

sub fix_number {
  my ($val) = @_;
  $val =~ s{,}{.} if ($val =~ m{,});
  $val .= '0' if ($val =~ m{\.\z});
  $val =~ s{\s+}{} if ($val =~ m{\s+});
  return $val;
};

## override a default value, for instance when reading an ini file

## need to take care that a key from an ini file is actually from the configuration files
sub set_default {
  my ($self, $group, $param, $value) = @_;
  return q{} if not $param;
  my $key = join(":", $group, $param);
  #local $| = 1;
  #print $key, $/;
  my $rhash = $self->get($key);
  if (not $rhash) {
    my $conffile = File::Spec->catfile(dirname($INC{'Demeter.pm'}), 'Demeter', 'Plugins', $group.'.demeter_conf');
    if (-e ($conffile)) {
      $self->read_config($conffile);
      $rhash = $self->get($key);
    };
    return $self if (not $rhash);
  };
  $rhash->{was} = $rhash->{default};
  $rhash->{default} = $value;
  if ($rhash->{type} eq 'boolean') {
    $rhash->{default} = ($self->is_true($value)) ? "true" : "false";
  } elsif ($rhash->{type} eq 'real') {
    #$rhash->{default} = 0 if (not looks_like_number($value));
    #$rhash->{default} = fix_number($value);
    $rhash->{default} = (looks_like_number($value)) ? $value : 0;
  } elsif ($rhash->{type} eq 'positive integer') {
    $rhash->{default} = 0 if (not looks_like_number($value));
    $rhash->{default} = int($value);
    ($rhash->{default} = $rhash->{maxint}) if ($value > $rhash->{maxint});
    ($rhash->{default} = $rhash->{minint}) if ($value < $rhash->{minint});
  };
  $self -> set($key=>$rhash);
  return $self;
};
# sub is_true {
#   my ($self, $value) = @_;
#   return 1 if ($value =~ m{^[ty]}i);
#   return 0 if ($value =~ m{^[fn]}i);
#   return 0 if (($value =~ m{$NUMBER}) and ($value == 0));
#   return 1 if ($value =~ m{$NUMBER});
#   return 0;
# };

sub new_params {
  my ($self, $rhash) = @_;
  $self->set($rhash);
  return 1;
};

sub default {
  my ($self, $group, $param) = @_;
  return q{} if not $param;
  my $key = join(":", $group, $param);
  my $rhash = $self->get($key);
  carp("$key is not a valid configuration parameter\n\n"), return 0 if not $rhash;
  if ($rhash->{type} eq 'boolean') {
    return $rhash->{onvalue}  || 1 if ($self->is_true($rhash->{default}));
    return $rhash->{offvalue} || 0;
  };
  if ($rhash->{type} eq 'positive integer') {
    return $rhash->{maxint} if ($rhash->{default} > $rhash->{maxint});
    return $rhash->{minint} if ($rhash->{default} < $rhash->{minint});
  };
  return $rhash->{default};
};
alias def => 'default';

sub description {
  my ($self, $group, $param) = @_;
  my $key = ($param) ? join(":", $group, $param) : $group;
  return $self->get($key) if (not $param);
  my $rhash = $self->get($key);
  carp("$key is not a valid configuration parameter\n\n"), return 0 if not $rhash;
  my $desc = $rhash->{description};
  if ($desc =~ m{\%list}) {
    $desc =~ s{\%list}{\n        }g;
    $desc =~ s{\%space}{ }g;
    $desc =~ s{\%endlist}{}g;
  #} else {
  #  $desc = wrap("    ", "    ", $desc) . "\n";
  };
  return $desc;
};
sub attribute {
  my ($self, $which, $group, $param) = @_;
  return q{} if not $param;
  my $key = join(":", $group, $param);
  my $rhash = $self->get($key);
  carp("$key is not a valid configuration parameter\n\n"), return 0 if not $rhash;
  return $rhash->{$which} || 0;
};
sub was      {my $self=shift; $self->attribute("was",      @_)};
sub Type     {my $self=shift; $self->attribute("type",     @_)};
sub units    {my $self=shift; $self->attribute("units",    @_)};
sub demeter  {my $self=shift; $self->attribute("demeter",  @_)};
sub options  {my $self=shift; $self->attribute("options",  @_)};
sub onvalue  {my $self=shift; $self->attribute("onvalue",  @_)};
sub offvalue {my $self=shift; $self->attribute("offvalue", @_)};
sub minint   {my $self=shift; $self->attribute("minint",   @_)};
sub maxint   {my $self=shift; $self->attribute("maxint",   @_)};
sub restart  {my $self=shift; $self->attribute("restart",  @_)};

sub set_options {
  my ($self, $group, $param, $value, $sort) = @_;
  my $string;
  if ($sort) {
    $string = join(" ", sort {$a cmp $b} @$value);
  } else {
    $string = join(" ", @$value);
  };
  $params_of{join(':',$group,$param)}->{options} = $string;
  return $self;
};

sub describe_param {
  my ($self, $group, $param, $width) = @_;
  my $config = $self->mo->config;
  my $text = q{};
  local $Text::Wrap::columns = $width || $config->default("operations", "config_text_width");
  local $Text::Wrap::huge = "overflow";
  if ($param) {
    my $key = join(":", $group, $param);
    my $rhash = $self->get($key);
    return q{} if not $rhash;
    $text .= "$group -> $param\n";
    my $desc = wrap("    \"", "     ", $rhash->{description}) . "\"\n";
    $desc =~ s{\%list}{\n        }g;
    $desc =~ s{\%space}{ }g;
    $desc =~ s{\%endlist}{}g;
    $text .= $desc;
    foreach my $k (qw(type default demeter options units minint maxint onvalue offvalue)) {
      $text .= sprintf("  %-8s : %s\n", $k, $rhash->{$k}) if defined $rhash->{$k};
    };
  } else {
    my $description = $self->get($group);
    return q{} if not $description;
    $text .= "$group:\n";
    $text .= wrap("    \"", "     ", $description) . "\"\n";
  };
  return $text;
};

sub write_ini {
  my ($self, $file) = @_;
  return $self if ($self->mo->ui eq 'web');
  $file ||= $self->ini_file;
  #my $ini_ref = tied %ini;
  #$ini_ref -> WriteConfig($file);
  Config::INI::Writer->write_file(\%ini, $file);
  return $self;
};

sub read_ini {
  my ($self, $group) = @_;
  return $self if ($self->mo->ui eq 'web');
  my $inifile = $self->ini_file;
  if (not -e $inifile) {
    $self->write_ini($inifile);
    return $self;
  };

  my $personal_ini = Demeter::IniReader->read_file($inifile);

  # my %personal_ini;
  # my $ini_ref = tied %ini;
  # tie %personal_ini, 'Config::IniFiles', (-file=>$inifile, -import=>$ini_ref );
  # {				# this is to encourage a spurious warning
  #   no strict qw{refs};		# related to Config::IniFiles to shut up
  #   my $toss = *{"Config::IniFiles::$inifile"}; # under windows
  #   undef $toss;
  # };
  foreach my $g (keys %$personal_ini) {
    #next if $g eq 'ini__filename';
    next if ($group and ($g ne $group));
    #print $g, $/;
    my $hash = $personal_ini->{$g};
    foreach my $p (keys %$hash) {
      ($p = 'col'.$1) if ($p =~ m{c(\d)}); # compatibility, convert cN -> colN
      #Demeter->pjoin($inifile, $g, $p, $personal_ini->{$g}{$p}) if ($g eq 'x15b');
      $self->set_default($g, $p, $personal_ini->{$g}{$p});
    };
  };
  #Demeter->trace;
  #Demeter->Dump($ini{x15b});
  return $self;
};


sub reset_all {
  my ($self) = @_;
  foreach my $g (Demeter->co->groups) {
    foreach my $p (Demeter->co->parameters($g)) {
      Demeter->co->set_default($g, $p, Demeter->co->demeter($g, $p));
    };
  };
  Demeter->co->write_ini;
  return $self;
};

## this method is used to fix parameters in a way that is backwards compatable
sub fix {
  my ($self) = @_;
  my $keyparams = $self->default("gnuplot", "keyparams");
  $keyparams =~ s{left|right|top|bottom|center}{}g;
  $keyparams =~ s{\A\s+}{};
  $self->set_default("gnuplot", "keyparams", $keyparams);

  ## somehow, it may happen that a users ini file will have
  ## zero-length plotting ranges -- very confusing, so reset to
  ## demeter defaults
  if (abs($self->default("plot", 'emax') - $self->default("plot", 'emin')) < $EPSILON2) {
    $self->set_default("plot", 'emin', $self->demeter("plot", 'emin'));
    $self->set_default("plot", 'emax', $self->demeter("plot", 'emax'));
  };
  if (abs($self->default("plot", 'kmax') - $self->default("plot", 'kmin')) < $EPSILON2) {
    $self->set_default("plot", 'kmin', $self->demeter("plot", 'kmin'));
    $self->set_default("plot", 'kmax', $self->demeter("plot", 'kmax'));
  };
  if (abs($self->default("plot", 'rmax') - $self->default("plot", 'rmin')) < $EPSILON2) {
    $self->set_default("plot", 'rmin', $self->demeter("plot", 'rmin'));
    $self->set_default("plot", 'rmax', $self->demeter("plot", 'rmax'));
  };
  if (abs($self->default("plot", 'qmax') - $self->default("plot", 'qmin')) < $EPSILON2) {
    $self->set_default("plot", 'qmin', $self->demeter("plot", 'qmin'));
    $self->set_default("plot", 'qmax', $self->demeter("plot", 'qmax'));
  };
  # foreach my $p (qw(kmax rmax qmax emin emax)) {
  #   my $val = $self->default("plot", $p);
  #   $self->set_default("plot", $p, $self->demeter("plot", $p));
  # };

  $self->set_default("athena", "autosave_frequency", $self->demeter("athena", "autosave_frequency"))
    if $self->default("athena", "autosave_frequency") < 2;

  if ($self->default('gnuplot', 'keyparams') =~ m{box(?!\slw)}) {
    my $old = $self->default('gnuplot', 'keyparams');
    $old =~ s{box}{box lw 1};
    $self->set_default('gnuplot', 'keyparams', $old);
  };
  if ($self->default('gnuplot', 'xkcd')) {
    $self->set_default('gnuplot', 'font', 'Humor Sans');
  };

  ## deal with new sphinx documentation (as of 0.9.25)
  if ($self->default('athena', 'doc_url') =~ m{aug}) {
    $self->set_default('athena', 'doc_url', q{http://bruceravel.github.io/demeter/documents/Athena/index.html});
  };
  if ($self->default('artemis', 'doc_url') =~ m{artug}) {
    $self->set_default('artemis', 'doc_url', q{http://bruceravel.github.io/demeter/documents/Artemis/index.html});
  };

  ## on windows, make sure gnuplot->program and feff->executable are sensible
  if ($self->is_windows) {
    if (not -x $self->default('gnuplot', 'program')) {
      my $distributed = File::Spec->catfile($ENV{DEMETER_BASE}, 'c', 'bin', 'gnuplot', 'bin', 'gnuplot.exe');
      $self->set_default('gnuplot', 'program', $distributed)
    };
    if (not -x $self->default('feff', 'executable')) {
      my $distributed = File::Spec->catfile($ENV{DEMETER_BASE}, 'c', 'bin', 'feff6.exe');
      $self->set_default('feff', 'executable', $distributed)
    };
  };
  return $self;
};


__PACKAGE__->meta->make_immutable;
1;

=head1 NAME

Demeter::Config - Demeter's configuration system

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 DESCRIPTION

This subclass of Demeter provides a general way of storing and
manipulating parameters of all sorts, including configuration
parameters for controlling the behavior of Demeter's various
subsystems, parameters using by Demeter-base applications, and
parameters that get passed to the templating subsystem.  The C<$C>
special templating variable accesses this object.

Demeter uses a two-tiered configuration system.  The system-wide tier
reads from a configuration file F<demeter.config> which lives in the
F<lib/> directory beneath the F<Demeter.pm> module.  This
configuration file contains complete information about each
configuration parameter, including description text and various
attributes.  The second tier is the user's initialization file.  This
uses the standard INI format and the L<Demeter::IniReader> module.  The
user's file lives in $ENV{HOME} on unix and Mac and in
$ENV{USERPROFILE} on Windows.

Defining new parameters to be handled by Config is as simple as
entering them into the INI file using normal INI syntax.  Those
user-created parameters will not possess descriptions or other
attributes, however.

=head1 METHODS

The normal idiom for accessing method of the Config class is to chain
method calls starting with any other Demeter object.  Although you can
certainly store a reference to the Config object as a scalar, it
usually is not necessary to do so.

The C<co> method, inhereted by all other objects from the Demeter
base class, returns a reference to the Config object.  All of the
examples below use the chain idiom and C<self> can be any kind of
object.

=head2 External methods

=over 4

=item C<default>

Return the default value for a parameter.

  print "Default fft kmin is ", $object->co->default("fft", "kmin"), $/;

=item C<set_default>

This method is called repeatedly by C<read_ini> to move the
information from the user's ini file into the Config object.

  $object -> co -> set_default("bkg", "rbkg", 1.2);

=item C<describe>

Return a text description of a parameter.

  print $object -> co -> describe("bkg", "kw");
   == prints ==>
    bkg -> kw
        "The default value for the k-weighting used to fit the background spline."
      type     : positive integer
      default  : 2
      demeter  : 2
      minint   : 0
      maxint   : 3

There is a third argument that controls the width of the description
text, the default being 90 (as set by the
C<operations--E<gt>config_text_width> parameter).

  print $object -> co -> describe("bkg", "kw", 45);
   == prints ==>
    bkg -> kw
        "The default value for the k-weighting
         used to fit the background spline."
      type     : positive integer
      default  : 2
      demeter  : 2
      minint   : 0
      maxint   : 3

=item C<groups>

Return a list of known configuration groups.

  @groups = $object -> co -> groups;

=item C<parameters>

Return a list of parameters associated with a specified configuration group.

  @params = $object -> co -> parameters('bkg');

=back

=head2 ini file methods

=over 4

=item C<read_ini>

Read default values from a user's ini file, overwriting the values
read from the system-wide configuration file.

  $object -> co -> read_ini($filename);

=item C<write_ini>

Write an ini file for a user.

  $object -> co -> write_ini($filename);

If the filename is not given, the user's ini file will be written.

=back

=head2 Internal methods

=over 4

=item C<is_configured>

This method returns true if a Config object has been created and
initialized by the C<read_config> method.

=item C<read_config>

This method reads the system-wide configuration file,
F<demeter.config>, from it location in the F<Demeter/lib/> folder just
below the main F<Demeter.pm> module.  This loads the entire contents,
including default values, parameter descriptions, and parameter
attributes, into the Config object.

=item C<set_this_param>

Use this method to update the default value for a parameter.  For
example, the C<read_ini> method calls this repeatedly.  Note that the
original value of each parameter from the F<demeter.config> file always
stays in the object as the "demeter" parameter attribute.

   $object->set_this_param($group, $param, %hash);

where %hash contains the various attributes decribed below.

=back

=head1 PARAMETER ATTRIBUTES

All parameters have these attributes.  Each attribute has an
associated convenience method of the same name.

=over 4

=item Type

This is one of string, regex, real, "positive integer", list, boolean,
color, or font.

  print $object -> co -> Type("fft", "kwindow")
    ==prints==>
      list

=item default

This is the value associated with the parameter.  This is overridden
by an imported INI file or by the C<set_default> method.

  print $object -> co -> default("fft", "kwindow")
    ==prints==>
      hanning

=item windows

This overrides the default, but only on Windows computers.

Strings that take a fully resolved path to an executable are treated
specially.  If that executable resides beneath the perl installation
location, then the installation location should be denoted as
C<__PERL_BASE__>.  For example, Strawberry perl is typically instaled
into C<C:\strawberry>.  Rather than denoting the location of the Feff
executable as C<C:\strawberry\c\bin\feff.exe>, it should be specified
as C<__PERL_BASE__\c\bin\feff.exe>.  The C<__PERL_BASE__> tag will be
replaced by the correct installation location.  Then, if Strawberry is
installed into some other location, the Feff executable will be found
at runtime.

=item demeter

This is the default value from the configuration file shipped with
demeter.  This is untouched even when a default is overridden by an
imported INI file.

  print $object -> co -> demeter("fft", "kwindow")
    ==prints==>
      hanning

=item was

This is the previous value of the parameter from before the last time
it was changed using the C<set_default> method.  If the parameter has
not been changed since it was initialized, this method returns 0.  The
primary use of this method is to aid in post-processing a change in
parameter value in a GUI or other application.

  print $object -> co -> demeter("fft", "kwindow")
    ==prints==>
      welch

=item description

This is a text string explaining the purpose of the parameter.

  print $object -> co -> description("fft", "kwindow")
    ==prints==>
      The default window type to use for the forward Fourier transform.

This can also return the description text of a parameter group.

  print $object -> co -> description("fft")
    ==prints==>
      These parameters determine how forward Fourier transforms are done by Demeter.

=back

Some attributes are specific to a parameter type.  Each attribute has
an associated convenience method of the same name.

=over 4

=item units

This specifies the units of the parameter, if appropriate.

  print $object -> co -> units("bkg", "pre1")
    ==prints==>
      eV (relative to e0 or to the beginning of the data)

=item restart

This is true or false depending on whether changing the parameter
takes effect immediately or if a restart of the application is
required.

=item onvalue/offvalue

These are the values associated with the true and false states of a
boolean parameter.  If unspecified in the configuration file, they
default to 1 and 0.

  print $object -> co -> onvalue("bkg", "flatten")
    ==prints==>
      1

=item minint/maxint

These are the minimum and maximum allowed values of a positive integer
parameter.  If unspecified in the configuration file, they default to
0 and 1e9.

  print $object -> co -> maxint("bkg", "kw")
    ==prints==>
      3

=item options

This are the possible options for a list parameter.  They are stored
as a space-separated string.  This string will have to be split on the
spaces to be used as a list.

  print $object -> co -> options("fft", "kwindow")
    ==prints==>
      hanning kaiser-bessel welch parzen sine

=back

=head1 USER DEFINED PARAMETERS

The C<set> method creates a user-defined parameter and stores it
in the Config object for later use.

    $merged->set(ndata=>$ndata);

These user-defined parameter are the accessed via C<get> (rather than
C<default>)

    print "Number of sets in merge = ", $data->co->get("ndata");

The reason to use the Config object to store parameters is that they
are easily used by the templating system that Demeter uses to do its
work.  Also, the user-defined parameters will be serialized along with
the Config object.

This system is used extensively in the C<merge> method from
L<Demeter::Data::Process>.  See also the F<merge_*.tmpl> files in the
"process" group of templates.

=head1 DIAGNOSTICS

=over 4

=item C<You cannot have more than one Config object>

One Config object is created when Demeter loads.  It is forbidden to
create a second one.  Config paramaters are global to an instance of
Demeter.

=item C<The Demeter configuration file ($config_file) does not exist>

The F<demeter.config> file should be installed in the F<lib/>
subdirectory beneath the location of the F<Demeter.pm> module.  This
diagnostic says that configuration file is absent.

=item C<The Demeter configuration file ($config_file) cannot be opened>

The F<demeter.config> file should be installed in the F<lib/>
subdirectory beneath the location of the F<Demeter.pm> module.  This
diagnostic says that configuration file is unreadable, perhaps because
of a permissions setting.

=item C<$key is not a valid configuration parameter>

This diagnostic says that you have attempted to retrieve an attribute
for a parameter that does not exist.

=back


=head1 DEMETER CONFIGURATION FILES

The configuration files are broken into a simple hierarchy.  Parameter
groups tend to be contained in their own files which are included into
the master configuration file F<config.demeter_conf>.

The included configuration files are fairly structured.
Beginning-of-line whitespace (2 spaces, no more, no less) is important
in the parameter descriptions, as are the empty lines and the lines
that begin with a dot.  The empty lines denote separations between
entries.  The dots are used to build lists in the descriptions.

The parser for this file is fairly stupid, so if you make mistakes in
the use of whitespace, bad things will happen.  An L</EMACS MAJOR MODE> is
provided, the main purpose of which is to color text as an indication
of correct formatting.  Also, order matters.  It is very important
that "variable=" comes first, then "type=", then "default=", then the
rest.

The contents of the configuration files are used to populate the
Config object and to generate the files system-wide F<demeter.ini>.
It can also be used to populate the preferences dialog in a GUI.  Here
is a list of suggested widgets to use with each configuration type

   variable types         suggested widget
     string                TextCtrl
     regex                 TextCtrl
     real                  TextCtrl -- validates to accept only numbers
     positive integer      SpinCtrl -- restricted to be >= 0
     list                  Choice or some other multiple selection widget
     boolean               Checkbutton
     keypress              TextCtrl -- rigged to display one character at a time
     color                 ColorPicker -- launches color browser
     font                  FontPicker -- does nothing at this time

=head1 EMACS MAJOR MODE

A major mode for emacs is provided with the Demeter package.  It
provides some functionality for editing Demeter configuration files.
The most significant functionality is syntax colorization that
enforces overly strict rules for the layout of the file.  Using this
major mode helps avoid introducing formatting errors into a
configuration file which has an admittedly fragile syntax.

See F<config-mode.el> in the F<tools/> subdirectory of the Demeter
distribution.  To use config-mode, simply place the file somewhere in
the Emacs load path and put this line in your .emacs file or some
other emacs initialization file.

   (autoload 'config-mode "config-mode")

The mode is tiny.  You can byte-compile or not -- as you wish.


=head1 DEPENDENCIES

See the F<Build.PL> file for a list of dependencies.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

This object needs to be a singleton, but it also needs to extend
Demeter.  How is that done in Moose?  I have no idea....

=item *

Nothing prevents overwriting Config entries -- this is a potential
problem for user-defined parameetrs.

=item *

There is no introspection method for returning all (sets of) user
defined parameters.

=item *

The configuration file syntax is somewhat fragile.

=item *

C<read_config_file> only handles files relative to the master config
file.  If it could understand fully resolved filenames, it could, for
instance, look for additional configuration files in userspace or
application space.  That way apps and one-off tools could use the
configuration system.

=item *

When (how often) should ini files be written out?

=back

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel, L<http://bruceravel.github.io/home>

L<http://bruceravel.github.io/demeter/>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut