File: striplog

package info (click to toggle)
gpsd 3.27-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,056 kB
  • sloc: ansic: 74,438; python: 16,521; sh: 890; cpp: 848; php: 225; makefile: 197; perl: 111; javascript: 26; xml: 11
file content (61 lines) | stat: -rwxr-xr-x 1,699 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
#
# striplog -- strip leading lines from logs
#
# striplog -j strips JSON leader sentences.
# striplog with no option strips all leading lines beginning with #
#
# This file is Copyright 2010 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import getopt
import sys

secondline = firstline = stripjson = False
stripval = 0
(options, arguments) = getopt.getopt(sys.argv[1:], "12n:j")
for (switch, val) in options:
    if (switch == '-1'):
        firstline = True
    if (switch == '-2'):
        secondline = True
    if (switch == '-n'):
        stripval = int(val)
    if (switch == '-j'):
        stripjson = True

try:
    if firstline:
        sys.stdin.readline()
    elif secondline:
        sys.stdin.readline()
        sys.stdin.readline()
    elif stripval:
        for _dummy in range(stripval):
            sys.stdin.readline()
    elif stripjson:
        while True:
            line = sys.stdin.readline()
            if ((line.startswith('{"class":"VERSION"') or
                 line.startswith('{"class":"DEVICE"') or
                 line.startswith('{"class":"DEVICES"') or
                 line.startswith('{"class":"WATCH"'))):
                continue
            else:
                break
        sys.stdout.write(line)
    else:
        while True:
            line = sys.stdin.readline()
            if line[0] != '#':
                break
        sys.stdout.write(line)

    sys.stdout.write(sys.stdin.read())
except KeyboardInterrupt:
    pass
# vim: set expandtab shiftwidth=4