Skip to content

Commit 1410828

Browse files
committed
Remove py2 unicode usage
1 parent 97d28fe commit 1410828

13 files changed

+42
-120
lines changed

odml/dtypes.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
self = sys.modules[__name__].__dict__
1212

13-
try:
14-
unicode = unicode
15-
except NameError:
16-
unicode = str
17-
1813
FORMAT_DATE = "%Y-%m-%d"
1914
FORMAT_DATETIME = "%Y-%m-%d %H:%M:%S"
2015
FORMAT_TIME = "%H:%M:%S"
@@ -103,7 +98,7 @@ def valid_type(dtype):
10398
if dtype is None:
10499
return True
105100

106-
if not isinstance(dtype, str) and not isinstance(dtype, unicode):
101+
if not isinstance(dtype, str):
107102
return False
108103

109104
dtype = dtype.lower()
@@ -158,7 +153,7 @@ def set(value, dtype=None):
158153
if isinstance(value, str):
159154
return str_set(value)
160155
else:
161-
if isinstance(value, (str, unicode)):
156+
if isinstance(value, str):
162157
return str_set(value)
163158
return self.get(dtype + "_set", str_set)(value)
164159

@@ -206,9 +201,6 @@ def str_get(string):
206201
if string in [None, "", [], {}]:
207202
return default_values("string")
208203

209-
if sys.version_info < (3, 0):
210-
return unicode(string)
211-
212204
return str(string)
213205

214206

@@ -296,7 +288,7 @@ def boolean_get(string):
296288
if string in [None, "", [], {}]:
297289
return default_values("boolean")
298290

299-
if isinstance(string, (unicode, str)):
291+
if isinstance(string, str):
300292
string = string.lower()
301293

302294
truth = ["true", "1", True, "t"] # be kind, spec only accepts True / False

odml/property.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ def odml_tuple_import(t_count, new_value):
2929
:param new_value: string containing an odml style tuple list.
3030
:return: list of odml style tuples.
3131
"""
32-
try:
33-
unicode = unicode
34-
except NameError:
35-
unicode = str
36-
3732
if not isinstance(new_value, (list, tuple)) and \
3833
not isinstance(new_value[0], (list, tuple)):
3934
new_value = [new_value]
@@ -48,9 +43,9 @@ def odml_tuple_import(t_count, new_value):
4843
n_val_str += str(tuple_val) + "; "
4944
return_value += [n_val_str[:-2] + ")"]
5045
else:
51-
#non-unicode handling needed for python2
52-
if len(n_val) != 1 and not isinstance(n_val[0], unicode):
53-
n_val = n_val.encode('utf-8')
46+
# non-unicode handling needed for python2
47+
# if len(n_val) != 1 and not isinstance(n_val[0], unicode):
48+
# n_val = n_val.encode('utf-8')
5449
cln = n_val.strip()
5550
br_check = cln.count("(") == cln.count(")")
5651
sep_check = t_count == 1 or cln.count("(") == (cln.count(";") / (t_count - 1))

odml/scripts/odml_convert.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737

3838
from odml.tools.converters import VersionConverter as VerConf
3939

40-
try:
41-
unicode = unicode
42-
except NameError:
43-
unicode = str
44-
4540

4641
def run_conversion(file_list, output_dir, report, source_format="XML"):
4742
"""
@@ -56,7 +51,7 @@ def run_conversion(file_list, output_dir, report, source_format="XML"):
5651
# Exceptions are kept as broad as possible to ignore any non-odML or
5752
# invalid odML files and ensuring everything that can be will be converted.
5853
for curr_file in file_list:
59-
file_path = unicode(curr_file.absolute())
54+
file_path = str(curr_file.absolute())
6055
report.write("[Info] Handling file '%s'\n" % file_path)
6156
# When loading the current file succeeds, it is
6257
# a recent odML format file and can be ignored.

odml/scripts/odml_to_rdf.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
from odml.tools.odmlparser import ODMLReader, ODMLWriter
4040
from odml.tools.converters import VersionConverter as VerConf
4141

42-
try:
43-
unicode = unicode
44-
except NameError:
45-
unicode = str
46-
4742

4843
def run_rdf_export(odml_file, export_dir):
4944
"""
@@ -75,7 +70,7 @@ def run_conversion(file_list, output_dir, rdf_dir, report, source_format="XML"):
7570
# Exceptions are kept as broad as possible to ignore any non-odML or
7671
# invalid odML files and ensuring everything that can be will be converted.
7772
for curr_file in file_list:
78-
file_path = unicode(curr_file.absolute())
73+
file_path = str(curr_file.absolute())
7974
report.write("[Info] Handling file '%s'\n" % file_path)
8075
# When loading the current file succeeds, it is
8176
# a recent odML format file and can be exported

odml/tools/converters/format_converter.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
from . import VersionConverter
3434
from ..parser_utils import RDF_CONVERSION_FORMATS
3535

36-
try:
37-
unicode = unicode
38-
except NameError:
39-
unicode = str
40-
4136

4237
CONVERSION_FORMATS = copy.deepcopy(RDF_CONVERSION_FORMATS)
4338
CONVERSION_FORMATS.update({

odml/tools/converters/version_converter.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
from ...info import FORMAT_VERSION
2020
from ...terminology import Terminologies, REPOSITORY_BASE
2121

22-
try:
23-
unicode = unicode
24-
except NameError:
25-
unicode = str
26-
2722

2823
class VersionConverter(object):
2924
"""

odml/tools/odmlparser.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
from .rdf_converter import RDFReader, RDFWriter
2222
from ..validation import Validation
2323

24-
try:
25-
unicode = unicode
26-
except NameError:
27-
unicode = str
28-
2924

3025
class ODMLWriter:
3126
"""
@@ -108,7 +103,9 @@ def to_string(self, odml_document, **kwargs):
108103
"""
109104
string_doc = ''
110105

111-
if self.parser == "RDF":
106+
if self.parser == 'XML':
107+
string_doc = str(xmlparser.XMLWriter(odml_document))
108+
elif self.parser == "RDF":
112109
rdf_format = "xml"
113110
if "rdf_format" in kwargs and isinstance(kwargs["rdf_format"], str):
114111
rdf_format = kwargs["rdf_format"]

odml/tools/rdf_converter.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
from .dict_parser import DictReader
2323
from .parser_utils import ParserException, RDF_CONVERSION_FORMATS
2424

25-
try:
26-
unicode = unicode
27-
except NameError:
28-
unicode = str
29-
3025
ODML_NS = Format.namespace()
3126

3227

@@ -129,7 +124,7 @@ def save_odml_values(self, parent_node, rdf_predicate, values):
129124
to the current parent node.
130125
:param values: list of odml values.
131126
"""
132-
seq = URIRef(ODML_NS + unicode(uuid.uuid4()))
127+
seq = URIRef(ODML_NS + str(uuid.uuid4()))
133128
self.graph.add((seq, RDF.type, RDF.Seq))
134129
self.graph.add((parent_node, rdf_predicate, seq))
135130

@@ -138,14 +133,14 @@ def save_odml_values(self, parent_node, rdf_predicate, values):
138133
# Once rdflib upgrades this should be reversed to RDF:li again!
139134
# see https://github.com/RDFLib/rdflib/issues/280
140135
# -- keep until supported
141-
# bag = URIRef(ODML_NS + unicode(uuid.uuid4()))
136+
# bag = URIRef(ODML_NS + str(uuid.uuid4()))
142137
# self.graph.add((bag, RDF.type, RDF.Bag))
143138
# self.graph.add((curr_node, fmt.rdf_map(k), bag))
144139
# for curr_val in values:
145140
# self.graph.add((bag, RDF.li, Literal(curr_val)))
146141
counter = 1
147142
for curr_val in values:
148-
custom_predicate = "%s_%s" % (unicode(RDF), counter)
143+
custom_predicate = "%s_%s" % (str(RDF), counter)
149144
self.graph.add((seq, URIRef(custom_predicate), Literal(curr_val)))
150145
counter = counter + 1
151146

@@ -160,7 +155,7 @@ def save_odml_list(self, parent_node, rdf_predicate, odml_list):
160155
:param odml_list: list of odml entities.
161156
"""
162157
for curr_item in odml_list:
163-
node = URIRef(ODML_NS + unicode(curr_item.id))
158+
node = URIRef(ODML_NS + str(curr_item.id))
164159
self.graph.add((parent_node, rdf_predicate, node))
165160

166161
fmt = curr_item.format()
@@ -185,7 +180,7 @@ def save_repository_node(self, parent_node, rdf_predicate, leaf_value):
185180
if not terminology_node:
186181
# adding terminology url value to the graph and linking it
187182
# to the current RDF node.
188-
terminology_node = URIRef(ODML_NS + unicode(uuid.uuid4()))
183+
terminology_node = URIRef(ODML_NS + str(uuid.uuid4()))
189184
self.graph.add((terminology_node, RDF.type, URIRef(leaf_value)))
190185
self.graph.add((self.hub_root, ODML_NS.hasTerminology, terminology_node))
191186

@@ -203,7 +198,7 @@ def save_document(self, doc, curr_node=None):
203198
fmt = doc.format()
204199

205200
if not curr_node:
206-
curr_node = URIRef(ODML_NS + unicode(doc.id))
201+
curr_node = URIRef(ODML_NS + str(doc.id))
207202

208203
self.graph.add((curr_node, RDF.type, URIRef(fmt.rdf_type)))
209204
self.graph.add((self.hub_root, ODML_NS.hasDocument, curr_node))
@@ -478,7 +473,7 @@ def parse_document(self, doc_uri):
478473
elif attr[0] == "id":
479474
doc_attrs[attr[0]] = doc_uri.split("#", 1)[1]
480475
elif elems:
481-
doc_attrs[attr[0]] = unicode(elems[0].toPython())
476+
doc_attrs[attr[0]] = str(elems[0].toPython())
482477

483478
return {'Document': doc_attrs, 'odml-version': FORMAT_VERSION}
484479

@@ -503,7 +498,7 @@ def parse_section(self, sec_uri):
503498
elif attr[0] == "id":
504499
sec_attrs[attr[0]] = sec_uri.split("#", 1)[1]
505500
elif elems:
506-
sec_attrs[attr[0]] = unicode(elems[0].toPython())
501+
sec_attrs[attr[0]] = str(elems[0].toPython())
507502

508503
self._check_mandatory_attrs(sec_attrs)
509504
return sec_attrs
@@ -537,7 +532,7 @@ def parse_property(self, prop_uri):
537532
elif attr[0] == "id":
538533
prop_attrs[attr[0]] = prop_uri.split("#", 1)[1]
539534
elif elems:
540-
prop_attrs[attr[0]] = unicode(elems[0].toPython())
535+
prop_attrs[attr[0]] = str(elems[0].toPython())
541536

542537
self._check_mandatory_attrs(prop_attrs)
543538
return prop_attrs

odml/tools/xmlparser.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
from ..info import FORMAT_VERSION
2525
from .parser_utils import InvalidVersionException, ParserException, odml_tuple_export
2626

27-
try:
28-
unicode = unicode
29-
except NameError:
30-
unicode = str
31-
3227

3328
XML_HEADER = """<?xml version="1.0" encoding="UTF-8"?>"""
3429
EXTERNAL_STYLE_HEADER = """<?xml-stylesheet type="text/xsl" href="odmlDocument.xsl"?>"""
@@ -86,7 +81,7 @@ def to_csv(val):
8681
"""
8782
# Make sure all individual values do not contain
8883
# leading or trailing whitespaces.
89-
unicode_values = list(map(unicode.strip, map(unicode, val)))
84+
unicode_values = list(map(str.strip, map(str, val)))
9085
stream = StringIO()
9186
writer = csv.writer(stream, dialect="excel")
9287
writer.writerow(unicode_values)
@@ -172,10 +167,7 @@ def save_element(curr_el):
172167
ele = XMLWriter.save_element(curr_val)
173168
cur.append(ele)
174169
else:
175-
if sys.version_info < (3,):
176-
ele = E(k, unicode(val))
177-
else:
178-
ele = E(k, str(val))
170+
ele = E(k, str(val))
179171
cur.append(ele)
180172
return cur
181173

@@ -205,10 +197,7 @@ def write_file(self, filename, local_style=False, custom_template=None):
205197
tag: '<xsl:template match="odML">[custom]</xsl:template>'.
206198
"""
207199
# calculate the data before opening the file in case we get any exception
208-
if sys.version_info < (3,):
209-
data = unicode(self).encode('utf-8')
210-
else:
211-
data = str(self)
200+
data = str(self)
212201

213202
with open(filename, "w") as file:
214203
file.write("%s\n" % XML_HEADER)
@@ -306,7 +295,7 @@ def from_file(self, xml_file):
306295
doc = self.parse_element(root)
307296

308297
# Provide original file name via the in memory document
309-
if isinstance(xml_file, unicode):
298+
if isinstance(xml_file, str):
310299
doc.origin_file_name = basename(xml_file)
311300

312301
return doc

odml/validation.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
from . import dtypes
1111

12-
try:
13-
unicode = unicode
14-
except NameError:
15-
unicode = str
16-
1712
LABEL_ERROR = 'error'
1813
LABEL_WARNING = 'warning'
1914

@@ -92,7 +87,7 @@ def path(self):
9287

9388
def __repr__(self):
9489
# Cleanup the odml object print strings
95-
print_str = unicode(self.obj).split()[0].split("[")[0].split(":")[0]
90+
print_str = str(self.obj).split()[0].split("[")[0].split(":")[0]
9691
# Document has no name attribute and should not print id or name info
9792
if hasattr(self.obj, "name"):
9893
if self.obj.name and self.obj.name != self.obj.id:

0 commit comments

Comments
 (0)