source: ftputil/tool.py @ 1721:3557f65ded13

Last change on this file since 1721:3557f65ded13 was 1721:3557f65ded13, checked in by Stefan Schwarzer <sschwarzer@…>, 4 months ago
Remove `compat.py` This module was for Python 2/3 compatibility, similar to the `six` package. Since the next version of ftputil will only support Python 3, hardcode the types from `compat.py` in the code that used to use the `compat` module. There may still be redundant code that isn't needed when running the tests under Python 3.
File size: 2.3 KB
Line 
1# Copyright (C) 2013-2018, 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
9__all__ = ["same_string_type_as", "as_bytes", "as_unicode",
10           "as_default_string"]
11
12
13# Encoding to convert between byte string and unicode string. This is
14# a "lossless" encoding: Strings can be encoded/decoded back and forth
15# without information loss or causing encoding-related errors. The
16# `ftplib` module under Python 3 also uses the "latin1" encoding
17# internally. It's important to use the same encoding here, so that users who
18# used `ftplib` to create FTP items with non-ASCII characters can access them
19# in the same way with ftputil.
20LOSSLESS_ENCODING = "latin1"
21
22
23def same_string_type_as(type_source, content_source):
24    """
25    Return a string of the same type as `type_source` with the content
26    from `content_source`.
27
28    If the `type_source` and `content_source` don't have the same
29    type, use `LOSSLESS_ENCODING` above to encode or decode, whatever
30    operation is needed.
31    """
32    if (
33      isinstance(type_source, bytes) and isinstance(content_source, str)):
34        return content_source.encode(LOSSLESS_ENCODING)
35    elif (
36      isinstance(type_source, str) and isinstance(content_source, bytes)):
37        return content_source.decode(LOSSLESS_ENCODING)
38    else:
39        return content_source
40
41
42def as_bytes(string):
43    """
44    Return the argument `string` converted to a byte string if it's a
45    unicode string. Otherwise just return the string.
46    """
47    return same_string_type_as(b"", string)
48
49
50def as_unicode(string):
51    """
52    Return the argument `string` converted to a unicode string if it's
53    a byte string. Otherwise just return the string.
54    """
55    return same_string_type_as("", string)
56
57
58def as_default_string(string):
59    """
60    Return the argument `string` converted to a the default string
61    type for the Python version. For unicode strings,
62    `LOSSLESS_ENCODING` is used for encoding or decoding.
63    """
64    return same_string_type_as("", string)
65
66
67def encode_if_unicode(string, encoding):
68    """
69    Return the string `string`, encoded with `encoding` if `string` is
70    a unicode string. Otherwise return `string` unchanged.
71    """
72    if isinstance(string, str):
73        return string.encode(encoding)
74    else:
75        return string
Note: See TracBrowser for help on using the repository browser.