12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #!/usr/bin/env python
- #
- # 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 (c) 2010-2019 by the GPSD project
- # BSD terms apply: see the file COPYING in the distribution root for details.
- #
- # 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
|