about summary refs log tree commit homepage
path: root/lib/PublicInbox/InboxWritable.pm
diff options
context:
space:
mode:
authorEric Wong (Contractor, The Linux Foundation) <e@80x24.org>2018-03-19 20:49:30 +0000
committerEric Wong (Contractor, The Linux Foundation) <e@80x24.org>2018-03-20 15:03:39 +0000
commit70207d974c5a965ef849b58c27b63fd644b3293e (patch)
tree2e1b87538d4c26ac6a6505e09f464fdb1249c8e1 /lib/PublicInbox/InboxWritable.pm
parentdfed6cc6f2881c77478174dd5eb9b93352b1f1c1 (diff)
downloadpublic-inbox-70207d974c5a965ef849b58c27b63fd644b3293e.tar.gz
This code will be shared with future mass-import tools.
Diffstat (limited to 'lib/PublicInbox/InboxWritable.pm')
-rw-r--r--lib/PublicInbox/InboxWritable.pm57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/PublicInbox/InboxWritable.pm b/lib/PublicInbox/InboxWritable.pm
new file mode 100644
index 00000000..0a976ea2
--- /dev/null
+++ b/lib/PublicInbox/InboxWritable.pm
@@ -0,0 +1,57 @@
+# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Extends read-only Inbox for writing
+package PublicInbox::InboxWritable;
+use strict;
+use warnings;
+use base qw(PublicInbox::Inbox);
+use PublicInbox::Import;
+
+sub new {
+        my ($class, $ibx) = @_;
+        bless $ibx, $class;
+}
+
+sub importer {
+        my ($self, $parallel) = @_;
+        $self->{-importer} ||= eval {
+                my $v = $self->{version} || 1;
+                if ($v == 2) {
+                        eval { require PublicInbox::V2Writable };
+                        die "v2 not supported: $@\n" if $@;
+                        my $v2w = PublicInbox::V2Writable->new($self);
+                        $v2w->{parallel} = $parallel;
+                        $v2w;
+                } elsif ($v == 1) {
+                        my $git = $self->git;
+                        my $name = $self->{name};
+                        my $addr = $self->{-primary_address};
+                        PublicInbox::Import->new($git, $name, $addr, $self);
+                } else {
+                        die "unsupported inbox version: $v\n";
+                }
+        }
+}
+
+sub filter {
+        my ($self) = @_;
+        my $f = $self->{filter};
+        if ($f && $f =~ /::/) {
+                my @args = (-inbox => $self);
+                # basic line splitting, only
+                # Perhaps we can have proper quote splitting one day...
+                ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
+
+                eval "require $f";
+                if ($@) {
+                        warn $@;
+                } else {
+                        # e.g: PublicInbox::Filter::Vger->new(@args)
+                        return $f->new(@args);
+                }
+        }
+        undef;
+}
+
+1;