source: ftputil/tool.py @ 1564:c5b353a1c23d

Last change on this file since 1564:c5b353a1c23d was 1564:c5b353a1c23d, checked in by Stefan Schwarzer <sschwarzer@…>, 5 years ago
List contributors in `doc/contributors.txt`. So far, individual files had copyright notices for contributors. However, this makes it difficult to properly adapt files in case of refactoring: If a piece of code is moved to another file, I would need to find out if this code was contributed by someone else and change the copyright notice in the target file accordingly. With the new approach, every file refers to the file `doc/contributors.txt`, which contains the names of contributors.
File size: 2.5 KB
Line 
1# Copyright (C) 2013, Stefan Schwarzer
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5"""
6tool.py - helper code
7"""
8
9from __future__ import unicode_literals
10
11import ftputil.compat as compat
12
13
14__all__ = ["same_string_type_as", "as_bytes", "as_unicode",
15           "as_default_string"]
16
17
18# Encoding to convert between byte string and unicode string. This is
19# a "lossless" encoding: Strings can be encoded/decoded back and forth
20# without information loss or causing encoding-related errors. The
21# `ftplib` module under Python 3 also uses the "latin1" encoding
22# internally. It's important to use the same encoding here, so that users who
23# used `ftplib` to create FTP items with non-ASCII characters can access them
24# in the same way with ftputil.
25LOSSLESS_ENCODING = "latin1"
26
27
28def same_string_type_as(type_source, content_source):
29    """
30    Return a string of the same type as `type_source` with the content
31    from `content_source`.
32
33    If the `type_source` and `content_source` don't have the same
34    type, use `LOSSLESS_ENCODING` above to encode or decode, whatever
35    operation is needed.
36    """
37    if (
38      isinstance(type_source, compat.bytes_type) and
39      isinstance(content_source, compat.unicode_type)):
40        return content_source.encode(LOSSLESS_ENCODING)
41    elif (
42      isinstance(type_source, compat.unicode_type) and
43      isinstance(content_source, compat.bytes_type)):
44        return content_source.decode(LOSSLESS_ENCODING)
45    else:
46        return content_source
47
48
49def as_bytes(string):
50    """
51    Return the argument `string` converted to a byte string if it's a
52    unicode string. Otherwise just return the string.
53    """
54    return same_string_type_as(b"", string)
55
56
57def as_unicode(string):
58    """
59    Return the argument `string` converted to a unicode string if it's
60    a byte string. Otherwise just return the string.
61    """
62    return same_string_type_as("", string)
63
64
65def as_default_string(string):
66    """
67    Return the argument `string` converted to a the default string
68    type for the Python version. For unicode strings,
69    `LOSSLESS_ENCODING` is used for encoding or decoding.
70    """
71    return same_string_type_as(compat.default_string_type(), string)
72
73
74def encode_if_unicode(string, encoding):
75    """
76    Return the string `string`, encoded with `encoding` if `string` is
77    a unicode string. Otherwise return `string` unchanged.
78    """
79    if isinstance(string, compat.unicode_type):
80        return string.encode(encoding)
81    else:
82        return string
Note: See TracBrowser for help on using the repository browser.