~ubuntu-branches/debian/stretch/bzr-xmloutput/stretch

« back to all changes in this revision

Viewing changes to annotatexml.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-01-20 05:18:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090120051835-r8ppbnuij6m2symv
Tags: upstream-0.8.1+bzr116
ImportĀ upstreamĀ versionĀ 0.8.1+bzr116

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- encoding: utf-8 -*-
 
3
# Copyright (C) 2007 Guillermo Gonzalez
 
4
#
 
5
# The code taken from bzrlib is under: Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
20
#
 
21
# Contributors:
 
22
#               Martin Albisetti
 
23
 
 
24
"""
 
25
This code is a modified copy from bzrlib.annotate
 
26
(see there for copyrights and licensing)
 
27
"""
 
28
 
 
29
import sys
 
30
 
 
31
_annotate_file = None
 
32
try:
 
33
    from bzrlib.annotate import _expand_annotations
 
34
except ImportError:
 
35
    # to support bzr < 1.8
 
36
    from bzrlib.annotate import _annotate_file
 
37
 
 
38
 
 
39
from bzrlib.annotate import _annotations
 
40
from bzrlib.xml_serializer import _escape_cdata
 
41
from bzrlib import user_encoding
 
42
from bzrlib import osutils
 
43
 
 
44
empty_annotation = 'revno="" author="" date=""'
 
45
 
 
46
def annotate_file_xml(branch, rev_id, file_id, to_file=None,
 
47
            show_ids=False, wt_root_path=None, file_path=None, full=False):
 
48
    if to_file is None:
 
49
        to_file = sys.stdout
 
50
 
 
51
    encoding = getattr(to_file, 'encoding', None) or \
 
52
            osutils.get_terminal_encoding()
 
53
    prevanno=''
 
54
    last_rev_id = None
 
55
    to_file.write('<?xml version="1.0"?>')
 
56
    to_file.write(('<annotation workingtree-root="%s" %s>' % \
 
57
                  (wt_root_path.encode(encoding),
 
58
                  'file="%s"' % file_path)).encode(encoding, 'replace'))
 
59
 
 
60
    annotations = _annotations(branch.repository, file_id, rev_id)
 
61
    if _annotate_file:
 
62
        annotation = list(_annotate_file(branch, rev_id, file_id))
 
63
    else:
 
64
        annotation = list(_expand_annotations(annotations, branch))
 
65
 
 
66
    for (revno_str, author, date_str, line_rev_id,
 
67
        text, origin) in _annotation_iter(annotation, annotations):
 
68
        if not show_ids:
 
69
            origin = None
 
70
        prevanno = _show_entry(to_file, prevanno, revno_str, author,
 
71
            date_str, line_rev_id, text, origin)
 
72
    to_file.write('</annotation>')
 
73
 
 
74
 
 
75
def _annotation_iter(annotation, annotations):
 
76
    for ((revno_str, author, date_str, line_rev_id, text), \
 
77
            (origin, text_dup)) in zip(annotation, annotations):
 
78
        yield (revno_str, author, date_str, line_rev_id, text, origin)
 
79
 
 
80
 
 
81
def _show_entry(to_file, prevanno, revno_str, author,
 
82
                date_str, line_rev_id, text, fid):
 
83
    anno = 'revno="%s" author="%s" date="%s"' % \
 
84
                (_escape_cdata(revno_str), _escape_cdata(author), date_str)
 
85
    if anno.lstrip() == empty_annotation:
 
86
        anno = prevanno
 
87
    if fid:
 
88
        to_file.write('<entry %s fid="%s">%s</entry>' % \
 
89
                    (anno, fid, _escape_cdata(text)))
 
90
    else:
 
91
        to_file.write('<entry %s>' % anno)
 
92
        to_file.write('%s</entry>' % _escape_cdata(text))
 
93
    return anno