X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=libvideosite.pm;h=34191b8ac73bca426c04f8c1d6f8046bceb5c3b4;hb=39767f785076ce64d831cbfbc7f51cc2af5ef22d;hp=f7f3d9da2d4e47416ed08ad5546513b80033b422;hpb=f9c997ac49bfdcd65ee89127f4d806e813355d8c;p=videosite.git diff --git a/libvideosite.pm b/libvideosite.pm index f7f3d9d..34191b8 100644 --- a/libvideosite.pm +++ b/libvideosite.pm @@ -17,6 +17,8 @@ use LWP::UserAgent; use Data::Dumper; use File::Basename; use Cwd qw(realpath); +use JSON -support_by_pp; +use File::Temp qw(tempfile); use strict; @ISA = qw(Exporter); @@ -30,6 +32,10 @@ my @grabbers; my @getters; my $getter; my %builtin_config = (); +my $builtin_config_path; +my $builtin_config_default; +my $config_cache = 1; +my %config_cache = (); our $error; # @@ -120,6 +126,10 @@ my $videosite_commands = { 'nodebug' => sub { _cmd_nodebug(@_); }, + + 'cache' => sub { + _cmd_cache(@_); + }, }; # @@ -149,7 +159,7 @@ sub _io { # @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text; - $outputstack[0]->{ewpf}->(sprintf($format, @text)); + $outputstack[0]->{io}->(sprintf($format, @text)); } # @@ -297,36 +307,53 @@ sub _load_modules($) { # sub _config_get { my $path = shift; + my $dotpath = join('.', @{$path}); my $value; - $value = $remote_api->{config_get}->($path); - _debug("config: getting %s=%s", join('.', @{$path}), $value); + if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{value})) { + $value = $config_cache{$dotpath}->{value}; + } else { + $value = $remote_api->{config_get}->($path); + $config_cache{$dotpath} = {value => $value, has => 1}; + + } + _debug("config: getting %s=%s", $dotpath, $value); return $value; } sub _config_set { my $path = shift; + my $dotpath = join('.', @{$path}); my $value = shift; - _debug("config: setting %s=%s", join('.', @{$path}), $value); + _debug("config: setting %s=%s", $dotpath, $value); + $config_cache{$dotpath} = {value => $value, has => 1}; return $remote_api->{config_set}->($path, $value); } sub _config_has { my $path = shift; + my $dotpath = join('.', @{$path}); my $b; - $b = $remote_api->{config_has}->($path); - _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false'); + if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{has})) { + $b = $config_cache{$dotpath}->{has}; + } else { + $b = $remote_api->{config_has}->($path); + $config_cache{$dotpath}->{has} = $b; + } + _debug("config: testing %s (%s)", $dotpath, $b?'true':'false'); return $b; } sub _config_del { my $path = shift; + my $dotpath = join('.', @{$path}); - _debug("config: removing %s", join('.', @{$path})); + _debug("config: removing %s", $dotpath); + delete($config_cache{$dotpath}); $remote_api->{config_del}->($path); } @@ -881,8 +908,6 @@ sub _cmd_debug { _io("Debug for this window enabled"); $debugwindows{$event->{window}} = 1; } - - _io("keys in debugwindows: %s", join(", ", keys(%debugwindows))); } # @@ -903,8 +928,31 @@ sub _cmd_nodebug { delete($debugwindows{$event->{window}}); _io("Debug for this window disabled"); } +} - _io("keys in debugwindows: %s", join(", ", keys(%debugwindows))); +# +# Display or clear the content of the config cache +# +sub _cmd_cache { + my $event = shift; + my $subcmd = shift; + + $subcmd = 'list' unless defined($subcmd); + $subcmd = lc($subcmd); + + if ($subcmd eq 'list') { + _io("Content of config cache:"); + foreach (sort(keys(%config_cache))) { + if (exists($config_cache{$_}->{value})) { + _io("%s => %s", $_, $config_cache{$_}->{value}); + } else { + _io("%s present", $_); + } + } + } elsif ($subcmd eq 'clear') { + _debug("Clearing config cache"); + %config_cache = (); + } } @@ -925,6 +973,29 @@ sub _grabbers { # ============================================== # sub _builtin_config_init { + + if (defined($builtin_config_path)) { + my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json'); + + _debug("Trying to load configuration from %s", $filename); + + if (-r $filename) { + eval { + local $/; + open(CONF, '<', $filename); + %builtin_config = %{JSON->new->utf8->decode()}; + close(CONF); + } or do { + _io("Error loading configuration: %s", $@); + } + }; + } elsif (defined($builtin_config_default)) { + _debug("Initializing builtin config from external default"); + foreach (keys(%{$builtin_config_default})) { + _debug("Setting %s=%s", $_, $builtin_config_default->{$_}); + $builtin_config{$_} = $builtin_config_default->{$_}; + } + } } sub _builtin_config_get { @@ -940,6 +1011,23 @@ sub _builtin_config_has { } sub _builtin_config_save { + + if (defined($builtin_config_path)) { + my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json'); + + _debug("Attempting to save config to %s", $filename); + + eval { + my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path); + print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config); + close($tempfile); + rename($tempfn, $filename); + } or do { + return 0; + } + } + + return 1; } sub _builtin_config_del { @@ -1038,7 +1126,19 @@ sub register_api { $debug = $a->{_debug}->(); } - @outputstack = ({ewpf => $remote_api->{'io'}, window => ""}); + if (exists($a->{_config_path})) { + $builtin_config_path = $a->{_config_path}->(); + } + + if (exists($a->{_config_default})) { + $builtin_config_default = $a->{_config_default}->(); + } + + if (exists($a->{_config_cache})) { + $config_cache = $a->{_config_cache}->(); + } + + @outputstack = ({io => $remote_api->{'io'}, window => ""}); return 1; }