~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/_escaping.py

  • Committer: Robert Collins
  • Date: 2005-09-14 11:27:20 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050914112720-c66a21de86eafa6e
trim fai cribbage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# arch-tag: b1ae35f1-d91b-49f0-a1ff-e163ddd8cefd
2
 
# Copyright (C) 2004 David Allouche <david@allouche.net>
3
 
#               2005 Canonical Limited.
4
 
#
5
 
#    This program is free software; you can redistribute it and/or modify
6
 
#    it under the terms of the GNU General Public License as published by
7
 
#    the Free Software Foundation; either version 2 of the License, or
8
 
#    (at your option) any later version.
9
 
#
10
 
#    This program is distributed in the hope that it will be useful,
11
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
#    GNU General Public License for more details.
14
 
#
15
 
#    You should have received a copy of the GNU General Public License
16
 
#    along with this program; if not, write to the Free Software
17
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 
19
 
"""
20
 
Internal module providing name escaping functionality.
21
 
 
22
 
This module implements some of public interface for the
23
 
arch_ package. But for convenience reasons the author prefers
24
 
to store this code in a file separate from ``__init__.py``  .
25
 
 
26
 
.. _arch: arch-module.html
27
 
 
28
 
This module is strictly internal and should never be used.
29
 
"""
30
 
 
31
 
__all__ = ['name_escape', 'name_unescape']
32
 
 
33
 
 
34
 
### Escaping Tables ###
35
 
 
36
 
__space_escape_table = { '\t': 'tab', '\n': 'nl', '\v': 'U+B',
37
 
                    '\f': 'np', '\r': 'cr', ' ': 'sp' }
38
 
 
39
 
def __make_name_escape_table():
40
 
    table = {}
41
 
    for N in range(256):
42
 
        C = chr(N)
43
 
        if C in __space_escape_table: val = '\\(%s)' % __space_escape_table[C]
44
 
        elif N < 32 or N > 127: val = '\\(U+%X)' % N
45
 
        elif C in '"\\': val = '\\'+C
46
 
        else: val = C
47
 
        table[C] = val
48
 
    return table
49
 
 
50
 
__name_escape_table = __make_name_escape_table()
51
 
 
52
 
__space_unescape_table = {}
53
 
for k, v in __space_escape_table.items(): __space_unescape_table[v] = k
54
 
 
55
 
 
56
 
### Escaping Functions ###
57
 
 
58
 
def name_escape(name):
59
 
    """Escape a file name using the Arch syntax.
60
 
 
61
 
    :arg name: unescaped file name.
62
 
    :type name: str
63
 
    :return: escaped file name.
64
 
    :rtype: str
65
 
    """
66
 
    return ''.join([__name_escape_table[C] for C in name])
67
 
 
68
 
 
69
 
def name_unescape(name):
70
 
    """Unescape a file name using the Arch syntax.
71
 
 
72
 
    :arg name: escaped file name.
73
 
    :type name: str
74
 
    :return: unescaped file name.
75
 
    :rtype: str
76
 
    :raise errors.IllegalEscapeSequence: the syntax of ``name`` is incorrect.
77
 
    """
78
 
    result = []
79
 
    after_backslash = 'after_backslash'
80
 
    in_brackets = 'in_brackets'
81
 
    state = None
82
 
    escape = None # buffer for escape sequence
83
 
    for C in name:
84
 
        if state is None:
85
 
            if C != '\\':
86
 
                result.append(C)
87
 
                continue
88
 
            else:
89
 
                state = after_backslash
90
 
                continue
91
 
        elif state is after_backslash:
92
 
            if C == '(':
93
 
                escape = []
94
 
                state = in_brackets
95
 
                continue
96
 
            elif C in '\\"':
97
 
                state = None
98
 
                result.append(C)
99
 
                continue
100
 
            else:
101
 
                raise errors.IllegalEscapeSequence(name)
102
 
        elif state is in_brackets:
103
 
            if C != ')':
104
 
                escape.append(C)
105
 
                continue
106
 
            else:
107
 
                state = None
108
 
                if len(escape) < 2:
109
 
                    raise errors.IllegalEscapeSequence(name)
110
 
                escape_str = ''.join(escape)
111
 
                escape = None
112
 
                if escape_str[0:2] in ('U+', 'u+'):
113
 
                    try:
114
 
                        code = int(escape_str[2:], 16)
115
 
                    except ValueError:
116
 
                        raise errors.IllegalEscapeSequence(name)
117
 
                    if code > 255:
118
 
                        raise errors.IllegalEscapeSequence(name)
119
 
                    result.append(chr(code))
120
 
                    continue
121
 
                else:
122
 
                    escape_str = escape_str.lower()
123
 
                    try:
124
 
                        result.append(__space_unescape_table[escape_str])
125
 
                    except KeyError:
126
 
                        raise errors.IllegalEscapeSequence(name)
127
 
                    continue
128
 
        else:
129
 
            raise AssertionError, "The programmer is on crack!"
130
 
    if state is not None:
131
 
        raise errors.IllegalEscapeSequence(name)
132
 
    else:
133
 
        return ''.join(result)