Skip to content

Commit daba406

Browse files
committed
Restore HART tests by fixing enip_format call (and fix indentation)
1 parent 6c27c57 commit daba406

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

server/enip/hart_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def hart_pass_thru( io, path, hart_data, data_size, route_path=None ):
286286
#
287287
# Since we implemented STRUCT tags, we need to update the HART implementation.
288288
#
289-
@pytest.mark.xfail
289+
#@pytest.mark.xfail
290290
def test_hart_pass_thru_poll( simulated_hart_gateway ):
291291
r"""To test a remote C*Logix w/ a HART card, set up a remote port forward from another host in the
292292
same LAN. Here's a windows example, using putty. This windows machine (at 100.100.102.1)
@@ -376,12 +376,12 @@ def test_hart_pass_thru_poll( simulated_hart_gateway ):
376376
hio, path=path, hart_data=[3, 0], route_path=route_path, data_size=4*4 ) # with no size
377377

378378
# small response carries PV, SV, TV, FV values, no data types
379-
value = []
379+
value = cpppo.dotdict( current=[] )
380380
if data and len( data ) == 4*4:
381381
# Short
382382
packer = struct.Struct( enip.REAL_network.struct_format )
383383
for i in range( 0, len( data ), 4 ):
384-
value += packer.unpack_from( buffer=bytearray( data[i:i+4] ))
384+
value.current += packer.unpack_from( buffer=bytearray( data[i:i+4] ))
385385
elif data and len( data ) >= 24:
386386
# Long
387387
packer = struct.Struct( enip.REAL_network.struct_format )
@@ -580,7 +580,7 @@ def test_hart_pass_thru_poll( simulated_hart_gateway ):
580580
),
581581
]
582582

583-
#@pytest.mark.xfail
583+
584584
def test_CIP_hart( repeat=1 ):
585585
"""HART protocol enip CIP messages
586586
"""

server/enip/parser.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -651,62 +651,64 @@ def enip_format( data, sort_keys=False, indent=4 ):
651651
enip_format attempting to decode str as utf-8.
652652
653653
"""
654-
pairs = data.items()
654+
assert isinstance( data, dict ), \
655+
"Unknown data type {data!r}".format( data=data )
656+
pairs = data.items()
655657
if sort_keys:
656-
pairs = sorted( pairs )
657-
prefix = ' ' * indent
658-
newline = '\n' + prefix
659-
result = '{'
658+
pairs = sorted( pairs )
659+
prefix = ' ' * indent
660+
newline = '\n' + prefix
661+
result = '{'
660662
for key,val in pairs:
661-
result += newline + "{key:32}".format( key=repr( key ) + ': ' )
663+
result += newline + "{key:32}".format( key=repr( key ) + ': ' )
662664
if isinstance( val, bytes ) and sys.version_info[0] < 3: # Python2: str; very ambiguous
663665
if not any( c < ' ' or c > '~' for c in val ):
664666
result += repr( val ) + ',' # '...',
665667
continue
666668
try:
667669
if not any( c < ' ' for c in val ):
668-
result += repr( val.decode( 'utf-8' )) + ',' # Python2: u"...", Python3: "..."
670+
result += repr( val.decode( 'utf-8' )) + ',' # Python2: u"...", Python3: "..."
669671
continue
670672
except:
671673
pass
672674
# Probably binary data in bytes; fall thru...
673675
try:
674-
binary = octets_encode( val )
676+
binary = octets_encode( val )
675677
except:
676678
pass
677679
else:
678680
# Yes, some binary data container
679681
if isinstance( val, array.array ):
680-
beg,end = 'array( {val.typecode!r}, '.format( val=val ),')'
682+
beg,end = 'array( {val.typecode!r}, '.format( val=val ),')'
681683
elif isinstance( val, bytearray ):
682-
beg,end = 'bytearray(',')'
684+
beg,end = 'bytearray(',')'
683685
else:
684-
beg,end = 'bytes(',')'
685-
result += "{beg}hexload('''".format( beg=beg )
686-
result += ''.join( newline + prefix + row for row in misc.hexdumper( val ))
687-
result += newline + "'''){end},".format( end=end )
686+
beg,end = 'bytes(',')'
687+
result += "{beg}hexload('''".format( beg=beg )
688+
result += ''.join( newline + prefix + row for row in misc.hexdumper( val ))
689+
result += newline + "'''){end},".format( end=end )
688690
continue
689691

690692
if is_listlike( val ) and len( val ) > 10:
691693
# Try to tabularize large lists of data
692694
try:
693-
beg,end = getattr( getattr( val, '__class__' ), '__name__' ) + '(',')'
695+
beg,end = getattr( getattr( val, '__class__' ), '__name__' ) + '(',')'
694696
except:
695697
pass
696698
else:
697-
result += beg
699+
result += beg
698700
for i,v in enumerate( val ):
699701
if i%10 == 0:
700702
result += newline + prefix
701-
fmt = "{v:<8}" if isinstance( v, type_str_base ) else "{v:>8}"
702-
result += fmt.format( v=repr( v )+',' )
703-
result += newline + end + ','
703+
fmt = "{v:<8}" if isinstance( v, type_str_base ) else "{v:>8}"
704+
result += fmt.format( v=repr( v )+',' )
705+
result += newline + end + ','
704706
continue
705707

706708
# Other data types
707-
result += repr( val )
708-
result += ','
709-
result += '\n}'
709+
result += repr( val )
710+
result += ','
711+
result += '\n}'
710712
return result
711713

712714
#

0 commit comments

Comments
 (0)