Skip to content

Commit

Permalink
Add the ability to specify any arbitrary class name and plugin data f…
Browse files Browse the repository at this point in the history
…or `test-parser` command
  • Loading branch information
Igor Afanasyev committed Dec 4, 2015
1 parent 803c48b commit 364decc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 22 deletions.
38 changes: 34 additions & 4 deletions doc/html/serge-test-parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@ <h1 id="NAME">NAME</h1>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<p><code>serge test-parser &lt;parser_name&gt; &lt;file_path&gt; [--import-mode] [--output-mode=&lt;mode&gt;]</code></p>
<p><code>serge test-parser &lt;parser_name&gt; &lt;file_path&gt; [--import-mode] [--output-mode=&lt;mode&gt;] [--data-file=&lt;file_path&gt;]</code></p>

<p>Where <code>&lt;parser_name&gt;</code> is a file name of the parser sans its extension (parsers are typically located in lib/Serge/Engine/Plugin folder), and <code>&lt;file_path&gt;</code> is a path to the localizable file to test this parser on.</p>
<p>Where <code>&lt;parser_name&gt;</code> is a class name of the parser, and <code>&lt;file_path&gt;</code> is a path to the localizable file to test this parser on.</p>

<p>Note that you can omit the <code>Serge::Engine::Plugin::</code> prefix of the parser class name if your parser is located in lib/Serge/Engine/Plugin folder. Consider the following examples of <code>&lt;parser_name&gt;</code> parameter:</p>

<dl>

<dt><b>parse_android</b></dt>
<dd>

<p>Use <code>Serge::Engine::Plugin::parse_android</code> parser class.</p>

</dd>
<dt><b>Serge::Engine::Plugin::parse_android</b></dt>
<dd>

<p>Same as above (explicit class declaration).</p>

</dd>
<dt><b>ACME::Parser</b></dt>
<dd>

<p>Use <code>ACME::Parser</code> parser class.</p>

</dd>
</dl>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

Expand All @@ -42,7 +66,7 @@ <h1 id="OPTIONS">OPTIONS</h1>
<p>With this option, parser will be told it works in import mode. In such mode import-aware parsers are expected to extract translations rather than source strings, and also skip missing translations.</p>

</dd>
<dt><b>--output-mode=mode</b></dt>
<dt><b>--output-mode=&lt;mode&gt;</b></dt>
<dd>

<p>By default, parsed data will be emitted in an easily readable Config::Neat format (the format used in Serge configuration files). Such files are also suitable for diff. However, there are alternative output modes available:</p>
Expand All @@ -57,6 +81,12 @@ <h1 id="OPTIONS">OPTIONS</h1>
</dd>
</dl>

</dd>
<dt><b>--data-file=&lt;file_path&gt;</b></dt>
<dd>

<p>Some parsers require configuration data. You can pass data structure to the parser using this parameter. Target file should be a configuration file in Config::Neat format with the parser data that you would usually put into <code>jobs &gt; ... &gt; parser &gt; data</code> section of Serge configuration file.</p>

</dd>
</dl>

Expand All @@ -78,7 +108,7 @@ <h1 id="SEE-ALSO">SEE ALSO</h1>
</div>
<div class="version">
Serge 1.0.<br/>
Page last generated: Fri Dec 4 12:07:24 2015
Page last generated: Fri Dec 4 13:35:21 2015
</div>
</div>
</div>
Expand Down
35 changes: 30 additions & 5 deletions doc/pod/serge-test-parser.pod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ serge-test-parser - Test parser against any given file
=head1 SYNOPSIS

C<< serge test-parser E<lt>parser_nameE<gt> E<lt>file_pathE<gt> [--import-mode]
[--output-mode=E<lt>modeE<gt>] >>
[--output-mode=E<lt>modeE<gt>] [--data-file=E<lt>file_pathE<gt>] >>

Where C<E<lt>parser_nameE<gt>> is a file name of the parser sans its extension
(parsers are typically located in lib/Serge/Engine/Plugin folder),
and C<E<lt>file_pathE<gt>> is a path to the localizable file to test this parser on.
Where C<E<lt>parser_nameE<gt>> is a class name of the parser, and C<E<lt>file_pathE<gt>>
is a path to the localizable file to test this parser on.

Note that you can omit the C<< Serge::Engine::Plugin:: >> prefix of the parser class name
if your parser is located in lib/Serge/Engine/Plugin folder. Consider the following examples of C<E<lt>parser_nameE<gt>> parameter:

=over 8

=item B<parse_android>

Use C<< Serge::Engine::Plugin::parse_android >> parser class.

=item B<Serge::Engine::Plugin::parse_android>

Same as above (explicit class declaration).

=item B<ACME::Parser>

Use C<< ACME::Parser >> parser class.

=back

=head1 DESCRIPTION

Expand All @@ -26,7 +44,7 @@ With this option, parser will be told it works in import mode. In such mode
import-aware parsers are expected to extract translations rather than source strings,
and also skip missing translations.

=item B<--output-mode=mode>
=item B<--output-mode=E<lt>modeE<gt>>

By default, parsed data will be emitted in an easily readable Config::Neat format
(the format used in Serge configuration files). Such files are also suitable for diff.
Expand All @@ -41,6 +59,13 @@ for debugging.

=back

=item B<--data-file=E<lt>file_pathE<gt>>

Some parsers require configuration data. You can pass data structure to the parser
using this parameter. Target file should be a configuration file in Config::Neat format
with the parser data that you would usually put into C<< jobs E<gt> ... E<gt> parser E<gt>
data >> section of Serge configuration file.

=back

=head1 SEE ALSO
Expand Down
29 changes: 16 additions & 13 deletions lib/Serge/Command/test_parser.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package Serge::Command::test_parser;
use parent Serge::Command;
use parent Serge::Command, Serge::Interface::PluginHost;

use strict;

Expand All @@ -20,11 +20,15 @@ sub init {
$self->SUPER::init($command);

GetOptions(
"data-file:s" => \$self->{data_file},
"import-mode" => \$self->{import_mode},
"output-mode:s" => \$self->{output_mode},
) or die "Failed to parse some command-line parameters.";

$self->{parser} = shift @ARGV;
$self->{parser} = {
plugin => shift @ARGV
};

$self->{path} = shift @ARGV;
}

Expand All @@ -34,26 +38,25 @@ sub validate_data {
$self->SUPER::validate_data($command);

die "Unknown --output-mode value\n" unless $self->{output_mode} =~ m/^(|dumper)$/;
die "Please provide the name of the parser as a first argument to the command" unless defined $self->{parser};
die "Please provide the name of the parser as a first argument to the command" unless defined $self->{parser}->{plugin};
die "Please provide the path to the file as a second argument to the command" unless defined $self->{path};
die "The provided path doesn't point to a valid file" unless -f $self->{path};
die "The provided '--data-file' parameter doesn't point to a valid file" if ($self->{data_file} ne '') && (!-f $self->{data_file});

if ($self->{data_file} ne '') {
eval 'use Config::Neat::Inheritable';
my $c = Config::Neat::Inheritable->new();

$self->{parser}->{data} = $c->parse_file($self->{data_file});
}
}

sub run {
my ($self, $command) = @_;

# Creating dummy engine object

my $engine = {
debug => undef,
};

# Loading parser plugin

my $parser;

eval('use Serge::Engine::Plugin::'.$self->{parser}.'; $parser = Serge::Engine::Plugin::'.$self->{parser}.'->new($engine);');
($@) && die "Can't load parser plugin [$self->{parser}]: $@";
my $parser = $self->load_plugin_from_node('Serge::Engine::Plugin', $self->{parser});

print "Using plugin: ".$parser->name."\n";

Expand Down

0 comments on commit 364decc

Please sign in to comment.