Python version independent string and unicode handling.
This commit is contained in:
@@ -210,7 +210,7 @@ class ObjectJob:
|
||||
This will also create any TCs stored in the template.'''
|
||||
tcs = []
|
||||
if template:
|
||||
with open(unicode(template), 'rb') as fp:
|
||||
with open(PathUtil.toUnicode(template), 'rb') as fp:
|
||||
attrs = json.load(fp)
|
||||
|
||||
if attrs.get(JobTemplate.Version) and 1 == int(attrs[JobTemplate.Version]):
|
||||
|
||||
@@ -27,6 +27,7 @@ import FreeCADGui
|
||||
import PathScripts.PathJob as PathJob
|
||||
import PathScripts.PathLog as PathLog
|
||||
import PathScripts.PathStock as PathStock
|
||||
import PathScripts.PathUtil as PathUtil
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
@@ -274,7 +275,7 @@ class CommandJobTemplateExport:
|
||||
|
||||
encoded = job.Proxy.setupSheet.encodeTemplateAttributes(attrs)
|
||||
# write template
|
||||
with open(unicode(path), 'wb') as fp:
|
||||
with open(PathUtil.toUnicode(path), 'wb') as fp:
|
||||
json.dump(encoded, fp, sort_keys=True, indent=2)
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
import FreeCAD
|
||||
import Path
|
||||
import PathScripts.PathLog as PathLog
|
||||
import PathScripts.PathUtil as PathUtil
|
||||
import PySide
|
||||
|
||||
from PathScripts.PathGeom import PathGeom
|
||||
@@ -67,7 +68,7 @@ def _traverseTemplateAttributes(attrs, codec):
|
||||
elif type(value) == list:
|
||||
PathLog.debug("%s is a list" % key)
|
||||
coded[key] = [_traverseTemplateAttributes(attr, codec) for attr in value]
|
||||
elif type(value) == str or type(value) == unicode:
|
||||
elif PathUtil.isString(value):
|
||||
PathLog.debug("%s is a string" % key)
|
||||
coded[key] = codec(value)
|
||||
else:
|
||||
@@ -194,10 +195,10 @@ class SetupSheet:
|
||||
|
||||
def encodeAttributeString(self, attr):
|
||||
'''encodeAttributeString(attr) ... return the encoded string of a template attribute.'''
|
||||
return unicode(attr.replace(self.expressionReference(), self.TemplateReference))
|
||||
return PathUtil.toUnicode(attr.replace(self.expressionReference(), self.TemplateReference))
|
||||
def decodeAttributeString(self, attr):
|
||||
'''decodeAttributeString(attr) ... return the decoded string of a template attribute.'''
|
||||
return unicode(attr.replace(self.TemplateReference, self.expressionReference()))
|
||||
return PathUtil.toUnicode(attr.replace(self.TemplateReference, self.expressionReference()))
|
||||
|
||||
def encodeTemplateAttributes(self, attrs):
|
||||
'''encodeTemplateAttributes(attrs) ... return a dictionary with all values encoded.'''
|
||||
|
||||
@@ -29,6 +29,7 @@ import FreeCADGui
|
||||
import Path
|
||||
import PathScripts
|
||||
import PathScripts.PathLog as PathLog
|
||||
import PathScripts.PathUtil as PathUtil
|
||||
import PathScripts.PathUtils as PathUtils
|
||||
import json
|
||||
import os
|
||||
@@ -270,13 +271,13 @@ class ToolLibraryManager():
|
||||
parser = xml.sax.make_parser()
|
||||
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
|
||||
parser.setContentHandler(xmlHandler)
|
||||
parser.parse(unicode(filename[0]))
|
||||
parser.parse(PathUtil.toUnicode(filename[0]))
|
||||
if not xmlHandler.tooltable:
|
||||
return None
|
||||
|
||||
ht = xmlHandler.tooltable
|
||||
else:
|
||||
with open(unicode(filename[0]), "rb") as fp:
|
||||
with open(PathUtil.toUnicode(filename[0]), "rb") as fp:
|
||||
ht = self.tooltableFromAttrs(json.load(fp))
|
||||
|
||||
tt = self._findList(listname)
|
||||
@@ -299,7 +300,7 @@ class ToolLibraryManager():
|
||||
fext = os.path.splitext(name)[1].lower()
|
||||
if fext != ext:
|
||||
name = "{}{}".format(name, ext)
|
||||
return (open(unicode(name), 'wb'), name)
|
||||
return (open(PathUtil.toUnicode(name), 'wb'), name)
|
||||
|
||||
if filename[1] == self.TooltableTypeXML:
|
||||
fp,fname = openFileWithExtension(filename[0], '.xml')
|
||||
@@ -315,7 +316,7 @@ class ToolLibraryManager():
|
||||
json.dump(self.templateAttrs(tt), fp, sort_keys=True, indent=2)
|
||||
|
||||
fp.close()
|
||||
print("Written ", unicode(fname))
|
||||
print("Written ", PathUtil.toUnicode(fname))
|
||||
|
||||
except Exception as e:
|
||||
print("Could not write file:", e)
|
||||
|
||||
@@ -33,6 +33,7 @@ other than PathLog, then it probably doesn't belong here.
|
||||
'''
|
||||
|
||||
import PathScripts.PathLog as PathLog
|
||||
import sys
|
||||
|
||||
PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
|
||||
|
||||
@@ -96,3 +97,18 @@ Use this function to remove all expressions before deletion.'''
|
||||
if hasattr(obj, 'ExpressionEngine'):
|
||||
for attr,expr in obj.ExpressionEngine:
|
||||
obj.setExpression(attr, None)
|
||||
|
||||
def toUnicode(string):
|
||||
'''toUnicode(string) ... returns a unicode version of string regardless of the python version.'''
|
||||
if sys.version_info[0] < 3:
|
||||
return unicode(string)
|
||||
return string
|
||||
|
||||
def isString(string):
|
||||
'''isString(string) ... return True if string is a string, regardless of string type and python version.'''
|
||||
if type(string) == str:
|
||||
return True
|
||||
if sys.version_info[0] < 3 and type(string) == unicode:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user