|
| 1 | +#!/usr/bin/python |
| 2 | +# -- Content-Encoding: utf-8 -- |
| 3 | +""" |
| 4 | +Provides utility methods used by the core implementation of javaobj. |
| 5 | +
|
| 6 | +Namely: logging methods, bytes/str/unicode converters |
| 7 | +
|
| 8 | +:authors: Thomas Calmant |
| 9 | +:license: Apache License 2.0 |
| 10 | +:version: 0.3.0 |
| 11 | +:status: Alpha |
| 12 | +
|
| 13 | +.. |
| 14 | +
|
| 15 | + Copyright 2019 Thomas Calmant |
| 16 | +
|
| 17 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 | + you may not use this file except in compliance with the License. |
| 19 | + You may obtain a copy of the License at |
| 20 | +
|
| 21 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | +
|
| 23 | + Unless required by applicable law or agreed to in writing, software |
| 24 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | + See the License for the specific language governing permissions and |
| 27 | + limitations under the License. |
| 28 | +""" |
| 29 | + |
| 30 | +# Standard library |
| 31 | +import logging |
| 32 | +import sys |
| 33 | + |
| 34 | +# Modified UTF-8 parser |
| 35 | +from javaobj.modifiedutf8 import decode_modified_utf8 |
| 36 | + |
| 37 | +# ------------------------------------------------------------------------------ |
| 38 | + |
| 39 | +# Module version |
| 40 | +__version_info__ = (0, 3, 0) |
| 41 | +__version__ = ".".join(str(x) for x in __version_info__) |
| 42 | + |
| 43 | +# Documentation strings format |
| 44 | +__docformat__ = "restructuredtext en" |
| 45 | + |
| 46 | +# ------------------------------------------------------------------------------ |
| 47 | + |
| 48 | +# Setup the logger |
| 49 | +_log = logging.getLogger("javaobj") |
| 50 | + |
| 51 | + |
| 52 | +def log_debug(message, ident=0): |
| 53 | + """ |
| 54 | + Logs a message at debug level |
| 55 | +
|
| 56 | + :param message: Message to log |
| 57 | + :param ident: Number of indentation spaces |
| 58 | + """ |
| 59 | + _log.debug("%s%s", " " * (ident * 2), message) |
| 60 | + |
| 61 | + |
| 62 | +def log_error(message, ident=0): |
| 63 | + """ |
| 64 | + Logs a message at error level |
| 65 | +
|
| 66 | + :param message: Message to log |
| 67 | + :param ident: Number of indentation spaces |
| 68 | + """ |
| 69 | + _log.error("%s%s", " " * (ident * 2), message) |
| 70 | + |
| 71 | + |
| 72 | +# ------------------------------------------------------------------------------ |
| 73 | + |
| 74 | +if sys.version_info[0] >= 3: |
| 75 | + # Python 3 interpreter : bytes & str |
| 76 | + def to_bytes(data, encoding="UTF-8"): |
| 77 | + """ |
| 78 | + Converts the given string to an array of bytes. |
| 79 | + Returns the first parameter if it is already an array of bytes. |
| 80 | +
|
| 81 | + :param data: A unicode string |
| 82 | + :param encoding: The encoding of data |
| 83 | + :return: The corresponding array of bytes |
| 84 | + """ |
| 85 | + if type(data) is bytes: |
| 86 | + # Nothing to do |
| 87 | + return data |
| 88 | + return data.encode(encoding) |
| 89 | + |
| 90 | + def to_str(data, encoding="UTF-8"): |
| 91 | + """ |
| 92 | + Converts the given parameter to a string. |
| 93 | + Returns the first parameter if it is already an instance of ``str``. |
| 94 | +
|
| 95 | + :param data: A string |
| 96 | + :param encoding: The encoding of data |
| 97 | + :return: The corresponding string |
| 98 | + """ |
| 99 | + if type(data) is str: |
| 100 | + # Nothing to do |
| 101 | + return data |
| 102 | + try: |
| 103 | + return str(data, encoding) |
| 104 | + except UnicodeDecodeError: |
| 105 | + return decode_modified_utf8(data)[0] |
| 106 | + |
| 107 | + def read_to_str(data): |
| 108 | + """ |
| 109 | + Concats all bytes into a string |
| 110 | + """ |
| 111 | + return "".join(chr(char) for char in data) |
| 112 | + |
| 113 | + |
| 114 | +else: |
| 115 | + # Python 2 interpreter : str & unicode |
| 116 | + def to_str(data, encoding="UTF-8"): |
| 117 | + """ |
| 118 | + Converts the given parameter to a string. |
| 119 | + Returns the first parameter if it is already an instance of ``str``. |
| 120 | +
|
| 121 | + :param data: A string |
| 122 | + :param encoding: The encoding of data |
| 123 | + :return: The corresponding string |
| 124 | + """ |
| 125 | + if type(data) is str: |
| 126 | + # Nothing to do |
| 127 | + return data |
| 128 | + return data.encode(encoding) |
| 129 | + |
| 130 | + # Same operation |
| 131 | + to_bytes = to_str |
| 132 | + |
| 133 | + def read_to_str(data): |
| 134 | + """ |
| 135 | + Nothing to do in Python 2 |
| 136 | + """ |
| 137 | + return data |
0 commit comments