diff options
author | Jeff Widman <jeff@jeffwidman.com> | 2020-09-20 22:44:41 -0700 |
---|---|---|
committer | Jeff Widman <jeff@jeffwidman.com> | 2020-09-26 04:38:18 +0000 |
commit | 8d7ebc732e93471ed88ff2759af0a0cef21a4771 (patch) | |
tree | be5d52d66d3a65df53e5831d62f8b18f3380a635 /tools/netscreen2dump.py | |
parent | c5926c51e79c9b3b620b9116ab7929afce97d18b (diff) |
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
Diffstat (limited to 'tools/netscreen2dump.py')
-rwxr-xr-x | tools/netscreen2dump.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/tools/netscreen2dump.py b/tools/netscreen2dump.py index 78312bc536..7aaac94b7d 100755 --- a/tools/netscreen2dump.py +++ b/tools/netscreen2dump.py @@ -13,6 +13,7 @@ import os import stat import time + class OutputFile: TIMER_MAX = 99999.9 @@ -28,11 +29,11 @@ class OutputFile: def PrintPacket(self, timestamp, datalines): # What do to with the timestamp? I need more data about what # the netscreen timestamp is, then I can generate one for the text file. -# print "TS:", timestamp.group("time") + # print("TS:", timestamp.group("time")) try: timestamp = float(timestamp.group("time")) except ValueError: - sys.exit("Unable to convert '%s' to floating point." % \ + sys.exit("Unable to convert '%s' to floating point." % (timestamp,)) # Did we wrap around the timeer max? @@ -63,12 +64,14 @@ class OutputFile: # Blank line print >> self.fh + # Find a timestamp line re_timestamp = re.compile(r"^(?P<time>\d+\.\d): [\w/]+\((?P<io>.)\)(:| len=)") # Find a hex dump line re_hex_line = re.compile(r"(?P<hex>([0-9a-f]{2} ){1,16})\s+(?P<ascii>.){1,16}") + def run(input_filename, output_filename): try: ifh = open(input_filename, "r") @@ -122,11 +125,13 @@ def usage(): print >> sys.stderr, "Usage: netscreen2dump.py netscreen-dump-file new-dump-file" sys.exit(1) + def main(): if len(sys.argv) != 3: usage() run(sys.argv[1], sys.argv[2]) + if __name__ == "__main__": main() |