aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaarten Vanraes <alien@mageia.org>2016-08-06 13:48:01 +0200
committerMaarten Vanraes <alien@mageia.org>2016-08-06 13:48:01 +0200
commit8ac2eb6482aac96305d9f893f7cfd0ff175dd27a (patch)
tree6dba2dc4d78c31411192d6335eb399f014134fdc /lib
parent2080e53f656bc36196247da694cfb962edade214 (diff)
downloadmanatools-8ac2eb6482aac96305d9f893f7cfd0ff175dd27a.tar
manatools-8ac2eb6482aac96305d9f893f7cfd0ff175dd27a.tar.gz
manatools-8ac2eb6482aac96305d9f893f7cfd0ff175dd27a.tar.bz2
manatools-8ac2eb6482aac96305d9f893f7cfd0ff175dd27a.tar.xz
manatools-8ac2eb6482aac96305d9f893f7cfd0ff175dd27a.zip
Plugin: add a tool_columns helper function
Diffstat (limited to 'lib')
-rw-r--r--lib/ManaTools/Shared/disk_backend/Plugin.pm81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/ManaTools/Shared/disk_backend/Plugin.pm b/lib/ManaTools/Shared/disk_backend/Plugin.pm
index fc1dd7ee..e9a6cdbb 100644
--- a/lib/ManaTools/Shared/disk_backend/Plugin.pm
+++ b/lib/ManaTools/Shared/disk_backend/Plugin.pm
@@ -347,4 +347,85 @@ sub tool_fields {
return %fields;
}
+#=============================================================
+
+=head2 tool_columns
+
+=head3 INPUT
+
+ $toolname: Str
+ $headers: Bool
+ $ignores: Int
+ $identifier: Int
+ $separator: Str
+ @args: Array[Str]
+
+=head3 OUTPUT
+
+ HashRef[HashRef]|undef
+
+=head3 DESCRIPTION
+
+ this is a default method for executing a tool and getting all the STDOUT
+ in a HASH depending on the separator when it's a column-based output
+
+=cut
+
+#=============================================================
+sub tool_columns {
+ my $self = shift;
+ my $toolname = shift;
+ my $headers = shift;
+ my $ignores = shift;
+ my $identifier = shift;
+ my $separator = shift;
+ my @args = @_;
+ my $fields = {};
+
+ # get lines from tool
+ my @lines = $self->tool_lines($toolname, @args);
+ return $fields if scalar(@lines) < ($ignores + !!$headers);
+ my @headers = ();
+ # get headers
+ my $line = shift(@lines);
+ @headers = split($separator, $line) if $headers;
+ # ignore lines if needed
+ for (my $i = 0; $i < $ignores; $i = $i + 1) {
+ shift(@lines);
+ }
+ # loop the data
+ for my $line (@lines) {
+ my $item = {};
+
+ # split into key & value
+ my @value = split($separator, $line);
+
+ # if no 2 columns, skip this line
+ next if (scalar(@value) < 2);
+
+ my $i = 0;
+ for my $value (@value) {
+ my $key = $i;
+ $key = $headers[$i] if (defined $headers[$i]);
+
+ # trim key & value
+ $key =~ s/^\s+//;
+ $key =~ s/\s+$//;
+ $value =~ s/^\s+//;
+ $value =~ s/\s+$//;
+
+ # need to be the index if key is empty after all
+ $key = $i if ($key eq '');
+
+ # set the field
+ $item->{$key} = $value;
+
+ # next field
+ $i = $i + 1;
+ }
+ $fields->{$item->{$identifier}} = $item if defined($item->{$identifier});
+ }
+ return $fields;
+}
+
1;