home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / cgkit / GUI / idmanager.py < prev    next >
Encoding:
Python Source  |  2007-01-11  |  4.6 KB  |  135 lines

  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. #
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS IS" basis,
  10. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. # for the specific language governing rights and limitations under the
  12. # License.
  13. #
  14. # The Original Code is the Python Computer Graphics Kit.
  15. #
  16. # The Initial Developer of the Original Code is Matthias Baas.
  17. # Portions created by the Initial Developer are Copyright (C) 2004
  18. # the Initial Developer. All Rights Reserved.
  19. #
  20. # Contributor(s):
  21. #
  22. # Alternatively, the contents of this file may be used under the terms of
  23. # either the GNU General Public License Version 2 or later (the "GPL"), or
  24. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25. # in which case the provisions of the GPL or the LGPL are applicable instead
  26. # of those above. If you wish to allow use of your version of this file only
  27. # under the terms of either the GPL or the LGPL, and not to allow others to
  28. # use your version of this file under the terms of the MPL, indicate your
  29. # decision by deleting the provisions above and replace them with the notice
  30. # and other provisions required by the GPL or the LGPL. If you do not delete
  31. # the provisions above, a recipient may use your version of this file under
  32. # the terms of any one of the MPL, the GPL or the LGPL.
  33. #
  34. # ***** END LICENSE BLOCK *****
  35.  
  36. ## \file idmanager.py
  37. ## \brief Contains the IDManager class.
  38.  
  39. # Exceptions...
  40. class IDNotAllocated(Exception):
  41.     """Exception class."""
  42.     pass
  43.  
  44. # IDManager
  45. class IDManager:
  46.     """This class manages IDs.
  47.  
  48.     This class is used to generate IDs for GUI elements such as menu items.
  49.     You can call allocID() to get an unused ID and later on freeID() to
  50.     make the ID available again.
  51.     """
  52.     def __init__(self, startid=1):
  53.         """Constructor.
  54.  
  55.         \param startid (\c int) The starting value for the IDs (default: 1)
  56.         """
  57.         # Each interval is given by its start and its end (=last item+1)
  58.         self._intervals = [[startid,None]]
  59.         self._startid = startid
  60.  
  61.     def __call__(self):
  62.         return self.allocID()
  63.  
  64.     # allocID
  65.     def allocID(self):
  66.         """Request an ID.
  67.  
  68.         \return An unused ID (\c int).
  69.         """
  70.         id = self._intervals[0][0]
  71.         self._intervals[0][0]+=1
  72.         # Is the interval eaten up? then remove it
  73.         if self._intervals[0][0]==self._intervals[0][1]:
  74.             del self._intervals[0]
  75.         return id
  76.  
  77.     # freeID
  78.     def freeID(self, id):
  79.         """Releases an ID.
  80.  
  81.         \param id (\c int) A previously allocated ID
  82.         """
  83.         if id<self._startid:
  84.             raise IDNotAllocated, "ID %d hasn't been allocated."%id
  85.         
  86.         idx = 0
  87.         for istart,iend in self._intervals:
  88.             if id<istart:
  89.                 break
  90.             # Check if id is in the current interval (=attempt to free an ID
  91.             # which wasn't allocated)
  92.             if iend==None:
  93.                 iend = id+1
  94.             if id>=istart and id<iend:
  95.                 raise IDNotAllocated, "ID %d hasn't been allocated."%id
  96.             idx+=1
  97.  
  98.         # Is the freed id id directly before the interval
  99.         if id==self._intervals[idx][0]-1:
  100.             self._intervals[idx][0]-=1
  101.             # Check if the interval can be merged with the previous interval
  102.             if idx!=0 and self._intervals[idx-1][1]==self._intervals[idx][0]:
  103.                 self._intervals[idx-1][1]=self._intervals[idx][1]
  104.                 del self._intervals[idx]
  105.  
  106.         # Is the freed id directly behind the previous interval? then enlarge
  107.         elif idx!=0 and id==self._intervals[idx-1][1]:
  108.             self._intervals[idx-1][1]+=1
  109.             # Check if the enlarged interval can be merged with its successor
  110.             if self._intervals[idx-1][1]==self._intervals[idx][0]:
  111.                 self._intervals[idx-1][1]=self._intervals[idx][1]
  112.                 del self._intervals[idx]
  113.         else:
  114.             # Create a new interval with one element
  115.             self._intervals.insert(idx,[id,id+1])
  116.  
  117.  
  118.  
  119. ######################################################################
  120. if __name__=="__main__":
  121.     im = IDManager(5)
  122.     print im.allocID()
  123.     print im.allocID()
  124.     print im.allocID()
  125.     print im._intervals
  126.     im.freeID(4)
  127.     im.freeID(5)
  128.     im.freeID(6)
  129.     print im._intervals
  130.     print im.allocID()
  131.     print im._intervals
  132.     print im.allocID()
  133.     print im.allocID()
  134.     
  135.