~bzr-pqm/bzr/bzr.dev

1 by mbp at sourcefrog
import from baz patch-364
1
#! /usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
8 by mbp at sourcefrog
store committer's timezone in revision and show
20
1 by mbp at sourcefrog
import from baz patch-364
21
from xml import XMLMixin
7 by mbp at sourcefrog
depend only on regular ElementTree installation
22
23
try:
24
    from cElementTree import Element, ElementTree, SubElement
25
except ImportError:
27 by mbp at sourcefrog
- fix up use of ElementTree without cElementTree
26
    from elementtree.ElementTree import Element, ElementTree, SubElement
7 by mbp at sourcefrog
depend only on regular ElementTree installation
27
1 by mbp at sourcefrog
import from baz patch-364
28
29
class Revision(XMLMixin):
30
    """Single revision on a branch.
31
32
    Revisions may know their revision_hash, but only once they've been
33
    written out.  This is not stored because you cannot write the hash
34
    into the file it describes.
35
36
    :todo: Perhaps make predecessor be a child element, not an attribute?
37
    """
38
    def __init__(self, **args):
39
        self.inventory_id = None
40
        self.revision_id = None
41
        self.timestamp = None
42
        self.message = None
8 by mbp at sourcefrog
store committer's timezone in revision and show
43
        self.timezone = None
1 by mbp at sourcefrog
import from baz patch-364
44
        self.__dict__.update(args)
45
46
47
    def __repr__(self):
48
        if self.revision_id:
49
            return "<Revision id %s>" % self.revision_id
50
51
        
52
    def to_element(self):
11 by mbp at sourcefrog
- change revision XML to <revision>
53
        root = Element('revision',
1 by mbp at sourcefrog
import from baz patch-364
54
                       committer = self.committer,
8 by mbp at sourcefrog
store committer's timezone in revision and show
55
                       timestamp = '%.9f' % self.timestamp,
1 by mbp at sourcefrog
import from baz patch-364
56
                       revision_id = self.revision_id,
8 by mbp at sourcefrog
store committer's timezone in revision and show
57
                       inventory_id = self.inventory_id,
58
                       timezone = str(self.timezone))
1 by mbp at sourcefrog
import from baz patch-364
59
        if self.precursor:
60
            root.set('precursor', self.precursor)
61
        root.text = '\n'
62
        
63
        msg = SubElement(root, 'message')
64
        msg.text = self.message
65
        msg.tail = '\n'
66
67
        return root
68
11 by mbp at sourcefrog
- change revision XML to <revision>
69
70
    def from_element(cls, elt):
71
        # <changeset> is deprecated...
72
        if elt.tag not in ('revision', 'changeset'):
73
            bailout("unexpected tag in revision file: %r" % elt)
74
75
        cs = cls(committer = elt.get('committer'),
76
                 timestamp = float(elt.get('timestamp')),
77
                 precursor = elt.get('precursor'),
78
                 revision_id = elt.get('revision_id'),
79
                 inventory_id = elt.get('inventory_id'))
80
81
        v = elt.get('timezone')
8 by mbp at sourcefrog
store committer's timezone in revision and show
82
        cs.timezone = v and int(v)
83
11 by mbp at sourcefrog
- change revision XML to <revision>
84
        cs.message = elt.findtext('message') # text of <message>
1 by mbp at sourcefrog
import from baz patch-364
85
        return cs
86
87
    from_element = classmethod(from_element)
88