From: Ralf Ertzinger Date: Wed, 8 May 2013 18:20:32 +0000 (+0200) Subject: Merge branch 'rertzinger/generic-load-save' into weechat X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=videosite.git;a=commitdiff_plain;h=01fc17a9fd2a11cd5826a2581fe662e96ac5912c;hp=3255f6f448ae2e392871dc5241536b0dfb009a59 Merge branch 'rertzinger/generic-load-save' into weechat --- diff --git a/libvideosite.pm b/libvideosite.pm index 959e61c..01fa7d9 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,8 @@ my @grabbers; my @getters; my $getter; my %builtin_config = (); +my $builtin_config_path; +my $builtin_config_default; our $error; # @@ -921,6 +925,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 { @@ -936,6 +963,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 { @@ -1034,6 +1078,14 @@ sub register_api { $debug = $a->{_debug}->(); } + if (exists($a->{_config_path})) { + $builtin_config_path = $a->{_config_path}->(); + } + + if (exists($a->{_config_default})) { + $builtin_config_default = $a->{_config_default}->(); + } + @outputstack = ({ewpf => $remote_api->{'io'}, window => ""}); return 1; diff --git a/videosite-dl.pl b/videosite-dl.pl index 594b020..46c8b49 100755 --- a/videosite-dl.pl +++ b/videosite-dl.pl @@ -40,13 +40,8 @@ push(@INC, dirname(realpath($0))); load 'libvideosite'; unless(libvideosite::register_api({ - config_init => sub {}, - config_save => sub {}, - config_get => sub { return $config{join(".", @{$_[0]})} }, - config_set => sub { $config{join(".", @{$_[0]})} = $_[1] }, - config_has => sub { exists($config{join(".", @{$_[0]})}) }, - config_del => sub { delete($config{join(".", @{$_[0]})}) }, link_callback => \&link_callback, + _config_default => sub { return \%config }, _debug => sub { return $debug }, })) { die("Error registering API: $libvideosite::error");