1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6class HostFileSystemIterator(object):
7  '''Provides methods for iterating through host file systems, in both
8  ascending (oldest to newest version) and descending order.
9  '''
10
11  def __init__(self, file_system_provider, branch_utility):
12    self._file_system_provider = file_system_provider
13    self._branch_utility = branch_utility
14
15  def _ForEach(self, channel_info, callback, get_next):
16    '''Iterates through a sequence of file systems defined by |get_next| until
17    |callback| returns False, or until the end of the sequence of file systems
18    is reached. Returns the BranchUtility.ChannelInfo of the last file system
19    for which |callback| returned True.
20    '''
21    last_true = None
22    while channel_info is not None:
23      if channel_info.branch == 'master':
24        file_system = self._file_system_provider.GetMaster()
25      else:
26        file_system = self._file_system_provider.GetBranch(channel_info.branch)
27      if not callback(file_system, channel_info):
28        return last_true
29      last_true = channel_info
30      channel_info = get_next(channel_info)
31    return last_true
32
33  def Ascending(self, channel_info, callback):
34    return self._ForEach(channel_info, callback, self._branch_utility.Newer)
35
36  def Descending(self, channel_info, callback):
37    return self._ForEach(channel_info, callback, self._branch_utility.Older)
38