File: Parser.pm

package info (click to toggle)
vnlog 1.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 992 kB
  • sloc: perl: 4,682; ansic: 752; python: 462; sh: 116; makefile: 7
file content (219 lines) | stat: -rw-r--r-- 4,677 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package Vnlog::Parser;

use strict;
use warnings;
use feature ':5.10';


our $VERSION = 1.00;
use base 'Exporter';
our @EXPORT_OK = qw();

sub new
{
    my $classname = shift;

    my $this = { 'keys'        => undef,
                 'values'      => undef,
                 'error'       => '',
                 'values_hash' => undef};
    bless($this, $classname);
    return $this;
}

sub parse
{
    my ($this, $line) = @_;

    # I reset the data first
    $this->{values}      = undef;
    $this->{values_hash} = undef;

    if( $line =~ /^\s*(?:#[#!]|#\s*$|$)/p)
    {
        # empty line or hard comment.
        # no data, no error
        return 1;
    }

    if( $line =~ /^\s*#\s*(.*?)\s*$/ )
    {
        if( $this->{keys} )
        {
            # already have legend, so this is just a comment
            # no data, no error
            return 1;
        }

        # got legend.
        # no data, no error
        $this->{keys} = [ split(' ', $1) ];
        return 1;
    }

    if( !$this->{'keys'} )
    {
        # Not comment, not empty line, but no legend yet. Barf
        $this->{error} = "Got dataline before legend";
        return undef;
    }

    $line =~ /^\s*(.*?)\s*$/; # get the non-newline part. Like chomp, but
                              # non-destructive
    $this->{values} = [ map {$_ eq '-' ? undef : $_} split(' ', $1) ];
    if( scalar @{$this->{'keys'}} != scalar @{$this->{'values'}} )
    {
        # mismatched key/value counts
        $this->{error} = sprintf('Legend line "%s" has %d elements, but data line "%s" has %d elements. Counts must match!',
                                 "# " . join(' ', @{$this->{'keys'}}),
                                 scalar @{$this->{'keys'}},
                                 $line,
                                 scalar @{$this->{'values'}});
        return undef;
    }

    return 1;
}

sub error
{
    my ($this) = @_;
    return $this->{error};
}

sub getKeys
{
    my ($this) = @_;
    return $this->{keys};
}

sub getValues
{
    my ($this) = @_;
    return $this->{values}
}

sub getValuesHash
{
    my ($this) = @_;

    # internally:
    #   $this->{values_hash} == undef:  not yet computed
    #   $this->{values_hash} == {}:     computed, but no-data
    # returning: undef if computed, but no-data

    if( defined $this->{values_hash} )
    {
        return undef if 0 == scalar(%{$this->{values_hash}});
        return $this->{values_hash};
    }

    $this->{values_hash} = {};
    if($this->{keys} && $this->{values})
    {
        for my $i (0..$#{$this->{keys}})
        {
            $this->{values_hash}{$this->{keys}[$i]} = $this->{values}[$i];
        }
    }
    return $this->{values_hash};
}

1;

=head1 NAME

Vnlog::Parser - Simple library to parse vnlog data

=head1 SYNOPSIS

 use Vnlog::Parser;

 my $parser = Vnlog::Parser->new();
 while (<DATA>)
 {
     if( !$parser->parse($_) )
     {
         die "Error parsing vnlog line '$_': " . $parser->error();
     }

     my $d = $parser->getValuesHash();
     next unless %$d;

     say "$d->{time}: $d->{height}";
 }


=head1 DESCRIPTION

This is a simple perl script to parse vnlog input and make the incoming
key/values available. The example above is representative of normal use. API
functions are

=over

=item *

new()

Creates new Vnlog::Parser object. Takes no arguments.

=item *

parse(line)

Method to call for each input line. On error, a false value is returned.

=item *

error()

If an error occurred, returns a string that describes the error.

=item *

getKeys()

Returns a list-ref containing the current column labels or undef if this hasn't
been parsed yet.

=item *

getValues()

Returns a list-ref containing the values for the current line or undef if there
aren't any. This isn't an error necessarily because this line could have been a
comment. Empty fields are '-' in the vnlog and undef in the values returned
here.

=item *

getValuesHash()

Returns a hash-ref containing the key-value mapping for the current line or
undef if there's no data in this line. This isn't an error necessarily because
this line could have been a comment. Empty fields are '-' in the vnlog and undef
in the values returned here.

=item *

=back

=head1 REPOSITORY

L<https://github.com/dkogan/vnlog>

=head1 AUTHOR

Dima Kogan, C<< <dima@secretsauce.net> >>

=head1 LICENSE AND COPYRIGHT

Copyright 2016 California Institute of Technology.

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

=cut