Package Camelot :: Package camelot :: Package view :: Module fifo |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.fifo

     1  #  ============================================================================

     2  #

     3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.

     4  #  www.conceptive.be / project-camelot@conceptive.be

     5  #

     6  #  This file is part of the Camelot Library.

     7  #

     8  #  This file may be used under the terms of the GNU General Public

     9  #  License version 2.0 as published by the Free Software Foundation

    10  #  and appearing in the file LICENSE.GPL included in the packaging of

    11  #  this file.  Please review the following information to ensure GNU

    12  #  General Public Licensing requirements will be met:

    13  #  http://www.trolltech.com/products/qt/opensource.html

    14  #

    15  #  If you are unsure which license is appropriate for your use, please

    16  #  review the following information:

    17  #  http://www.trolltech.com/products/qt/licensing.html or contact

    18  #  project-camelot@conceptive.be.

    19  #

    20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

    21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

    22  #

    23  #  For use of this library in commercial applications, please contact

    24  #  project-camelot@conceptive.be

    25  #

    26  #  ============================================================================

    27

    28

    29 -class fifo(object):
    30      """fifo, is the actual cache containing a limited set of copies of row data

    31      so the data in fifo, is always immediately accessible to the gui thread,

    32      with zero delay as you scroll down the table view, fifo is filled and

    33      refilled with data queried from the database

    34

    35      the cache can be queried either by the row number or by object represented

    36      by the row data.

    37      """
    38 -    def __init__(self, max_entries):
    39          self.max_entries = max_entries
    40          self.entities = []
    41          self.data_by_rows = dict()
    42          self.rows_by_entity = dict()
    43

    44 -    def __unicode__(self):
    45          return u','.join(unicode(e) for e in self.entities)
    46

    47 -    def add_data(self, row, entity, value):
    48          self.delete_by_entity(entity)
    49          self.data_by_rows[row] = (entity, value)
    50          self.rows_by_entity[entity] = row
    51          self.entities.append(entity)
    52          if len(self.entities)>self.max_entries:
    53              entity = self.entities.pop(0)
    54              self.delete_by_entity(entity)
    55

    56 -    def delete_by_row(self, row):
    57          (entity, value_) = self.data_by_rows[row]
    58          del self.data_by_rows[row]
    59          del self.rows_by_entity[entity]
    60          return row
    61

    62 -    def delete_by_entity(self, entity):
    63          """Remove everything in the cache related to an entity instance

    64          returns the row at which the data was stored if the data was in the

    65          cache, return None otherwise"""
    66          row = None
    67          try:
    68              row = self.rows_by_entity[entity]
    69              del self.data_by_rows[row]
    70              del self.rows_by_entity[entity]
    71          except KeyError:
    72              pass
    73          try:
    74              self.entities.remove(entity)
    75          except ValueError:
    76              pass
    77          return row
    78

    79 -    def get_data_at_row(self, row):
    80          return self.data_by_rows[row][1]
    81

    82 -    def get_row_by_entity(self, entity):
    83          return self.rows_by_entity[entity]
    84

    85 -    def get_entity_at_row(self, row):
    86          return self.data_by_rows[row][0]
    87