/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.8 by Ian Clatworthy
custom parameters for processors
44
        self.params = params
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
45
        self.verbose = verbose
46
47
    def process(self, command_iter):
48
        """Import data into Bazaar by processing a stream of commands.
49
50
        :param command_iter: an iterator providing commands
51
        """
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
52
        if self.working_tree is not None:
53
            self.working_tree.lock_write()
54
        elif self.branch is not None:
55
            self.branch.lock_write()
56
        try:
57
            self._process(command_iter)
58
        finally:
59
            if self.working_tree is not None:
60
                self.working_tree.unlock()
61
            elif self.branch is not None:
62
                self.branch.unlock()
63
64
    def _process(self, command_iter):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
65
        self.pre_process()
66
        for cmd in command_iter():
67
            try:
68
                handler = self.__class__.__dict__[cmd.name + "_handler"]
69
            except KeyError:
70
                raise errors.MissingHandler(cmd.name)
71
            else:
72
                # TODO: put hooks around processing each command?
73
                handler(self, cmd)
74
        self.post_process()
75
76
    def pre_process(self):
77
        """Hook for logic at start of processing."""
78
        pass
79
80
    def post_process(self):
81
        """Hook for logic at end of processing."""
82
        pass
83
84
    def progress_handler(self, cmd):
85
        """Process a ProgressCommand."""
86
        raise NotImplementedError(self.progress_handler)
87
88
    def blob_handler(self, cmd):
89
        """Process a BlobCommand."""
90
        raise NotImplementedError(self.blob_handler)
91
92
    def checkpoint_handler(self, cmd):
93
        """Process a CheckpointCommand."""
94
        raise NotImplementedError(self.checkpoint_handler)
95
96
    def commit_handler(self, cmd):
97
        """Process a CommitCommand."""
98
        raise NotImplementedError(self.commit_handler)
99
100
    def reset_handler(self, cmd):
101
        """Process a ResetCommand."""
102
        raise NotImplementedError(self.reset_handler)
103
104
    def tag_handler(self, cmd):
105
        """Process a TagCommand."""
106
        raise NotImplementedError(self.tag_handler)
0.64.4 by Ian Clatworthy
abstract CommitHandler
107
108
109
class CommitHandler(object):
110
    """Base class for commit handling.
111
    
112
    Subclasses should override the pre_*, post_* and *_handler
113
    methods as appropriate.
114
    """
115
116
    def __init__(self, command):
117
        self.command = command
118
119
    def process(self):
120
        self.pre_process_files()
121
        for fc in self.command.file_iter():
122
            try:
123
                handler = self.__class__.__dict__[fc.name + "_handler"]
124
            except KeyError:
125
                raise errors.MissingHandler(fc.name)
126
            else:
127
                handler(self, fc)
128
        self.post_process_files()
129
130
    def pre_process_files(self):
131
        """Prepare for committing."""
132
        pass
133
134
    def post_process_files(self):
135
        """Save the revision."""
136
        pass
137
138
    def modify_handler(self, filecmd):
139
        """Handle a filemodify command."""
140
        raise NotImplementedError(self.modify_handler)
141
142
    def delete_handler(self, filecmd):
143
        """Handle a filedelete command."""
144
        raise NotImplementedError(self.delete_handler)
145
146
    def copy_handler(self, filecmd):
147
        """Handle a filecopy command."""
148
        raise NotImplementedError(self.copy_handler)
149
150
    def rename_handler(self, filecmd):
151
        """Handle a filerename command."""
152
        raise NotImplementedError(self.rename_handler)
153
154
    def deleteall_handler(self, filecmd):
155
        """Handle a filedeleteall command."""
156
        raise NotImplementedError(self.deleteall_handler)