app/soc/logic/lists.py
changeset 539 e30462354e26
child 553 c0cc20b4afc9
equal deleted inserted replaced
538:4d209757c835 539:e30462354e26
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """List generation logic
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 class Lists(object):
       
    26   """List array suitable for enumerating over with just 'for in'
       
    27   """
       
    28 
       
    29   DEF_PASSTHROUGH_FIELDS = [
       
    30       'pagination',
       
    31       'description',
       
    32       'heading',
       
    33       'row',
       
    34       'limit',
       
    35       'newest',
       
    36       'prev',
       
    37       'next',
       
    38       'first',
       
    39       'last',
       
    40       ]
       
    41 
       
    42   def __init__(self, contents):
       
    43     """Constructs a new Lists object with the specified contents
       
    44     """
       
    45 
       
    46     # All the contents of all the lists
       
    47     self.contents = contents
       
    48     self.content = {}
       
    49 
       
    50     # For iterating over all the lists
       
    51     self.lists = range(len(contents))
       
    52     self.list_data = []
       
    53 
       
    54     # For iterating over the rows
       
    55     self.rows = []
       
    56     self.row_data = []
       
    57 
       
    58   def __getattr__(self, attr):
       
    59     """Delegate field lookup to the current list if appropriate
       
    60 
       
    61     If, and only if, a lookup is done on one of the fields defined in
       
    62     DEF_PASSTHROUGH_FIELDS, and the current list defines this field,
       
    63     the value from the current list is returned.
       
    64     """
       
    65 
       
    66     if attr not in self.DEF_PASSTHROUGH_FIELDS:
       
    67       raise AttributeError()
       
    68 
       
    69     if attr not in self.content:
       
    70       raise AttributeError()
       
    71 
       
    72     return self.get(attr)
       
    73 
       
    74   def get(self, item):
       
    75     """Returns the item for the current list data
       
    76     """
       
    77 
       
    78     return self.content[item]
       
    79 
       
    80   def next_list(self):
       
    81     """Shifts out the current list content
       
    82 
       
    83     The main content of the next list is returned for further processing.
       
    84     """
       
    85 
       
    86     # Advance the list data once
       
    87     self.content = self.contents[0]
       
    88     self.contents = self.contents[1:]
       
    89 
       
    90     # Update internal 'iterators'
       
    91     self.list_data = self.get('data')
       
    92     self.rows = range(len(self.list_data))
       
    93 
       
    94     return self.get('main')
       
    95 
       
    96   def next_row(self):
       
    97     """Returns the next list row for the current list
       
    98 
       
    99     Before calling this method, next_list should be called at least once.
       
   100     """
       
   101 
       
   102     # Update internal 'iterators'
       
   103     self.row_data =  self.list_data[0]
       
   104 
       
   105     # Advance the row data once
       
   106     self.list_data = self.list_data[1:]
       
   107 
       
   108     return self.get('row')
       
   109 
       
   110   def lists(self):
       
   111     """Returns a list of numbers the size of the amount of lists.
       
   112 
       
   113     This method can be used to iterate over all lists with shift,
       
   114     without using a while loop.
       
   115     """
       
   116 
       
   117     return self.lists
       
   118 
       
   119   def rows(self):
       
   120     """Returns a list of numbers the size of the amount of items.
       
   121 
       
   122     This method can be used to iterate over all items with next for
       
   123     the current list, without using a while loop.
       
   124     """
       
   125 
       
   126     return self.rows
       
   127 
       
   128   def item(self):
       
   129     """Returns the current row item for the current list
       
   130 
       
   131     Before calling this method, next_row should be called at least once.
       
   132     """
       
   133 
       
   134     return self.row_data