[Draft] Remove obsolete decode code (#9106)

This commit is contained in:
Roy-043
2023-03-28 21:03:33 +02:00
committed by GitHub
parent d65b05c3df
commit 87b0893044
7 changed files with 12 additions and 201 deletions

View File

@@ -845,7 +845,7 @@ class DraftToolBar:
def labelUi(self,title=translate("draft","Label"),callback=None):
w = QtGui.QWidget()
w.setWindowTitle(translate("draft","Label type", utf8_decode=True))
w.setWindowTitle(translate("draft","Label type"))
l = QtGui.QVBoxLayout(w)
combo = QtGui.QComboBox()
from draftobjects.label import get_label_types

View File

@@ -165,34 +165,16 @@ def get_text(plane, techdraw,
fontname, angle, base, flip)
if len(text) == 1:
try:
_t = text[0].replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
except Exception:
# TODO: trap only specific exception; what is the problem?
# Bad UTF8 string specification? This can be removed
# once the code is only used with Python 3.
_t = text[0].decode("utf8")
_t = _t.replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
_t = text[0].replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
else:
for i in range(len(text)):
if i == 0:
svg += '<tspan>'
else:
svg += '<tspan x="0" dy="{}">'.format(linespacing)
try:
_t = text[i].replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
except Exception:
# TODO: trap only specific exception; what is the problem?
# Bad UTF8 string specification? This can be removed
# once the code is only used with Python 3.
_t = text[i].decode("utf8")
_t = _t.replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
_t = text[i].replace("&", "&amp;").replace("<", "&lt;")
svg += _t.replace(">", "&gt;")
svg += '</tspan>\n'
svg += '</text>\n'
return svg

View File

@@ -43,7 +43,7 @@ except AttributeError:
_encoding = None
def translate(context, text, utf8_decode=False):
def translate(context, text):
r"""Translate the text using the Qt translate function.
It wraps around `QtGui.QApplication.translate`,
@@ -61,53 +61,24 @@ def translate(context, text, utf8_decode=False):
text: str
Text that will be translated. It could be a single word,
a full sentence, paragraph, or multiple paragraphs with new lines.
Usually the last endline character '\\n'
Usually the last endline character '\n'
that finishes the string doesn't need to be included
for translation.
utf8_decode: bool
It defaults to `False`.
This must be set to `True` to indicate that the `text`
is an `'utf8'` encoded string, so it should be returned as such.
This option is ignored when using Python 3
as with Python 3 all strings are `'utf8'` by default.
Returns
-------
str
A unicode string returned by `QtGui.QApplication.translate`.
If `utf8_decode` is `True`, the resulting string will be encoded
in `'utf8'`, and a `bytes` object will be returned.
::
Qtranslate = QtGui.QApplication.translate
return Qtranslate(context, text, None).encode("utf8")
Unicode strings
---------------
Whether it is Qt4 or Qt5, the `translate` function
always returns a unicode string.
The difference is how it handles the input.
Reference: https://pyside.github.io/docs/pyside/PySide/QtCore/
In Qt4 the translate function has a 4th parameter to define the encoding
of the input string.
>>> QtCore.QCoreApplication.translate(context, text, None, UnicodeUT8)
>>> QtGui.QApplication.translate(context, text, None, UnicodeUT8)
Reference: https://doc.qt.io/qtforpython/PySide2/QtCore
In Qt5 the strings are always assumed unicode, so the 4th parameter
is for a different use, and it is not used.
In Qt5 the strings are always assumed unicode
>>> QtCore.QCoreApplication.translate(context, text, None)
>>> QtGui.QApplication.translate(context, text, None)
"""
# Python 3 and Qt5
# The text is a utf8 string, and since it is Qt5
# the translate function doesn't use the 4th parameter
return Qtranslate(context, text, None)

View File

@@ -55,38 +55,6 @@ if open.__module__ in ['__builtin__', 'io']:
useDraftWire = True
def decodeName(name):
"""Decode encoded name.
Parameters
----------
name : str
The string to decode.
Returns
-------
tuple
(string)
A tuple containing the decoded `name` in 'latin1',
otherwise in 'utf8'.
If it fails it returns the original `name`.
"""
try:
decodedName = name
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
_msg = ("AirfoilDAT error: "
"couldn't determine character encoding.")
FCC.PrintError(translate("ImportAirfoilDAT", _msg) + "\n")
decodedName = name
return decodedName
def open(filename):
"""Open filename and parse.
@@ -102,7 +70,7 @@ def open(filename):
"""
docname = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(docname)
doc.Label = decodeName(docname)
doc.Label = docname
process(filename)
doc.recompute()
@@ -134,7 +102,6 @@ def insert(filename, docname):
obj = process(filename)
if obj is not None:
importgroup = doc.addObject("App::DocumentObjectGroup", groupname)
importgroup.Label = decodeName(groupname)
importgroup.Group = [obj]
doc.recompute()

View File

@@ -214,41 +214,10 @@ def prec():
return Draft.getParam("precision", 6)
def decodeName(name):
"""Decode the encoded name into utf8 or latin1.
Parameters
----------
name : str
The string to decode.
Returns
-------
str
The decoded string in utf8, latin1, or the original `name`
if the decoding was not needed, for example,
when using Python 3.
"""
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
print("dxf: error: couldn't determine character encoding")
decodedName = name
except AttributeError:
# this is python3 (nothing to do)
decodedName = name
return decodedName
def deformat(text):
"""Remove weird formats in texts and wipes UTF characters.
It removes `{}`, html codes, \\(U...) characters,
and decodes the string as utf8 or latin1 if needed.
For Python 3 there is no decoding needed.
Parameters
----------
@@ -267,19 +236,7 @@ def deformat(text):
t = re.sub("\\\.*?;", "", t)
# replace UTF codes by utf chars
sts = re.split("\\\\(U\+....)", t)
ns = u""
for ss in sts:
try:
ns += ss.decode("utf8")
except UnicodeError:
try:
ns += ss.decode("latin1")
except UnicodeError:
print("unable to decode text: ", text)
except AttributeError:
# this is python3 (nothing to do)
ns += ss
t = ns
t = u"".join(sts)
# replace degrees, diameters chars
t = re.sub('%%d', u'°', t)
t = re.sub('%%c', u'Ø', t)
@@ -2038,16 +1995,6 @@ def addText(text, attrib=False):
else:
name = "Text"
val = deformat(val)
# the following stores text as Latin1 in annotations, which
# displays ok in coin texts, but causes errors later on.
# better store as utf8 always.
# try:
# val = val.decode("utf8").encode("Latin1")
# except Exception:
# try:
# val = val.encode("latin1")
# except Exception:
# pass
newob = Draft.make_text(val.split("\n"))
if hasattr(lay, "addObject"):
lay.addObject(newob)
@@ -2770,7 +2717,7 @@ def open(filename):
if dxfReader:
docname = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(docname)
doc.Label = decodeName(docname)
doc.Label = docname
processdxf(doc, filename)
return doc
else:
@@ -2778,7 +2725,7 @@ def open(filename):
else:
docname = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(docname)
doc.Label = decodeName(docname)
doc.Label = docname
FreeCAD.setActiveDocument(doc.Name)
import Import
Import.readDXF(filename)
@@ -2816,7 +2763,6 @@ def insert(filename, docname):
if dxfReader:
groupname = os.path.splitext(os.path.basename(filename))[0]
importgroup = doc.addObject("App::DocumentObjectGroup", groupname)
importgroup.Label = decodeName(groupname)
processdxf(doc, filename)
for l in layers:
importgroup.addObject(l)

View File

@@ -333,35 +333,6 @@ def parse(filename, doc):
float(c[3])/255)
def decodeName(name):
"""Decode encoded name.
Parameters
----------
name : str
The string to decode.
Returns
-------
tuple
(string)
A tuple containing the decoded `name` in 'utf8', otherwise in 'latin1'.
If it fails it returns the original `name`.
"""
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
FCC.PrintError(translate("importOCA",
"OCA error: "
"couldn't determine character encoding")
+ "\n")
decodedName = name
return decodedName
def open(filename):
"""Open filename and parse.

View File

@@ -1631,32 +1631,6 @@ class svgHandler(xml.sax.ContentHandler):
# class svgHandler
def decodeName(name):
"""Decode encoded name.
Parameters
----------
name : str
The string to decode.
Returns
-------
tuple
(string)
A tuple containing the decoded `name` in 'utf8', otherwise in 'latin1'.
If it fails it returns the original `name`.
"""
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
_err("SVG error: couldn't determine character encoding")
decodedName = name
return decodedName
def getContents(filename, tag, stringmode=False):
"""Get the contents of all occurrences of the given tag in the file.