~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# arch-tag: b1ae35f1-d91b-49f0-a1ff-e163ddd8cefd
# Copyright (C) 2004 David Allouche <david@allouche.net>
#               2005 Canonical Limited.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
Internal module providing name escaping functionality.

This module implements some of public interface for the
arch_ package. But for convenience reasons the author prefers
to store this code in a file separate from ``__init__.py``  .

.. _arch: arch-module.html

This module is strictly internal and should never be used.
"""

__all__ = ['name_escape', 'name_unescape']


### Escaping Tables ###

__space_escape_table = { '\t': 'tab', '\n': 'nl', '\v': 'U+B',
                    '\f': 'np', '\r': 'cr', ' ': 'sp' }

def __make_name_escape_table():
    table = {}
    for N in range(256):
        C = chr(N)
        if C in __space_escape_table: val = '\\(%s)' % __space_escape_table[C]
        elif N < 32 or N > 127: val = '\\(U+%X)' % N
        elif C in '"\\': val = '\\'+C
        else: val = C
        table[C] = val
    return table

__name_escape_table = __make_name_escape_table()

__space_unescape_table = {}
for k, v in __space_escape_table.items(): __space_unescape_table[v] = k


### Escaping Functions ###

def name_escape(name):
    """Escape a file name using the Arch syntax.

    :arg name: unescaped file name.
    :type name: str
    :return: escaped file name.
    :rtype: str
    """
    return ''.join([__name_escape_table[C] for C in name])


def name_unescape(name):
    """Unescape a file name using the Arch syntax.

    :arg name: escaped file name.
    :type name: str
    :return: unescaped file name.
    :rtype: str
    :raise errors.IllegalEscapeSequence: the syntax of ``name`` is incorrect.
    """
    result = []
    after_backslash = 'after_backslash'
    in_brackets = 'in_brackets'
    state = None
    escape = None # buffer for escape sequence
    for C in name:
        if state is None:
            if C != '\\':
                result.append(C)
                continue
            else:
                state = after_backslash
                continue
        elif state is after_backslash:
            if C == '(':
                escape = []
                state = in_brackets
                continue
            elif C in '\\"':
                state = None
                result.append(C)
                continue
            else:
                raise errors.IllegalEscapeSequence(name)
        elif state is in_brackets:
            if C != ')':
                escape.append(C)
                continue
            else:
                state = None
                if len(escape) < 2:
                    raise errors.IllegalEscapeSequence(name)
                escape_str = ''.join(escape)
                escape = None
                if escape_str[0:2] in ('U+', 'u+'):
                    try:
                        code = int(escape_str[2:], 16)
                    except ValueError:
                        raise errors.IllegalEscapeSequence(name)
                    if code > 255:
                        raise errors.IllegalEscapeSequence(name)
                    result.append(chr(code))
                    continue
                else:
                    escape_str = escape_str.lower()
                    try:
                        result.append(__space_unescape_table[escape_str])
                    except KeyError:
                        raise errors.IllegalEscapeSequence(name)
                    continue
        else:
            raise AssertionError, "The programmer is on crack!"
    if state is not None:
        raise errors.IllegalEscapeSequence(name)
    else:
        return ''.join(result)