Skip to content

Commit d4de9a0

Browse files
committed
Extracted utility methods to a new module
1 parent 92766e0 commit d4de9a0

File tree

2 files changed

+138
-91
lines changed

2 files changed

+138
-91
lines changed

javaobj/core.py

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import sys
4242

4343
from javaobj.modifiedutf8 import decode_modified_utf8
44+
from javaobj.utils import log_debug, log_error, read_to_str, to_bytes, to_str
4445

4546
try:
4647
# Python 2
@@ -85,97 +86,6 @@
8586

8687
# ------------------------------------------------------------------------------
8788

88-
# Setup the logger
89-
_log = logging.getLogger(__name__)
90-
91-
92-
def log_debug(message, ident=0):
93-
"""
94-
Logs a message at debug level
95-
96-
:param message: Message to log
97-
:param ident: Number of indentation spaces
98-
"""
99-
_log.debug(" " * (ident * 2) + str(message))
100-
101-
102-
def log_error(message, ident=0):
103-
"""
104-
Logs a message at error level
105-
106-
:param message: Message to log
107-
:param ident: Number of indentation spaces
108-
"""
109-
_log.error(" " * (ident * 2) + str(message))
110-
111-
# ------------------------------------------------------------------------------
112-
113-
if sys.version_info[0] >= 3:
114-
# Python 3 interpreter : bytes & str
115-
def to_bytes(data, encoding="UTF-8"):
116-
"""
117-
Converts the given string to an array of bytes.
118-
Returns the first parameter if it is already an array of bytes.
119-
120-
:param data: A unicode string
121-
:param encoding: The encoding of data
122-
:return: The corresponding array of bytes
123-
"""
124-
if type(data) is bytes:
125-
# Nothing to do
126-
return data
127-
return data.encode(encoding)
128-
129-
def to_str(data, encoding="UTF-8"):
130-
"""
131-
Converts the given parameter to a string.
132-
Returns the first parameter if it is already an instance of ``str``.
133-
134-
:param data: A string
135-
:param encoding: The encoding of data
136-
:return: The corresponding string
137-
"""
138-
if type(data) is str:
139-
# Nothing to do
140-
return data
141-
try:
142-
return str(data, encoding)
143-
except UnicodeDecodeError:
144-
return decode_modified_utf8(data)[0]
145-
146-
def read_to_str(data):
147-
"""
148-
Concats all bytes into a string
149-
"""
150-
return ''.join(chr(char) for char in data)
151-
152-
else:
153-
# Python 2 interpreter : str & unicode
154-
def to_str(data, encoding="UTF-8"):
155-
"""
156-
Converts the given parameter to a string.
157-
Returns the first parameter if it is already an instance of ``str``.
158-
159-
:param data: A string
160-
:param encoding: The encoding of data
161-
:return: The corresponding string
162-
"""
163-
if type(data) is str:
164-
# Nothing to do
165-
return data
166-
return data.encode(encoding)
167-
168-
# Same operation
169-
to_bytes = to_str
170-
171-
def read_to_str(data):
172-
"""
173-
Nothing to do in Python 2
174-
"""
175-
return data
176-
177-
# ------------------------------------------------------------------------------
178-
17989

18090
def load(file_object, *transformers, **kwargs):
18191
"""

javaobj/utils.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)