/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.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
34
    known_params = []
35
0.64.8 by Ian Clatworthy
custom parameters for processors
36
    def __init__(self, bzrdir, params=None, verbose=False):
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
37
        self.verbose = verbose
38
        if params is None:
39
            self.params = []
40
        else:
41
            self.params = params
42
            self.validate_parameters()
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
43
        self.bzrdir = bzrdir
44
        if bzrdir is None:
45
            # Some 'importers' don't need a repository to write to
46
            self.branch = None
47
            self.repo = None
48
            self.working_tree = None
49
        else:
50
            (self.working_tree, self.branch) = bzrdir._get_tree_branch()
51
            self.repo = self.branch.repository
0.64.9 by Ian Clatworthy
dump parameter for info processor
52
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
53
    def validate_parameters(self):
54
        """Validate that the parameters are correctly specified."""
55
        for p in self.params:
56
            if p not in self.known_params:
57
                raise errors.UnknownParameter(p, self.known_params)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
58
59
    def process(self, command_iter):
60
        """Import data into Bazaar by processing a stream of commands.
61
62
        :param command_iter: an iterator providing commands
63
        """
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
64
        if self.working_tree is not None:
65
            self.working_tree.lock_write()
66
        elif self.branch is not None:
67
            self.branch.lock_write()
68
        try:
69
            self._process(command_iter)
70
        finally:
71
            if self.working_tree is not None:
72
                self.working_tree.unlock()
73
            elif self.branch is not None:
74
                self.branch.unlock()
75
76
    def _process(self, command_iter):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
77
        self.pre_process()
78
        for cmd in command_iter():
79
            try:
80
                handler = self.__class__.__dict__[cmd.name + "_handler"]
81
            except KeyError:
82
                raise errors.MissingHandler(cmd.name)
83
            else:
0.64.9 by Ian Clatworthy
dump parameter for info processor
84
                self.pre_handler(cmd)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
85
                handler(self, cmd)
0.64.9 by Ian Clatworthy
dump parameter for info processor
86
                self.post_handler(cmd)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
87
        self.post_process()
88
89
    def pre_process(self):
90
        """Hook for logic at start of processing."""
91
        pass
92
93
    def post_process(self):
94
        """Hook for logic at end of processing."""
95
        pass
96
0.64.9 by Ian Clatworthy
dump parameter for info processor
97
    def pre_handler(self, cmd):
98
        """Hook for logic before each handler starts."""
99
        pass
100
101
    def post_handler(self, cmd):
102
        """Hook for logic after each handler finishes."""
103
        pass
104
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
105
    def progress_handler(self, cmd):
106
        """Process a ProgressCommand."""
107
        raise NotImplementedError(self.progress_handler)
108
109
    def blob_handler(self, cmd):
110
        """Process a BlobCommand."""
111
        raise NotImplementedError(self.blob_handler)
112
113
    def checkpoint_handler(self, cmd):
114
        """Process a CheckpointCommand."""
115
        raise NotImplementedError(self.checkpoint_handler)
116
117
    def commit_handler(self, cmd):
118
        """Process a CommitCommand."""
119
        raise NotImplementedError(self.commit_handler)
120
121
    def reset_handler(self, cmd):
122
        """Process a ResetCommand."""
123
        raise NotImplementedError(self.reset_handler)
124
125
    def tag_handler(self, cmd):
126
        """Process a TagCommand."""
127
        raise NotImplementedError(self.tag_handler)
0.64.4 by Ian Clatworthy
abstract CommitHandler
128
129
130
class CommitHandler(object):
131
    """Base class for commit handling.
132
    
133
    Subclasses should override the pre_*, post_* and *_handler
134
    methods as appropriate.
135
    """
136
137
    def __init__(self, command):
138
        self.command = command
139
140
    def process(self):
141
        self.pre_process_files()
142
        for fc in self.command.file_iter():
143
            try:
0.64.20 by Ian Clatworthy
clean-up fixes after filtering enhancements
144
                handler = self.__class__.__dict__[fc.name[4:] + "_handler"]
0.64.4 by Ian Clatworthy
abstract CommitHandler
145
            except KeyError:
146
                raise errors.MissingHandler(fc.name)
147
            else:
148
                handler(self, fc)
149
        self.post_process_files()
150
151
    def pre_process_files(self):
152
        """Prepare for committing."""
153
        pass
154
155
    def post_process_files(self):
156
        """Save the revision."""
157
        pass
158
159
    def modify_handler(self, filecmd):
160
        """Handle a filemodify command."""
161
        raise NotImplementedError(self.modify_handler)
162
163
    def delete_handler(self, filecmd):
164
        """Handle a filedelete command."""
165
        raise NotImplementedError(self.delete_handler)
166
167
    def copy_handler(self, filecmd):
168
        """Handle a filecopy command."""
169
        raise NotImplementedError(self.copy_handler)
170
171
    def rename_handler(self, filecmd):
172
        """Handle a filerename command."""
173
        raise NotImplementedError(self.rename_handler)
174
175
    def deleteall_handler(self, filecmd):
176
        """Handle a filedeleteall command."""
177
        raise NotImplementedError(self.deleteall_handler)