« blib/lib/App/RecordStream/Operation/collate/BaseClumperCallback.pm
↑ Summary
blib/lib/App/RecordStream/Clumper/Options.pm »
| 1 | package App::RecordStream::Operation::generate; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $VERSION = "4.0.23"; | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use App::RecordStream::Executor::Getopt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use App::RecordStream::Executor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use App::RecordStream::InputStream; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use App::RecordStream::Record; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use App::RecordStream::Executor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | sub init { | ||||||||||
| 18 | 3 | ▶ | my $this = shift; | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | 3 | my $args = shift; | |||||||||
| 20 | |||||||||||
| 21 | 3 | my $keychain = '_chain'; | |||||||||
| 22 | 3 | my $passthrough = 0; | |||||||||
| 23 | 3 | my $executor_options = App::RecordStream::Executor::Getopt->new(); | |||||||||
| 24 | |||||||||||
| 25 | 3 | my $spec = { | |||||||||
| 26 | 'keychain=s' => \$keychain, | ||||||||||
| 27 | 'passthrough' => \$passthrough, | ||||||||||
| 28 | $executor_options->arguments(), | ||||||||||
| 29 | }; | ||||||||||
| 30 | |||||||||||
| 31 | 3 | $this->parse_options($args, $spec, ['bundling']); | |||||||||
| 32 | |||||||||||
| 33 | 2 | my $expression = $executor_options->get_string($args); | |||||||||
| 34 | 2 | my $executor = App::RecordStream::Executor->new($expression); | |||||||||
| 35 | |||||||||||
| 36 | 2 | $this->{'KEYCHAIN'} = $keychain; | |||||||||
| 37 | 2 | $this->{'PASSTHROUGH'} = $passthrough; | |||||||||
| 38 | 2 | $this->{'EXECUTOR'} = $executor; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | sub accept_record { | ||||||||||
| 42 | 4 | ▶ | my $this = shift; | ||||||||
|
Subroutine
accept_record: called CC=5 SCAR=17.0
Pod
undocumented
| |||||||||||
| 43 | 4 | my $record = shift; | |||||||||
| 44 | |||||||||||
| 45 | 4 | ▶ | $this->push_record($record) if $this->{'PASSTHROUGH'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 4 | my $interpolated_command = $this->{'EXECUTOR'}->execute_code($record); | |||||||||
| 48 | |||||||||||
| 49 | 4 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | chomp $@; | |||||||||
| 51 | 0 | warn "# $0 interpolating command threw: " . $@ . "\n"; | |||||||||
| 52 | 0 | return 1; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 4 | my $pid = open(my $pipe, "-|", $interpolated_command); | |||||||||
| 56 | |||||||||||
| 57 | 4 | ▶ | if (!$pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | warn "# $0 open(..., \"$interpolated_command |\") failed: $!\n"; | |||||||||
| 59 | 0 | return 1; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 4 | my $generator_stream = App::RecordStream::InputStream->new(FH => $pipe); | |||||||||
| 63 | |||||||||||
| 64 | 4 | ▶ | while(my $generated_record = $generator_stream->get_record()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 4 | ${$generated_record->guess_key_from_spec($this->{'KEYCHAIN'})} = $record->as_hashref(); | |||||||||
| 66 | 4 | $this->push_record($generated_record); | |||||||||
| 67 | } | ||||||||||
| 68 | # App::RecordStream::InputStream closes the file handle for us | ||||||||||
| 69 | |||||||||||
| 70 | 4 | return 1; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub add_help_types { | ||||||||||
| 74 | 3 | ▶ | my $this = shift; | ||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | 3 | $this->use_help_type('keyspecs'); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub usage | ||||||||||
| 79 | { | ||||||||||
| 80 | 1 | ▶ | my $this = shift; | ||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 81 | |||||||||||
| 82 | 1 | my $options = [ | |||||||||
| 83 | App::RecordStream::Executor::options_help(), | ||||||||||
| 84 | [ 'passthrough', 'Emit input record in addition to generated records' ], | ||||||||||
| 85 | [ 'keychain <name>', 'Use \'name\' as the chain key (default is \'_chain\') may be a key spec, see \'--help-keyspecs\' for more info' ], | ||||||||||
| 86 | ]; | ||||||||||
| 87 | |||||||||||
| 88 | 1 | my $args_string = $this->options_string($options); | |||||||||
| 89 | |||||||||||
| 90 | 1 | my $usage = <<USAGE; | |||||||||
| 91 | Usage: recs-generate <args> <command> [<files>] | ||||||||||
| 92 | __FORMAT_TEXT__ | ||||||||||
| 93 | Executes <command> for each record to generate a record stream, which is | ||||||||||
| 94 | then output with a chain link back to the original record. | ||||||||||
| 95 | |||||||||||
| 96 | <command> is executed opened as a command for each record of input (or | ||||||||||
| 97 | records from <files>) with \$r set to a App::RecordStream::Record object. The output | ||||||||||
| 98 | lines of each command execution are interpreted as a serialized Recs records, | ||||||||||
| 99 | one per line. Each such line is reconstituted as a App::RecordStream::Record, and the | ||||||||||
| 100 | '_chain' key is added to the record before it is printed. The value of the | ||||||||||
| 101 | '_chain' key is the record that was originally passed to the eval expression. | ||||||||||
| 102 | __FORMAT_TEXT__ | ||||||||||
| 103 | |||||||||||
| 104 | For instance. If you did: | ||||||||||
| 105 | recs-generate "recs-fromatomfeed http://...?key=\$r->{title}..." | ||||||||||
| 106 | |||||||||||
| 107 | with input that looked like: | ||||||||||
| 108 | { 'title' : 'foo' } | ||||||||||
| 109 | { 'title' : 'bar' } | ||||||||||
| 110 | |||||||||||
| 111 | then recs-generate would end up executing: | ||||||||||
| 112 | recs-fromatomfeed http://...?key=foo... | ||||||||||
| 113 | |||||||||||
| 114 | __FORMAT_TEXT__ | ||||||||||
| 115 | and interpreting the result as a series of new line separated records. | ||||||||||
| 116 | |||||||||||
| 117 | If the result from recs-fromatomfeed was something like: | ||||||||||
| 118 | __FORMAT_TEXT__ | ||||||||||
| 119 | { 'title' : 'zip' } | ||||||||||
| 120 | { 'title' : 'zap' } | ||||||||||
| 121 | |||||||||||
| 122 | __FORMAT_TEXT__ | ||||||||||
| 123 | then recs-generate would add the chain link so the output would look like: | ||||||||||
| 124 | __FORMAT_TEXT__ | ||||||||||
| 125 | { 'title' : 'zip', 'chain' : { 'title' : 'foo' } } | ||||||||||
| 126 | { 'title' : 'zap', 'chain' : { 'title' : 'foo' } } | ||||||||||
| 127 | |||||||||||
| 128 | Arguments: | ||||||||||
| 129 | $args_string | ||||||||||
| 130 | |||||||||||
| 131 | Examples: | ||||||||||
| 132 | Chain recs from a feed to recs from a second feed and the print the titles. | ||||||||||
| 133 | recs-fromatomfeed "http://..." | recs-generate "recs-fromatomfeed http://...?key=\$r->{title}" | recs-eval 'join("\t", \$r->{title}, \$r->{chain}->{title})' | ||||||||||
| 134 | USAGE | ||||||||||
| 135 | |||||||||||
| 136 | 1 | return $usage; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 2 | 1; | |||||||||