blib/lib/PMLTQ/Commands.pm

filter: Statementstmt 28.0% 30 / 107 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 52.9% 9 / 17 Podpod 0.0% 0 / 2 totaltotal 17.8% 39 / 219 CC 37 SCAR 50.3
CC
37
Cov
16%
_load_config
380.0
run
56.0
_parse_args
56.0
CRAP 153.7
« blib/lib/PMLTQ/Relation/DepthFirstRangeIterator.pm ↑ Summary blib/lib/PMLTQ/Relation/PDT.pm »
1 package PMLTQ::Commands;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::Commands::VERSION = '1.5.0';
4 # ABSTRACT: PMLTQ command line interface
5  
6 1 ▶use PMLTQ::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Cwd qw/getcwd abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Hash::Merge 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::MoreUtils 'apply';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use PMLTQ::Loader qw/find_modules load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub DEFAULT_CONFIG {
18 0 ▶ my $base_dir = shift || getcwd();
Subroutine DEFAULT_CONFIG: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
shift() || getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || getcwd()
shift() getcwd()
Truth table0%0/3
A || B
A= shift()B= getcwd()
ABresult
1X1
011
000
19 return {
20 0 data_dir => File::Spec->catdir( $base_dir, 'data' ),
21 output_dir => File::Spec->catdir( $base_dir, 'sql_dump' ),
22 resources => File::Spec->catdir( $base_dir, 'resources' ),
23 db => {
24 host => 'localhost',
25 port => 5432
26 },
27 isFree => 'false',
28 isAllLogged => 'false',
29 isPublic => 'false',
30 isFeatured => 'false',
31 info => {
32 fields => 'name'
33 }
34 };
35 }
36  
37 sub run {
38 0 ▶ my ( $self, $name, @args ) = @_;
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
39  
40 0 ▶ if ( $name && $name =~ /^\w+$/ && ( $name ne 'help' || $args[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and $name =~ /^\w+$/ and $name ne "help" || $args[0]) 0 0 0
Condition0%0/9
$name and $name =~ /^\w+$/ and $name ne "help" || $args[0]
!ll&&!rl&&r
000
$name and $name =~ /^\w+$/
!ll&&!rl&&r
000
$name ne "help" or $args[0]
l!l&&r!l&&!r
000
MC/DC0%0/4
$name and $name =~ /^\w+$/ and $name ne "help" || $args[0]
$name $name =~ /^\w+$/ $name ne "help" $args[0]
Truth table0%0/5
A and B and C or D
A= $nameB= $name =~ /^\w+$/C= $name ne "help"D= $args[0]
ABCDresult
0XXX0
10XX0
11000
111X1
11011
41 0 ▶ $name = shift @args if my $help = $name eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if my $help = $name eq "help" 0 0 0
42  
43 0 my $command = _command( "PMLTQ::Command::$name", \@args );
44 0 ▶ return $help ? $command->help : $command->run(@args);
Branch: 1 branch
BranchTrueFalseTotal
$help ? : 0 0 0
45 }
46 0 print "Available commands:\n\t", join( "\n\t", sort { $a cmp $b } _available_commands() ), "\n";
47 }
48  
49 sub _available_commands {
50 0 ▶ apply {s/^PMLTQ::Command:://} find_modules('PMLTQ::Command');
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _available_commands: not called CC=1 SCAR=6.9
51 }
52  
53 sub _command {
54 0 ▶ my ( $module, $args ) = @_;
Subroutine _command: not called CC=3 SCAR=24.8
55  
56 0 ▶ die qq{Unknown command "$module", maybe you need to install it?\n} unless load_class($module);
Branch: 1 branch
BranchTrueFalseTotal
unless load_class($module) 0 0 0
57 0 ▶ die qq{Command doesn't inherit from PMLTQ::Command} unless $module->isa('PMLTQ::Command');
Branch: 1 branch
BranchTrueFalseTotal
unless $module->isa("PMLTQ::Command") 0 0 0
58  
59 0 my $config = _parse_args($args);
60 0 return $module->new( config => $config );
61 }
62  
63 sub _parse_args {
64 0 ▶ my $args = shift;
Subroutine _parse_args: not called CC=7 SCAR=40.3
65 0 my $p = Getopt::Long::Parser->new( config => [qw/pass_through no_ignore_case no_auto_abbrev/] );
66 0 my $config_file = '';
67 0 my $config = {};
68  
69 0 my $command_line_config = {};
70 0 my @unprocessed_args = ();
71  
72 $p->getoptionsfromarray(
73 $args,
74 'c|config=s' => \$config_file,
75 '<>' => sub {
76 0 ▶ my $arg = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
77 0 my ( $path, $value ) = $arg =~ m/^--([a-z0-9-_]+)=(.*)$/;
78 0 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
79 0 push @unprocessed_args, $arg; # push back to args
80 0 return;
81 }
82  
83 0 my @path = split /-/, $path;
84 0 my $name = pop @path;
85 0 my $ref = $command_line_config;
86 0 ▶ while ( my $part = shift @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = shift @path) 0 0 0
87 0 ▶ $ref->{$part} = {} unless defined $ref->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref->{$part} 0 0 0
88 0 $ref = $ref->{$part};
89 }
90 0 $ref->{$name} = $value;
91 }
92 0 );
93  
94 0 ▶ if ( $config_file ne '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_file ne "--") 0 0 0
95 0 ▶ if ($config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_file) { } 0 0 0
96 0 ▶ die "Configuration file '$config_file' does not exists or is not readable" unless -r $config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $config_file 0 0 0
97 } else {
98 0 $config_file = File::Spec->catfile( getcwd(), 'pmltq.yml' );
99 0 ▶ $config_file = undef unless -r $config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $config_file 0 0 0
100 }
101 }
102  
103 0 ▶ push @$args, @unprocessed_args if @unprocessed_args > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @unprocessed_args > 0 0 0 0
104  
105 0 ▶ $config = _load_config($config_file) if $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if $config_file 0 0 0
106  
107 0 return merge( $command_line_config, merge( $config, DEFAULT_CONFIG ) );
108 }
109  
110 sub _load_config {
111 0 ▶ my $config_file = shift;
Subroutine _load_config: not called CC=19 SCAR=59.4
112 0 my $data;
113 my $yaml_str;
114 0 ▶ if ( $config_file eq '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_file eq '--') { } 0 0 0
115 0 $yaml_str = do {
116 0 local $/;
117 0 <STDIN>;
118 };
119 0 eval { $data = YAML::Tiny->read_string($yaml_str) };
120 } else {
121 0 eval { $data = YAML::Tiny->read($config_file) };
122 }
123 0 ▶ if ( $@ && $@ =~ m/YAML_LOAD_ERR/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($@ and $@ =~ /YAML_LOAD_ERR/) { } 0 0 0
elsif ($@ and $@ =~ /YAML_PARSE_ERR/) { } 0 0 0
elsif ($config_file eq '--' and not $data) { } 0 0 0
elsif (not $data) { } 0 0 0
Condition0%0/3
$@ and $@ =~ /YAML_LOAD_ERR/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $@ =~ /YAML_LOAD_ERR/
$@ $@ =~ /YAML_LOAD_ERR/
Truth table0%0/3
A and B
A= $@B= $@ =~ /YAML_LOAD_ERR/
ABresult
0X0
100
111
124 0 die "Unable to load config file '$config_file'\n";
125 ▶ } elsif ( $@ && $@ =~ m/YAML_PARSE_ERR/ ) {
Condition0%0/3
$@ and $@ =~ /YAML_PARSE_ERR/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $@ =~ /YAML_PARSE_ERR/
$@ $@ =~ /YAML_PARSE_ERR/
Truth table0%0/3
A and B
A= $@B= $@ =~ /YAML_PARSE_ERR/
ABresult
0X0
100
111
126 0 $@ =~ s/\n.*//g;
127 0 die "Unable to parse config file '$config_file'\n\t$@\n";
128 ▶ } elsif ( $config_file eq '--' && !$data ) {
Condition0%0/3
$config_file eq "--" and not $data
!ll&&!rl&&r
000
MC/DC0%0/2
$config_file eq "--" and not $data
$config_file eq "--" not $data
Truth table0%0/3
A and not(B)
A= $config_file eq "--"B= not $data
ABresult
0X0
100
111
129 0 die "Unable to parse config from STDIN:\n$yaml_str\n";
130 } elsif ( !$data ) {
131 0 die "Unable to open config file '$config_file'\n";
132 }
133  
134 0 my $config = $data->[0];
135  
136 0 my $base_dir = $config->{base_dir};
137 0 ▶ unless ($base_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_dir) 0 0 0
138 0 ( undef, $base_dir, undef ) = fileparse($config_file);
139 0 $base_dir = abs_path($base_dir);
140 }
141  
142 0 ▶ $config->{db} = {} unless $config->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"db"} 0 0 0
143 0 ▶ $config->{db}->{name} = $config->{treebank_id} if ( $config->{treebank_id} && !$config->{db}->{name} );
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"treebank_id"} and not $config->{"db"}{"name"} 0 0 0
Condition0%0/3
$config->{"treebank_id"} and not $config->{"db"}{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"treebank_id"} and not $config->{"db"}{"name"}
$config->{"treebank_id"} not $config->{"db"}{"name"}
Truth table0%0/3
A and not(B)
A= $config->{"treebank_id"}B= not $config->{"db"}{"name"}
ABresult
0X0
100
111
144  
145 0 for ( grep { $config->{$_} } qw/data_dir resources output_dir/ ) {
146 0 $config->{$_} = abs_path( File::Spec->rel2abs( $config->{$_}, $base_dir ) );
147 }
148  
149 0 ▶ if ( $config->{layers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"layers"}) 0 0 0
150 0 for my $lr ( @{ $config->{layers} } ) {
151 $lr->{'related-schema'} =
152 0 [ map { abs_path( File::Spec->rel2abs( $_, $config->{resources} ) ) } @{ $lr->{'related-schema'} } ]
153 0 ▶ if $lr->{'related-schema'};
Branch: 1 branch
BranchTrueFalseTotal
if $lr->{"related-schema"} 0 0 0
154 $lr->{filelist} = abs_path( File::Spec->rel2abs( $lr->{filelist}, $base_dir ) )
155 0 ▶ if $lr->{filelist} && !File::Spec->file_name_is_absolute( $lr->{filelist} );
Branch: 1 branch
BranchTrueFalseTotal
if $lr->{"filelist"} and not "File::Spec"->file_name_is_absolute($lr->{"filelist"}) 0 0 0
Condition0%0/3
$lr->{"filelist"} and not "File::Spec"->file_name_is_absolute($lr->{"filelist"})
!ll&&!rl&&r
000
MC/DC0%0/2
$lr->{"filelist"} and not "File::Spec"->file_name_is_absolute($lr->{"filelist"})
$lr->{"filelist"} not "File::Spec"->file_name_is_absolute($lr->{"filelist"})
Truth table0%0/3
A and not(B)
A= $lr->{"filelist"}B= not "File::Spec"->file_name_is_absolute($lr->{"filelist"})
ABresult
0X0
100
111
156 }
157 }
158  
159 0 return merge( $config, DEFAULT_CONFIG($base_dir) );
160 }
161  
162 1 1;
163  
164 __END__
« blib/lib/PMLTQ/Relation/DepthFirstRangeIterator.pm ↑ Summary blib/lib/PMLTQ/Relation/PDT.pm »