From 87b0893044f16da41aa84a500d7faaa895a953aa Mon Sep 17 00:00:00 2001 From: Roy-043 <70520633+Roy-043@users.noreply.github.com> Date: Tue, 28 Mar 2023 21:03:33 +0200 Subject: [PATCH] [Draft] Remove obsolete decode code (#9106) --- src/Mod/Draft/DraftGui.py | 2 +- src/Mod/Draft/draftfunctions/svgtext.py | 26 ++--------- src/Mod/Draft/draftutils/translate.py | 35 ++------------- src/Mod/Draft/importAirfoilDAT.py | 35 +-------------- src/Mod/Draft/importDXF.py | 60 ++----------------------- src/Mod/Draft/importOCA.py | 29 ------------ src/Mod/Draft/importSVG.py | 26 ----------- 7 files changed, 12 insertions(+), 201 deletions(-) diff --git a/src/Mod/Draft/DraftGui.py b/src/Mod/Draft/DraftGui.py index df30512ac2..f218ed8a90 100644 --- a/src/Mod/Draft/DraftGui.py +++ b/src/Mod/Draft/DraftGui.py @@ -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 diff --git a/src/Mod/Draft/draftfunctions/svgtext.py b/src/Mod/Draft/draftfunctions/svgtext.py index b470a42eca..c696496f7d 100644 --- a/src/Mod/Draft/draftfunctions/svgtext.py +++ b/src/Mod/Draft/draftfunctions/svgtext.py @@ -165,34 +165,16 @@ def get_text(plane, techdraw, fontname, angle, base, flip) if len(text) == 1: - try: - _t = text[0].replace("&", "&").replace("<", "<") - svg += _t.replace(">", ">") - 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("&", "&").replace("<", "<") - svg += _t.replace(">", ">") + _t = text[0].replace("&", "&").replace("<", "<") + svg += _t.replace(">", ">") else: for i in range(len(text)): if i == 0: svg += '' else: svg += ''.format(linespacing) - - try: - _t = text[i].replace("&", "&").replace("<", "<") - svg += _t.replace(">", ">") - 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("&", "&").replace("<", "<") - svg += _t.replace(">", ">") - + _t = text[i].replace("&", "&").replace("<", "<") + svg += _t.replace(">", ">") svg += '\n' svg += '\n' return svg diff --git a/src/Mod/Draft/draftutils/translate.py b/src/Mod/Draft/draftutils/translate.py index d6d0f0a0ff..04e7176650 100644 --- a/src/Mod/Draft/draftutils/translate.py +++ b/src/Mod/Draft/draftutils/translate.py @@ -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) diff --git a/src/Mod/Draft/importAirfoilDAT.py b/src/Mod/Draft/importAirfoilDAT.py index 2b4738cc12..0cbfe58546 100644 --- a/src/Mod/Draft/importAirfoilDAT.py +++ b/src/Mod/Draft/importAirfoilDAT.py @@ -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() diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index eb005d5e0b..03124e41c3 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -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) diff --git a/src/Mod/Draft/importOCA.py b/src/Mod/Draft/importOCA.py index 8935aa4d16..bf5ec34ce6 100644 --- a/src/Mod/Draft/importOCA.py +++ b/src/Mod/Draft/importOCA.py @@ -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. diff --git a/src/Mod/Draft/importSVG.py b/src/Mod/Draft/importSVG.py index 216e927663..32ddbbc792 100644 --- a/src/Mod/Draft/importSVG.py +++ b/src/Mod/Draft/importSVG.py @@ -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.