| 1 | |||
| 2 | package App::FQStat::Config; | ||
| 3 | # App::FQStat is (c) 2007-2009 Steffen Mueller | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use App::FQStat::Debug; | ||
| 11 | use YAML::Tiny (); | ||
| 12 | use File::HomeDir (); | ||
| 13 | use File::Spec; | ||
| 14 | use Term::ANSIScreen qw(); | ||
| 15 | |||
| 16 | |||
| 17 | use base 'Exporter'; | ||
| 18 | our %EXPORT_TAGS = ( | ||
| 19 | 'all' => [qw( | ||
| 20 | get_config | ||
| 21 | set_config | ||
| 22 | )], | ||
| 23 | ); | ||
| 24 | our @EXPORT_OK = @{$EXPORT_TAGS{'all'}}; | ||
| 25 | |||
| 26 | my $YAMLObject; | ||
| 27 | my $Config; | ||
| 28 | |||
| 29 | sub get { | ||
| 30 | warnenter if ::DEBUG; | ||
| 31 | if (not defined $Config) { | ||
| 32 | read_configuration(); | ||
| 33 | } | ||
| 34 | my $key = shift; | ||
| 35 | return $Config->{$key}; | ||
| 36 | } | ||
| 37 | |||
| 38 | { | ||
| 39 | no strict; | ||
| 40 | *get_config = *get; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub set { | ||
| 44 | warnenter if ::DEBUG; | ||
| 45 | if (not defined $Config) { | ||
| 46 | read_configuration(); | ||
| 47 | } | ||
| 48 | my $key = shift; | ||
| 49 | my $value = shift; | ||
| 50 | $Config->{$key} = $value; | ||
| 51 | save_configuration() if $Config->{persistent}; | ||
| 52 | return $value; | ||
| 53 | } | ||
| 54 | |||
| 55 | { | ||
| 56 | no strict; | ||
| 57 | *set_config = *set; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub read_configuration { | ||
| 61 | warnenter if ::DEBUG; | ||
| 62 | my $cfg_file = _config_file(); | ||
| 63 | warnline("Config file: '$cfg_file'.\n") if ::DEBUG; | ||
| 64 | if (not -f $cfg_file) { | ||
| 65 | $YAMLObject = YAML::Tiny->new(); | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | $YAMLObject = YAML::Tiny->read($cfg_file); | ||
| 69 | } | ||
| 70 | push @$YAMLObject, {} if @$YAMLObject == 0; | ||
| 71 | $Config = $YAMLObject->[0]; | ||
| 72 | _default_config(); | ||
| 73 | |||
| 74 | warnline $YAMLObject->write_string()."\n" if ::DEBUG > 1; | ||
| 75 | return $Config; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub save_configuration { | ||
| 79 | warnenter if ::DEBUG; | ||
| 80 | $YAMLObject->write(_config_file()); | ||
| 81 | } | ||
| 82 | |||
| 83 | { | ||
| 84 | my $config_file; | ||
| 85 | sub _config_file { | ||
| 86 | warnenter if ::DEBUG > 1; | ||
| 87 | if (not defined $config_file) { | ||
| 88 | my $home = File::HomeDir->my_home; | ||
| 89 | $config_file = File::Spec->catfile($home, '.fqstat.yml'); | ||
| 90 | } | ||
| 91 | return $config_file; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub reset_configuration { | ||
| 96 | warnenter if ::DEBUG; | ||
| 97 | my $yaml = YAML::Tiny->new; | ||
| 98 | $YAMLObject = $yaml; | ||
| 99 | push @$YAMLObject, {} if @$YAMLObject == 0; | ||
| 100 | $Config = $YAMLObject->[0]; | ||
| 101 | _default_config(); | ||
| 102 | return $Config; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _default_config { | ||
| 106 | warnenter if ::DEBUG; | ||
| 107 | my %default = ( | ||
| 108 | persistent => 1, | ||
| 109 | qstatcmd => 'qstat', | ||
| 110 | qdelcmd => 'qdel', | ||
| 111 | qaltercmd => 'qalter', | ||
| 112 | qmodcmd => 'qmod', | ||
| 113 | sshcommand => '', | ||
| 114 | version => $App::FQStat::VERSION, | ||
| 115 | colors => $App::FQStat::Colors::DefaultColors, | ||
| 116 | color_schemes => $App::FQStat::Colors::DefaultColorSchemes, | ||
| 117 | summary_mode => 0, | ||
| 118 | summary_clustering => 0, | ||
| 119 | summary_clustering_similarity => 0.25, | ||
| 120 | ); | ||
| 121 | |||
| 122 | my %upgrades; | ||
| 123 | %upgrades = ( | ||
| 124 | old => sub { | ||
| 125 | my $cfg = shift; | ||
| 126 | %$cfg = %default; | ||
| 127 | }, | ||
| 128 | '6.0' => sub { | ||
| 129 | my $cfg = shift; | ||
| 130 | $cfg->{colors} = $App::FQStat::Colors::DefaultColors; | ||
| 131 | $upgrades{6.1}->($cfg); | ||
| 132 | save_configuration(); | ||
| 133 | }, | ||
| 134 | '6.1' => sub { | ||
| 135 | my $cfg = shift; | ||
| 136 | $cfg->{color_schemes} = $App::FQStat::Colors::DefaultColorSchemes; | ||
| 137 | $upgrades{6.2}->($cfg); | ||
| 138 | save_configuration(); | ||
| 139 | }, | ||
| 140 | '6.2' => sub { | ||
| 141 | my $cfg = shift; | ||
| 142 | foreach my $scheme (values %{$cfg->{color_schemes}}) { | ||
| 143 | $scheme->{summary} = $scheme->{user_highlight}; | ||
| 144 | } | ||
| 145 | $cfg->{colors}->{summary} = $cfg->{colors}->{user_highlight}; | ||
| 146 | $cfg->{summary_clustering_similarity} = 0.25; | ||
| 147 | save_configuration(); | ||
| 148 | }, | ||
| 149 | ); | ||
| 150 | |||
| 151 | # upgrade old configs | ||
| 152 | my $cfgversion = $Config->{version}; | ||
| 153 | $upgrades{old}->($Config), $cfgversion = $Config->{version} if not $cfgversion or $cfgversion < 5; | ||
| 154 | |||
| 155 | $upgrades{$cfgversion}->($Config) if exists $upgrades{$cfgversion}; | ||
| 156 | |||
| 157 | $Config->{version} = $App::FQStat::VERSION, save_configuration() if $Config->{version} ne $App::FQStat::VERSION; | ||
| 158 | |||
| 159 | my $add = 0; | ||
| 160 | foreach my $key (keys %default) { | ||
| 161 | $add++, $Config->{$key} = $default{$key} if not defined $Config->{$key}; | ||
| 162 | } | ||
| 163 | save_configuration() if $Config->{persistent} and $add; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub edit_configuration { | ||
| 167 | warnenter if ::DEBUG; | ||
| 168 | require Term::CallEditor; | ||
| 169 | my $string = $YAMLObject->write_string()."\n"; | ||
| 170 | $string =~ s/^\s*---\s*//s; | ||
| 171 | |||
| 172 | my $fh = Term::CallEditor::solicit($string); | ||
| 173 | die "Error while editing the fqstat configuration: $Term::CallEditor::errstr\n" unless $fh; | ||
| 174 | local $/ = undef; | ||
| 175 | my $result = "---\n" . <$fh>; | ||
| 176 | my $yml; | ||
| 177 | eval { $yml = YAML::Tiny->read_string($result); }; | ||
| 178 | my $errstr = "The edited configuration file has a syntax error! I'm reverting to the old configuration.\n"; | ||
| 179 | if ($@) { | ||
| 180 | print $errstr . "The error message from the parser was: $@\n"; | ||
| 181 | print "(continue with any key)"; | ||
| 182 | App::FQStat::Input::get_input_key(1000); | ||
| 183 | } | ||
| 184 | elsif (not defined $yml) { | ||
| 185 | print $errstr; | ||
| 186 | print "(continue with any key)"; | ||
| 187 | App::FQStat::Input::get_input_key(1000); | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | $YAMLObject = $yml; | ||
| 191 | push @$YAMLObject, {} if @$YAMLObject == 0; | ||
| 192 | $Config = $YAMLObject->[0]; | ||
| 193 | _default_config(); | ||
| 194 | save_configuration() if $Config->{persistent}; | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | return(); | ||
| 198 | } | ||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 |