Skip to content

Commit

Permalink
Fix some lint bugs, and move to version 1.9.9
Browse files Browse the repository at this point in the history
Replace some bare exceptions with UnicodeEncodeError catches.
Also, reindent and refactor a few lines to get rid of flake8 and
pylint errors.  Also, add some comments with inline pylint
directives to disable some false-positive errors.

Signed-off-by: Tim Bird <[email protected]>
  • Loading branch information
tbird20d committed Sep 3, 2019
1 parent dfa9074 commit 5c8f2ef
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions grabserial
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# See the LICENSE file, which should have accompanied this program,
# for the text of the license.
#
# 2018-08-20 by Tim Bird <[email protected]>
# 2019-09-03 by Tim Bird <[email protected]>
# 2006-09-07 by Tim Bird
#
# To do:
Expand All @@ -23,6 +23,10 @@
# * restart based on received bytes?
#
# CHANGELOG:
# 2019.09.03 - Version 1.9.9
# - fixed a bunch of pylint errors, and disabled some false positives
# with inline pylint directives
# - this included replacing some bare exceptions with UnicodeEncodeError
# 2018.08.20 - Version 1.9.8
# - try to fix unicode handling (yet again)
# - some work based on pull request submitted by 'modbw' on github
Expand Down Expand Up @@ -77,15 +81,16 @@ import time
import datetime
import re

VERSION = (1, 9, 8)

try:
import thread
except ImportError:
import _thread as thread

verbose = 0
cmdinput = u""
VERSION = (1, 9, 9)


verbose = 0 # pylint: disable=I0011,C0103
cmdinput = u"" # pylint: disable=I0011,C0103


def vprint(message):
Expand Down Expand Up @@ -163,7 +168,7 @@ def device_exists(device):


def read_input():
global cmdinput
global cmdinput # pylint: disable=I0011,C0103

# NOTE: cmdinput is in unicode (to make handling similar between
# python2 and python3)
Expand All @@ -183,7 +188,7 @@ def read_input():
# https://www.python.org/dev/peps/pep-3111/
# input() returns string in unicode already
try:
cmdinput = input()
cmdinput = input() # pylint: disable=I0011,W0141
except EOFError:
break

Expand All @@ -201,39 +206,39 @@ def read_input():
# would only make sense if you specified another out_filename with
# "-o","myoutputfilename"
def grab(arglist, outputfd=sys.stdout):
global verbose
global cmdinput
global verbose # pylint: disable=I0011,C0103
global cmdinput # pylint: disable=I0011,C0103

# parse the command line options
try:
opts, args = getopt.getopt(
arglist,
"hli:d:b:B:w:p:s:xrfc:taTm:e:o:QvVq:S", [
"help",
"launchtime",
"instantpat=",
"device=",
"baudrate=",
"width=",
"parity=",
"stopbits=",
"xonxoff",
"rtscts",
"force-reset",
"command=",
"time",
"again",
"systime",
"match=",
"endtime=",
"output=",
"quiet",
"verbose",
"version",
"quitpat=",
"skip",
"crtonewline",
])
arglist,
"hli:d:b:B:w:p:s:xrfc:taTm:e:o:QvVq:S", [
"help",
"launchtime",
"instantpat=",
"device=",
"baudrate=",
"width=",
"parity=",
"stopbits=",
"xonxoff",
"rtscts",
"force-reset",
"command=",
"time",
"again",
"systime",
"match=",
"endtime=",
"output=",
"quiet",
"verbose",
"version",
"quitpat=",
"skip",
"crtonewline",
])
except getopt.GetoptError:
# print help info and exit
print("Error parsing command line options")
Expand Down Expand Up @@ -373,7 +378,7 @@ def grab(arglist, outputfd=sys.stdout):
vprint(
"%d:%d%s%s:xonxoff=%d:rtscts=%d" %
(sd.baudrate, sd.bytesize, sd.parity, sd.stopbits,
sd.xonxoff, sd.rtscts))
sd.xonxoff, sd.rtscts))
else:
print("Error: Missing serial port to read from")
usage(2)
Expand All @@ -388,7 +393,8 @@ def grab(arglist, outputfd=sys.stdout):
if basepat:
vprint("Matching pattern '%s' to set base time" % basepat)
if instantpat:
vprint("Instant pattern '%s' to report time of at end of run" % instantpat)
vprint("Instant pattern '%s' to report time of at end of run"
% instantpat)
if quitpat:
vprint("Instant pattern '%s' to exit program" % quitpat)
if skip_device_check:
Expand Down Expand Up @@ -486,10 +492,10 @@ def grab(arglist, outputfd=sys.stdout):
if out:
try:
out.write(msg.encode(sys.stdout.encoding))
except:
except UnicodeEncodeError:
try:
out.write(msg.encode("utf8"))
except:
except UnicodeEncodeError:
out.write(msg)

prev1 = elapsed
Expand All @@ -506,10 +512,10 @@ def grab(arglist, outputfd=sys.stdout):
if out:
try:
out.write(msg.encode(sys.stdout.encoding))
except:
except UnicodeEncodeError:
try:
out.write(msg.encode("utf8"))
except:
except UnicodeEncodeError:
out.write(msg)

prev1 = elapsed
Expand Down Expand Up @@ -559,7 +565,8 @@ def grab(arglist, outputfd=sys.stdout):
out.close()
sd.close()
os.execv(sys.executable, ['python'] + sys.argv)
stop_reason = "grabserial stopped because quit pattern '%s' was found" % quitpat
stop_reason = "grabserial stopped because quit pattern '" + \
quitpat + "' was found"
break

if x == b"\n":
Expand Down Expand Up @@ -591,10 +598,10 @@ def grab(arglist, outputfd=sys.stdout):
if out:
try:
out.write(msg.encode(sys.stdout.encoding))
except:
except UnicodeEncodeError:
try:
out.write(msg.encode("utf8"))
except:
except UnicodeEncodeError:
out.write(msg)
out.flush()

Expand Down

0 comments on commit 5c8f2ef

Please sign in to comment.