~roguescholar/brz-loom/debian

21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
1
# Loom, a plugin for bzr to assist in developing focused patches.
81.2.3 by Aaron Bentley
Allow combine-thread to delete the bottom thread (#194274)
2
# Copyright (C) 2006, 2008 Canonical Limited.
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
3
# 
4
# This program is free software; you can redistribute it and/or modify
65 by Robert Collins
GPL Version 2.
5
# it under the terms of the GNU General Public License version 2 as published
6
# by the Free Software Foundation.
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
7
# 
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
# 
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
16
# 
17
18
19
"""The current-loom state object."""
20
157 by Jelmer Vernooij
Use absolute_import everywhere.
21
from __future__ import absolute_import
22
161 by Jelmer Vernooij
Port to breezy (mostly)
23
from breezy.revision import NULL_REVISION
59 by Robert Collins
No more deprecation warnings.
24
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
25
26
class LoomState(object):
27
    """The LoomState represents the content of the current-loom branch file.
28
    
29
    It is planned to not need access to repository data - it will be driven
30
    by the LoomBranch and have data fed into it.
31
    """
32
24 by Robert Collins
Factor all read and writes of the last-loom format into loom_io.py.
33
    def __init__(self, reader=None):
34
        """Create a loom state object.
35
36
        :param reader: If not None, this should be a LoomStateReader from
37
            which this LoomState is meant to retrieve its current data.
38
        """
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
39
        self._parents = []
40
        self._threads = []
24 by Robert Collins
Factor all read and writes of the last-loom format into loom_io.py.
41
        if reader is not None:
42
            # perhaps this should be lazy evaluated at some point?
43
            self._parents = reader.read_parents()
44
            for thread in reader.read_thread_details():
32 by Robert Collins
Change the get_threads api to return the known parents for each thread, and set_threads to take the parents list as well.
45
                self._threads.append(thread)
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
46
30 by Robert Collins
Simplify the LoomState api - remove all ability to store anything other than the current threads and the parents list, as that was getting overly complex.
47
    def get_basis_revision_id(self):
48
        """Get the revision id for the basis revision.
49
50
        None is return if there is no basis revision.
51
        """
52
        if not self._parents:
59 by Robert Collins
No more deprecation warnings.
53
            return NULL_REVISION
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
54
        else:
30 by Robert Collins
Simplify the LoomState api - remove all ability to store anything other than the current threads and the parents list, as that was getting overly complex.
55
            return self._parents[0]
56
 
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
57
    def get_parents(self):
58
        """Get the list of loom revisions that are parents to this state."""
59
        return self._parents
60
61
    def get_threads(self):
62
        """Get the threads for the current state."""
22 by Robert Collins
Create a LoomStateWriter and give all LoomState writing responsibilities to it.
63
        return list(self._threads)
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
64
32 by Robert Collins
Change the get_threads api to return the known parents for each thread, and set_threads to take the parents list as well.
65
    def get_threads_dict(self):
66
        """Get the threads as a dict. 
67
68
        This loses ordering, but is useful for quickly locating the details on 
69
        a given thread.
70
        """
71
        return dict((thread[0], thread[1:]) for thread in self._threads)
72
81.2.2 by Aaron Bentley
Refactor, producing get_previous_thread and moving thread_index to LoomState
73
    def thread_index(self, thread):
74
        """Find the index of thread in threads."""
75
        # Avoid circular import
161 by Jelmer Vernooij
Port to breezy (mostly)
76
        from breezy.plugins.loom.branch import NoSuchThread
81.2.2 by Aaron Bentley
Refactor, producing get_previous_thread and moving thread_index to LoomState
77
        thread_names = [name for name, rev, parents in self._threads]
78
        try:
79
            return thread_names.index(thread)
80
        except ValueError:
81
            raise NoSuchThread(self, thread)
82
81.2.3 by Aaron Bentley
Allow combine-thread to delete the bottom thread (#194274)
83
    def get_new_thread_after_deleting(self, current_thread):
84
        if len(self._threads) == 1:
85
            return None
81.2.2 by Aaron Bentley
Refactor, producing get_previous_thread and moving thread_index to LoomState
86
        current_index = self.thread_index(current_thread)
87
        if current_index == 0:
81.2.3 by Aaron Bentley
Allow combine-thread to delete the bottom thread (#194274)
88
            new_index = 1
89
        else:
90
            new_index = current_index - 1
91
        return self._threads[new_index][0]
81.2.2 by Aaron Bentley
Refactor, producing get_previous_thread and moving thread_index to LoomState
92
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
93
    def set_parents(self, parent_list):
94
        """Set the parents of this state to parent_list.
95
96
        :param parent_list: A list of (parent_id, threads) tuples.
97
        """
30 by Robert Collins
Simplify the LoomState api - remove all ability to store anything other than the current threads and the parents list, as that was getting overly complex.
98
        self._parents = list(parent_list)
21 by Robert Collins
Introduce LoomState for storing the working loom in memory, and use this for queries in LoomBranch.
99
100
    def set_threads(self, threads):
101
        """Set the current threads to threads.
102
103
        :param threads: A list of (name, revid) pairs that make up the threads.
104
            If the list is altered after calling set_threads, there is no 
105
            effect on the LoomState.
106
        """
107
        self._threads = list(threads)