aboutsummaryrefslogtreecommitdiff
path: root/serial/tools/hexlify_codec.py
diff options
context:
space:
mode:
authorChris Liechti <cliechti@gmx.net>2016-02-19 23:54:14 +0100
committerChris Liechti <cliechti@gmx.net>2016-02-19 23:54:14 +0100
commit4e34c4cb92bfafa0471a83e37f117db57632db06 (patch)
treedd3a36c8b8748b0e696336c94d5b4f11522f5560 /serial/tools/hexlify_codec.py
parent277220e967ac648911570c5c64b578d2ca9ae3f6 (diff)
downloadpyserial-4e34c4cb92bfafa0471a83e37f117db57632db06.tar.gz
doc: add doc comments
Diffstat (limited to 'serial/tools/hexlify_codec.py')
-rw-r--r--serial/tools/hexlify_codec.py52
1 files changed, 35 insertions, 17 deletions
diff --git a/serial/tools/hexlify_codec.py b/serial/tools/hexlify_codec.py
index 6d42080..f8449e2 100644
--- a/serial/tools/hexlify_codec.py
+++ b/serial/tools/hexlify_codec.py
@@ -3,11 +3,19 @@
# This is a codec to create and decode hexdumps with spaces between characters. used by miniterm.
#
# This file is part of pySerial. https://github.com/pyserial/pyserial
-# (C) 2011 Chris Liechti <cliechti@gmx.net>
+# (C) 2015-2016 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier: BSD-3-Clause
"""\
Python 'hex' Codec - 2-digit hex with spaces content transfer encoding.
+
+Encode and decode may be a bit missleading at first sight...
+
+The textual representation is a hex dump: e.g. "40 41"
+The "encoded" data of this is the binary form, e.g. b"@A"
+
+Therefore decoding is binary to text and thus converting binary data to hex dump.
+
"""
import codecs
@@ -18,23 +26,28 @@ HEXDIGITS = '0123456789ABCDEF'
# Codec APIs
-def hex_encode(input, errors='strict'):
- return (serial.to_bytes([int(h, 16) for h in input.split()]), len(input))
+def hex_encode(data, errors='strict'):
+ """'40 41 42' -> '@ab'"""
+ return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
-def hex_decode(input, errors='strict'):
- return (''.join('{:02X} '.format(b) for b in input), len(input))
+def hex_decode(data, errors='strict'):
+ """'@ab' -> '40 41 42'"""
+ return (''.join('{:02X} '.format(b) for b in data), len(data))
class Codec(codecs.Codec):
- def encode(self, input, errors='strict'):
- return serial.to_bytes([int(h, 16) for h in input.split()])
+ def encode(self, data, errors='strict'):
+ """'40 41 42' -> '@ab'"""
+ return serial.to_bytes([int(h, 16) for h in data.split()])
- def decode(self, input, errors='strict'):
- return ''.join('{:02X} '.format(b) for b in input)
+ def decode(self, data, errors='strict'):
+ """'@ab' -> '40 41 42'"""
+ return ''.join('{:02X} '.format(b) for b in data)
class IncrementalEncoder(codecs.IncrementalEncoder):
+ """Incremental hex encoder"""
def __init__(self, errors='strict'):
self.errors = errors
@@ -49,10 +62,15 @@ class IncrementalEncoder(codecs.IncrementalEncoder):
def setstate(self, state):
self.state = state
- def encode(self, input, final=False):
+ def encode(self, data, final=False):
+ """\
+ Incremental encode, keep track of digits and emit a byte when a pair
+ of hex digits is found. The space is optional unless the error
+ handling is defined to be 'strict'.
+ """
state = self.state
encoded = []
- for c in input.upper():
+ for c in data.upper():
if c in HEXDIGITS:
z = HEXDIGITS.index(c)
if state:
@@ -72,21 +90,21 @@ class IncrementalEncoder(codecs.IncrementalEncoder):
class IncrementalDecoder(codecs.IncrementalDecoder):
- def decode(self, input, final=False):
- return ''.join('{:02X} '.format(b) for b in input)
+ """Incremental decoder"""
+ def decode(self, data, final=False):
+ return ''.join('{:02X} '.format(b) for b in data)
class StreamWriter(Codec, codecs.StreamWriter):
- pass
+ """Combination of hexlify codec and StreamWriter"""
class StreamReader(Codec, codecs.StreamReader):
- pass
-
+ """Combination of hexlify codec and StreamReader"""
-# encodings module API
def getregentry():
+ """encodings module API"""
return codecs.CodecInfo(
name='hexlify',
encode=hex_encode,