Unicode fixes for Python3

This commit is contained in:
cclauss
2019-01-04 12:18:25 +01:00
committed by wmayer
parent 0453ee71e6
commit e3545c8c06
12 changed files with 108 additions and 101 deletions

View File

@@ -39,6 +39,8 @@ This is the GUI part of the Draft module.
Report to Draft.py for info
'''
import six
import FreeCAD, FreeCADGui, os, Draft, sys, DraftVecUtils, math
try:
@@ -47,10 +49,7 @@ except ImportError:
FreeCAD.Console.PrintMessage("Error: Python-pyside package must be installed on your system to use the Draft module.")
try:
if sys.version_info.major >= 3:
_encoding = None
else:
_encoding = QtGui.QApplication.UnicodeUTF8
_encoding = QtGui.QApplication.UnicodeUTF8 if six.PY2 else None
def translate(context, text, utf8_decode=True):
"""convenience function for Qt translator
context: str
@@ -61,7 +60,7 @@ try:
if set to true utf8 encoded unicode will be returned. This option does not have influence
on python3 as for python3 we are returning utf-8 encoded unicode by default!
"""
if sys.version_info.major >= 3:
if six.PY3:
return QtGui.QApplication.translate(context, text, None)
elif utf8_decode:
return QtGui.QApplication.translate(context, text, None, _encoding)
@@ -79,7 +78,7 @@ except AttributeError:
if set to true utf8 encoded unicode will be returned. This option does not have influence
on python3 as for python3 we are returning utf-8 encoded unicode by default!
"""
if sys.version_info.major >= 3:
if six.PY3:
return QtGui.QApplication.translate(context, text, None)
elif QtCore.qVersion() > "4":
if utf8_decode:
@@ -153,12 +152,12 @@ class todo:
wrn = "[Draft.todo.tasks] Unexpected error:", sys.exc_info()[0], "in ", f, "(", arg, ")"
FreeCAD.Console.PrintWarning (wrn)
except ReferenceError:
print ("Debug: DraftGui.todo.doTasks: queue contains a deleted object, skipping")
print("Debug: DraftGui.todo.doTasks: queue contains a deleted object, skipping")
todo.itinerary = []
if todo.commitlist:
for name,func in todo.commitlist:
if sys.version_info.major < 3:
if isinstance(name,unicode):
if six.PY2:
if isinstance(name,six.text_type):
name = name.encode("utf8")
#print("debug: committing ",str(name))
try:

View File

@@ -1,4 +1,6 @@
import FreeCAD, math, sys, os, DraftVecUtils, WorkingPlane
import six
import FreeCAD, math, os, DraftVecUtils, WorkingPlane
import Part, DraftGeomUtils
from FreeCAD import Vector
from Draft import getType, getrgb, svgpatterns, gui
@@ -390,7 +392,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
svg = ""
for i in range(len(text)):
t = text[i]
if sys.version_info.major < 3 and (not isinstance(t,unicode)):
if six.PY2 and not isinstance(t, six.text_type):
t = t.decode("utf8")
# possible workaround if UTF8 is unsupported
# import unicodedata

View File

@@ -33,6 +33,8 @@
# Converter application to convert to/from DXF. Then the real work is done by
# importDXF
import six
if open.__module__ == '__builtin__':
pythonopen = open # to distinguish python built-in open function from the one declared here
@@ -101,8 +103,8 @@ def convertToDxf(dwgfilename):
basename = os.path.basename(dwgfilename)
cmdline = '"%s" "%s" "%s" "ACAD2000" "DXF" "0" "1" "%s"' % (teigha, indir, outdir, basename)
print("Converting: " + cmdline)
if sys.version_info.major < 3:
if isinstance(cmdline,unicode):
if six.PY2:
if isinstance(cmdline,six.text_type):
encoding = sys.getfilesystemencoding()
cmdline = cmdline.encode(encoding)
subprocess.call(cmdline, shell=True) #os.system(cmdline)

View File

@@ -48,6 +48,8 @@ texts, colors,layers (from groups)
TEXTSCALING = 1.35 # scaling factor between autocad font sizes and coin font sizes
CURRENTDXFLIB = 1.40 # the minimal version of the dxfLibrary needed to run
import six
import sys, FreeCAD, os, Part, math, re, string, Mesh, Draft, DraftVecUtils, DraftGeomUtils
from Draft import _Dimension, _ViewProviderDimension
from FreeCAD import Vector
@@ -128,8 +130,8 @@ Please either enable FreeCAD to download these libraries:
Or download these libraries manually, as explained on
https://github.com/yorikvanhavre/Draft-dxf-importer
To enabled FreeCAD to download these libraries, answer Yes.""")
if sys.version_info.major < 3:
if not isinstance(message,unicode):
if six.PY2:
if not isinstance(message,six.text_type):
message = message.decode('utf8')
reply = QtGui.QMessageBox.question(None,"",message,
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
@@ -206,7 +208,7 @@ def deformat(text):
#print(ss, type(ss))
if ss.startswith("U+"):
ucode = "0x"+ss[2:]
ns += unichr(eval(ucode)) #Python3 - unichr doesn't exist anymore
ns += six.unichr(eval(ucode)) # Python3 - unichr doesn't exist anymore
else:
try:
ns += ss.decode("utf8")
@@ -1566,14 +1568,13 @@ def warn(dxfobject,num=None):
def open(filename):
"called when freecad opens a file."
import sys
readPreferences()
if dxfUseLegacyImporter:
getDXFlibs()
if dxfReader:
docname = os.path.splitext(os.path.basename(filename))[0]
if sys.version_info.major < 3:
if isinstance(docname,unicode):
if six.py2:
if isinstance(docname,six.text_type):
#workaround since newDocument currently can't handle unicode filenames
docname = docname.encode(sys.getfilesystemencoding())
doc = FreeCAD.newDocument(docname)
@@ -1584,8 +1585,8 @@ def open(filename):
errorDXFLib(gui)
else:
docname = os.path.splitext(os.path.basename(filename))[0]
if sys.version_info.major < 3:
if isinstance(docname,unicode):
if six.PY2:
if isinstance(docname,six.text_type):
#workaround since newDocument currently can't handle unicode filenames
docname = docname.encode(sys.getfilesystemencoding())
doc = FreeCAD.newDocument(docname)
@@ -1606,8 +1607,8 @@ def insert(filename,docname):
getDXFlibs()
if dxfReader:
groupname = os.path.splitext(os.path.basename(filename))[0]
if sys.version_info.major < 3:
if isinstance(groupname,unicode):
if six.PY2:
if isinstance(groupname,six.text_type):
#workaround since newDocument currently can't handle unicode filenames
groupname = groupname.encode(sys.getfilesystemencoding())
importgroup = doc.addObject("App::DocumentObjectGroup",groupname)
@@ -1922,8 +1923,8 @@ def writePanelCut(ob,dxf,nospline,lwPoly,parent=None):
def getStrGroup(ob):
"gets a string version of the group name"
l = getGroup(ob)
if sys.version_info.major < 3:
if isinstance(l,unicode):
if six.PY2:
if isinstance(l,six.text_type):
# dxf R12 files are rather over-sensitive with utf8...
try:
import unicodedata
@@ -2134,8 +2135,8 @@ def export(objectslist,filename,nospline=False,lwPoly=False):
dxf.append(dxfLibrary.Dimension(pbase,p1,p2,color=getACI(ob),
layer=getStrGroup(ob)))
if sys.version_info.major < 3:
if isinstance(filename,unicode):
if six.PY2:
if isinstance(filename,six.text_type):
filename = filename.encode("utf8")
dxf.saveas(filename)