/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Processor of import commands.
18
19
This module provides core processing functionality including an abstract class
20
for basing real processors on. See the processors package for examples.
21
"""
22
23
24
import errors
25
26
27
class ImportProcessor(object):
28
    """Base class for import processors.
29
    
30
    Subclasses should override the pre_*, post_* and *_handler
31
    methods as appropriate.
32
    """
33
0.64.8 by Ian Clatworthy
custom parameters for processors
34
    def __init__(self, bzrdir, params=None, verbose=False):
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
35
        self.bzrdir = bzrdir
36
        if bzrdir is None:
37
            # Some 'importers' don't need a repository to write to
38
            self.branch = None
39
            self.repo = None
40
            self.working_tree = None
41
        else:
42
            (self.working_tree, self.branch) = bzrdir._get_tree_branch()
43
            self.repo = self.branch.repository
0.64.9 by Ian Clatworthy
dump parameter for info processor
44
        if params is None:
45
            self.params = []
46
        else:
47
            self.params = params
48
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
49
        self.verbose = verbose
50
51
    def process(self, command_iter):
52
        """Import data into Bazaar by processing a stream of commands.
53
54
        :param command_iter: an iterator providing commands
55
        """
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
56
        if self.working_tree is not None:
57
            self.working_tree.lock_write()
58
        elif self.branch is not None:
59
            self.branch.lock_write()
60
        try:
61
            self._process(command_iter)
62
        finally:
63
            if self.working_tree is not None:
64
                self.working_tree.unlock()
65
            elif self.branch is not None:
66
                self.branch.unlock()
67
68
    def _process(self, command_iter):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
69
        self.pre_process()
70
        for cmd in command_iter():
71
            try:
72
                handler = self.__class__.__dict__[cmd.name + "_handler"]
73
            except KeyError:
74
                raise errors.MissingHandler(cmd.name)
75
            else:
0.64.9 by Ian Clatworthy
dump parameter for info processor
76
                self.pre_handler(cmd)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
77
                handler(self, cmd)
0.64.9 by Ian Clatworthy
dump parameter for info processor
78
                self.post_handler(cmd)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
79
        self.post_process()
80
81
    def pre_process(self):
82
        """Hook for logic at start of processing."""
83
        pass
84
85
    def post_process(self):
86
        """Hook for logic at end of processing."""
87
        pass
88
0.64.9 by Ian Clatworthy
dump parameter for info processor
89
    def pre_handler(self, cmd):
90
        """Hook for logic before each handler starts."""
91
        pass
92
93
    def post_handler(self, cmd):
94
        """Hook for logic after each handler finishes."""
95
        pass
96
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
97
    def progress_handler(self, cmd):
98
        """Process a ProgressCommand."""
99
        raise NotImplementedError(self.progress_handler)
100
101
    def blob_handler(self, cmd):
102
        """Process a BlobCommand."""
103
        raise NotImplementedError(self.blob_handler)
104
105
    def checkpoint_handler(self, cmd):
106
        """Process a CheckpointCommand."""
107
        raise NotImplementedError(self.checkpoint_handler)
108
109
    def commit_handler(self, cmd):
110
        """Process a CommitCommand."""
111
        raise NotImplementedError(self.commit_handler)
112
113
    def reset_handler(self, cmd):
114
        """Process a ResetCommand."""
115
        raise NotImplementedError(self.reset_handler)
116
117
    def tag_handler(self, cmd):
118
        """Process a TagCommand."""
119
        raise NotImplementedError(self.tag_handler)
0.64.4 by Ian Clatworthy
abstract CommitHandler
120
121
122
class CommitHandler(object):
123
    """Base class for commit handling.
124
    
125
    Subclasses should override the pre_*, post_* and *_handler
126
    methods as appropriate.
127
    """
128
129
    def __init__(self, command):
130
        self.command = command
131
132
    def process(self):
133
        self.pre_process_files()
134
        for fc in self.command.file_iter():
135
            try:
136
                handler = self.__class__.__dict__[fc.name + "_handler"]
137
            except KeyError:
138
                raise errors.MissingHandler(fc.name)
139
            else:
140
                handler(self, fc)
141
        self.post_process_files()
142
143
    def pre_process_files(self):
144
        """Prepare for committing."""
145
        pass
146
147
    def post_process_files(self):
148
        """Save the revision."""
149
        pass
150
151
    def modify_handler(self, filecmd):
152
        """Handle a filemodify command."""
153
        raise NotImplementedError(self.modify_handler)
154
155
    def delete_handler(self, filecmd):
156
        """Handle a filedelete command."""
157
        raise NotImplementedError(self.delete_handler)
158
159
    def copy_handler(self, filecmd):
160
        """Handle a filecopy command."""
161
        raise NotImplementedError(self.copy_handler)
162
163
    def rename_handler(self, filecmd):
164
        """Handle a filerename command."""
165
        raise NotImplementedError(self.rename_handler)
166
167
    def deleteall_handler(self, filecmd):
168
        """Handle a filedeleteall command."""
169
        raise NotImplementedError(self.deleteall_handler)