~ubuntu-branches/ubuntu/wily/python-watchdog/wily

« back to all changes in this revision

Viewing changes to src/watchdog/utils/unicode_paths.py

  • Committer: Package Import Robot
  • Author(s): gustavo panizzo
  • Date: 2014-01-26 10:29:40 UTC
  • Revision ID: package-import@ubuntu.com-20140126102940-601093u3h1hp35b2
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# Copyright (c) 2013 Will Bond <will@wbond.net>
 
5
#
 
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
# of this software and associated documentation files (the "Software"), to deal
 
8
# in the Software without restriction, including without limitation the rights
 
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
# copies of the Software, and to permit persons to whom the Software is
 
11
# furnished to do so, subject to the following conditions:
 
12
#
 
13
# The above copyright notice and this permission notice shall be included in
 
14
# all copies or substantial portions of the Software.
 
15
#
 
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
# SOFTWARE.
 
23
 
 
24
 
 
25
import sys
 
26
 
 
27
from watchdog.utils import platform
 
28
 
 
29
try:
 
30
    # Python 2
 
31
    str_cls = unicode
 
32
    bytes_cls = str
 
33
except NameError:
 
34
    # Python 3
 
35
    str_cls = str
 
36
    bytes_cls = bytes
 
37
 
 
38
 
 
39
fs_encoding = sys.getfilesystemencoding()
 
40
# This is used by Linux when the locale seems to be improperly set. UTF-8 tends
 
41
# to be the encoding used by all distros, so this is a good fallback.
 
42
fs_fallback_encoding = 'utf-8'
 
43
 
 
44
 
 
45
def encode(path):
 
46
    if isinstance(path, str_cls):
 
47
        try:
 
48
            path = path.encode(fs_encoding, 'strict')
 
49
        except UnicodeEncodeError:
 
50
            if not platform.is_linux():
 
51
                raise
 
52
            path = path.encode(fs_fallback_encoding, 'strict')
 
53
    return path
 
54
 
 
55
 
 
56
def decode(path):
 
57
    if isinstance(path, bytes_cls):
 
58
        try:
 
59
            path = path.decode(fs_encoding, 'strict')
 
60
        except UnicodeDecodeError:
 
61
            if not platform.is_linux():
 
62
                raise
 
63
            path = path.decode(fs_fallback_encoding, 'strict')
 
64
    return path