Skip to content

Commit

Permalink
Adding slave context delete ability
Browse files Browse the repository at this point in the history
- Fixes #20
  • Loading branch information
Galen Collins committed Jun 5, 2013
1 parent f7575ea commit d98b372
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pymodbus/datastore/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,28 @@ def __contains__(self, slave):
return slave in self.__slaves

def __setitem__(self, slave, context):
''' Wrapper used to access the slave context
''' Used to set a new slave context
:param slave: slave The context to set
:param slave: The slave context to set
:param context: The new context to set for this slave
'''
if self.single: slave = Defaults.UnitId
if 0xf7 >= slave >= 0x00:
self.__slaves[slave] = context
else: raise ParameterException('slave index out of range')

def __getitem__(self, slave):
def __delitem__(self, slave):
''' Wrapper used to access the slave context
:param slave: The slave context to remove
'''
if not self.single and (0xf7 >= slave >= 0x00):
del self.__slaves[slave]
else: raise ParameterException('slave index out of range')

def __getitem__(self, slave):
''' Used to get access to a slave context
:param slave: The slave context to get
:returns: The requested slave context
'''
Expand Down
11 changes: 11 additions & 0 deletions test/test_server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def testSingleContextGets(self):
for id in xrange(0, 0xff):
self.assertEqual(self.slave, self.context[id])

def testSingleContextDeletes(self):
''' Test removing on multiple context '''
def _test():
del self.context[0x00]
self.assertRaises(ParameterException, _test)

def testSingleContextIter(self):
''' Test iterating over a single context '''
expected = (0, self.slave)
Expand Down Expand Up @@ -60,6 +66,11 @@ def testMultipleContextGets(self):
for id in xrange(0, 10):
self.assertEqual(self.slaves[id], self.context[id])

def testMultipleContextDeletes(self):
''' Test removing on multiple context '''
del self.context[0x00]
self.assertRaises(ParameterException, lambda: self.context[0x00])

def testMultipleContextIter(self):
''' Test iterating over multiple context '''
for id, slave in self.context:
Expand Down

0 comments on commit d98b372

Please sign in to comment.