user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] lei: simplify lazy-loading
Date: Sun, 21 Mar 2021 13:24:05 +0200	[thread overview]
Message-ID: <20210321112405.25679-1-e@80x24.org> (raw)

This makes it slightly easier to implement future commands,
since there'll be a couple more relatively self-contained
ones.
---
 lib/PublicInbox/LEI.pm        | 22 ++++++----------------
 lib/PublicInbox/LeiConvert.pm |  6 +++---
 lib/PublicInbox/LeiImport.pm  |  6 +++---
 lib/PublicInbox/LeiP2q.pm     |  6 +++---
 4 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index bf97a680..b6d21af6 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -598,7 +598,12 @@ sub dispatch {
 	}
 	my $func = "lei_$cmd";
 	$func =~ tr/-/_/;
-	if (my $cb = __PACKAGE__->can($func)) {
+	my $cb = __PACKAGE__->can($func) // ($CMD{$cmd} ? do {
+		my $mod = "PublicInbox::Lei\u$cmd";
+		($INC{"PublicInbox/Lei\u$cmd.pm"} //
+			eval("require $mod")) ? $mod->can($func) : undef;
+	} : undef);
+	if ($cb) {
 		optparse($self, $cmd, \@argv) or return;
 		if (my $chdir = $self->{opt}->{C}) {
 			for my $d (@$chdir) {
@@ -685,21 +690,6 @@ sub lei_config {
 	x_it($self, $?) if $?;
 }
 
-sub lei_import {
-	require PublicInbox::LeiImport;
-	PublicInbox::LeiImport->call(@_);
-}
-
-sub lei_convert {
-	require PublicInbox::LeiConvert;
-	PublicInbox::LeiConvert->call(@_);
-}
-
-sub lei_p2q {
-	require PublicInbox::LeiP2q;
-	PublicInbox::LeiP2q->call(@_);
-}
-
 sub lei_init {
 	my ($self, $dir) = @_;
 	my $cfg = _lei_cfg($self, 1);
diff --git a/lib/PublicInbox/LeiConvert.pm b/lib/PublicInbox/LeiConvert.pm
index fcc67f0b..8d3b221a 100644
--- a/lib/PublicInbox/LeiConvert.pm
+++ b/lib/PublicInbox/LeiConvert.pm
@@ -77,11 +77,11 @@ sub do_convert { # via wq_do
 	delete $self->{wcb}; # commit
 }
 
-sub call { # the main "lei convert" method
-	my ($cls, $lei, @inputs) = @_;
+sub lei_convert { # the main "lei convert" method
+	my ($lei, @inputs) = @_;
 	my $opt = $lei->{opt};
 	$opt->{kw} //= 1;
-	my $self = $lei->{cnv} = bless {}, $cls;
+	my $self = $lei->{cnv} = bless {}, __PACKAGE__;
 	my $in_fmt = $opt->{'in-format'};
 	my (@f, @d);
 	$opt->{dedupe} //= 'none';
diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm
index ae24a1fa..0e2a96e8 100644
--- a/lib/PublicInbox/LeiImport.pm
+++ b/lib/PublicInbox/LeiImport.pm
@@ -63,13 +63,13 @@ sub import_start {
 	while ($op && $op->{sock}) { $op->event_step }
 }
 
-sub call { # the main "lei import" method
-	my ($cls, $lei, @inputs) = @_;
+sub lei_import { # the main "lei import" method
+	my ($lei, @inputs) = @_;
 	my $sto = $lei->_lei_store(1);
 	$sto->write_prepare($lei);
 	my ($net, @f, @d);
 	$lei->{opt}->{kw} //= 1;
-	my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
+	my $self = $lei->{imp} = bless { inputs => \@inputs }, __PACKAGE__;
 	if ($lei->{opt}->{stdin}) {
 		@inputs and return $lei->fail("--stdin and @inputs do not mix");
 		$lei->check_input_format or return;
diff --git a/lib/PublicInbox/LeiP2q.pm b/lib/PublicInbox/LeiP2q.pm
index c5718603..302d7864 100644
--- a/lib/PublicInbox/LeiP2q.pm
+++ b/lib/PublicInbox/LeiP2q.pm
@@ -174,9 +174,9 @@ sub do_p2q { # via wq_do
 	$lei->out(@q, "\n");
 }
 
-sub call { # the "lei patch-to-query" entry point
-	my ($cls, $lei, $input) = @_;
-	my $self = $lei->{p2q} = bless {}, $cls;
+sub lei_p2q { # the "lei patch-to-query" entry point
+	my ($lei, $input) = @_;
+	my $self = $lei->{p2q} = bless {}, __PACKAGE__;
 	if ($lei->{opt}->{stdin}) {
 		$self->{0} = delete $lei->{0}; # guard from lei_atfork_child
 	} else {

                 reply	other threads:[~2021-03-21 11:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210321112405.25679-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).