Start: Better handling of unicode on py2

This commit is contained in:
Yorik van Havre
2018-12-29 14:41:25 -02:00
parent 91a61d4813
commit e83c44200a

View File

@@ -36,6 +36,17 @@ iconbank = {} # to store already created icons so we don't overpollute the temp
tempfolder = None # store icons inside a subfolder in temp dir
defaulticon = None # store a default icon for problematic file types
def encode(text):
"make sure we are always working with unicode in python2"
if sys.version_info.major < 3:
if not isinstance(text,unicode):
return text.decode("utf8")
return text
def gethexcolor(color):
"returns a color hex value #000000"
@@ -187,7 +198,7 @@ def buildCard(filename,method,arg=None):
"builds a html <li> element representing a file. method is a script + a keyword, for ex. url.py?key="
result = ""
result = encode("")
if os.path.exists(filename) and isOpenableByFreeCAD(filename):
basename = os.path.basename(filename)
if not arg:
@@ -197,10 +208,10 @@ def buildCard(filename,method,arg=None):
image = finfo[0]
size = finfo[1]
author = finfo[2]
infostring = TranslationTexts.T_CREATIONDATE+": "+finfo[3]+"\n"
infostring += TranslationTexts.T_LASTMODIFIED+": "+finfo[4]
infostring = encode(TranslationTexts.T_CREATIONDATE+": "+finfo[3]+"\n")
infostring += encode(TranslationTexts.T_LASTMODIFIED+": "+finfo[4])
if finfo[5]:
infostring += "\n\n" + finfo[5]
infostring += "\n\n" + encode(finfo[5])
if size:
result += '<a href="'+method+arg+'" title="'+infostring+'">'
result += '<li class="icon">'
@@ -251,6 +262,7 @@ def handle():
CSS = f.read()
HTML = HTML.replace("JS",JS)
HTML = HTML.replace("CSS",CSS)
HTML = encode(HTML)
# get the stylesheet if we are using one
@@ -258,25 +270,20 @@ def handle():
qssfile = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/MainWindow").GetString("StyleSheet","")
if qssfile:
with open(qssfile, 'r') as f:
ALTCSS = f.read()
if sys.version_info.major < 3:
ALTCSS = ALTCSS.decode("utf8")
ALTCSS = encode(f.read())
HTML = HTML.replace("<!--QSS-->","<style type=\"text/css\">"+ALTCSS+"</style>")
# get FreeCAD version
v = FreeCAD.Version()
VERSIONSTRING = TranslationTexts.T_VERSION + " " + v[0] + "." + v[1] + " " + TranslationTexts.T_BUILD + " " + v[2]
VERSIONSTRING = encode(TranslationTexts.T_VERSION + " " + v[0] + "." + v[1] + " " + TranslationTexts.T_BUILD + " " + v[2])
HTML = HTML.replace("VERSIONSTRING",VERSIONSTRING)
# translate texts
texts = [t for t in dir(TranslationTexts) if t.startswith("T_")]
for text in texts:
if sys.version_info.major < 3:
HTML = HTML.replace(text,getattr(TranslationTexts,text).decode("utf8"))
else:
HTML = HTML.replace(text,getattr(TranslationTexts,text))
HTML = HTML.replace(text,encode(getattr(TranslationTexts,text)))
# build a "create new" icon with the FreeCAD background color gradient
@@ -297,60 +304,54 @@ def handle():
# build SECTION_RECENTFILES
SECTION_RECENTFILES = ""
SECTION_RECENTFILES = encode("")
rf = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/RecentFiles")
rfcount = rf.GetInt("RecentFiles",0)
if rfcount:
SECTION_RECENTFILES = "<h2>"+TranslationTexts.T_RECENTFILES+"</h2>"
SECTION_RECENTFILES = encode("<h2>"+TranslationTexts.T_RECENTFILES+"</h2>")
SECTION_RECENTFILES += "<ul>"
SECTION_RECENTFILES += '<a href="LoadNew.py" title="'+TranslationTexts.T_CREATENEW+'">'
SECTION_RECENTFILES += '<a href="LoadNew.py" title="'+encode(TranslationTexts.T_CREATENEW)+'">'
SECTION_RECENTFILES += '<li class="icon">'
if FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Start").GetBool("NewFileGradient",False):
SECTION_RECENTFILES += '<img src="'+iconbank["createimg"]+'">'
SECTION_RECENTFILES += '<img src="'+encode(iconbank["createimg"])+'">'
else:
SECTION_RECENTFILES += '<img src="images/new_file_thumbnail.svg">'
SECTION_RECENTFILES += '<div class="caption">'
SECTION_RECENTFILES += '<h4>'+TranslationTexts.T_CREATENEW+'</h4>'
SECTION_RECENTFILES += '<h4>'+encode(TranslationTexts.T_CREATENEW)+'</h4>'
SECTION_RECENTFILES += '</div>'
SECTION_RECENTFILES += '</li>'
SECTION_RECENTFILES += '</a>'
for i in range(rfcount):
filename = rf.GetString("MRU%d" % (i))
SECTION_RECENTFILES += buildCard(filename,method="LoadMRU.py?MRU=",arg=str(i))
SECTION_RECENTFILES += encode(buildCard(filename,method="LoadMRU.py?MRU=",arg=str(i)))
SECTION_RECENTFILES += '</ul>'
if sys.version_info.major < 3:
SECTION_RECENTFILES = SECTION_RECENTFILES.decode("utf8")
HTML = HTML.replace("SECTION_RECENTFILES",SECTION_RECENTFILES)
# build SECTION_EXAMPLES
SECTION_EXAMPLES = ""
SECTION_EXAMPLES = encode("")
if FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Start").GetBool("ShowExamples",True):
SECTION_EXAMPLES = "<h2>"+TranslationTexts.T_EXAMPLES+"</h2>"
SECTION_EXAMPLES = encode("<h2>"+TranslationTexts.T_EXAMPLES+"</h2>")
SECTION_EXAMPLES += "<ul>"
for basename in os.listdir(FreeCAD.getResourceDir()+"examples"):
filename = FreeCAD.getResourceDir()+"examples"+os.sep+basename
SECTION_EXAMPLES += buildCard(filename,method="LoadExample.py?filename=")
SECTION_EXAMPLES += encode(buildCard(filename,method="LoadExample.py?filename="))
SECTION_EXAMPLES += "</ul>"
if sys.version_info.major < 3:
SECTION_EXAMPLES = SECTION_EXAMPLES.decode("utf8")
HTML = HTML.replace("SECTION_EXAMPLES",SECTION_EXAMPLES)
# build SECTION_CUSTOM
SECTION_CUSTOM = ""
SECTION_CUSTOM = encode("")
cfolder = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Start").GetString("ShowCustomFolder","")
if cfolder:
if not os.path.isdir(cfolder):
cfolder = os.path.dirname(cfolder)
SECTION_CUSTOM = "<h2>"+os.path.basename(os.path.normpath(cfolder))+"</h2>"
SECTION_CUSTOM = encode("<h2>"+os.path.basename(os.path.normpath(cfolder))+"</h2>")
SECTION_CUSTOM += "<ul>"
for basename in os.listdir(cfolder):
filename = os.path.join(cfolder,basename)
SECTION_CUSTOM += buildCard(filename,method="LoadCustom.py?filename=")
SECTION_CUSTOM += encode(buildCard(filename,method="LoadCustom.py?filename="))
SECTION_CUSTOM += "</ul>"
if sys.version_info.major < 3:
SECTION_CUSTOM = SECTION_CUSTOM.decode("utf8")
HTML = HTML.replace("SECTION_CUSTOM",SECTION_CUSTOM)
# build UL_WORKBENCHES
@@ -395,7 +396,7 @@ def handle():
UL_WORKBENCHES += '<a href="https://www.freecadweb.org/wiki/'+wn+'_Workbench">'+wn.replace("ReverseEngineering","ReverseEng")+'</a>'
UL_WORKBENCHES += '</li>'
UL_WORKBENCHES += '</ul>'
HTML = HTML.replace("UL_WORKBENCHES",UL_WORKBENCHES)
HTML = HTML.replace("UL_WORKBENCHES",encode(UL_WORKBENCHES))
# Detect additional addons that are not a workbench
@@ -435,7 +436,7 @@ def handle():
SHADOW = "#888888"
if QtGui.QColor(BASECOLOR).valueF() < 0.5: # dark page - we need to make darker shadows
SHADOW = "#000000"
FONTFAMILY = p.GetString("FontFamily","Arial,Helvetica,sans")
FONTFAMILY = encode(p.GetString("FontFamily","Arial,Helvetica,sans"))
if not FONTFAMILY:
FONTFAMILY = "Arial,Helvetica,sans"
FONTSIZE = p.GetInt("FontSize",13)