Skip to content

Commit

Permalink
Merge pull request #33 from roee88/master
Browse files Browse the repository at this point in the history
Additional unicode fixes
  • Loading branch information
BobCratchett committed Jul 31, 2015
2 parents 935388a + eae500f commit 1b4ffef
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 82 deletions.
28 changes: 7 additions & 21 deletions resources/lib/datafunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from traceback import print_exc
from htmlentitydefs import name2codepoint
from unidecode import unidecode
from unicodeutils import try_decode

import nodefunctions
NODE = nodefunctions.NodeFunctions()
Expand Down Expand Up @@ -139,10 +140,7 @@ def _get_shortcuts( self, group, defaultGroup = None, isXML = False, profileDir
paths = [userShortcuts, skinShortcuts, defaultShortcuts ]

for path in paths:
try:
path = path.decode( "utf-8" )
except:
pass
path = try_decode( path )

tree = None
altPath = path.replace( ".DATA.xml", ".shortcuts" )
Expand Down Expand Up @@ -795,10 +793,7 @@ def local( self, data ):
if data is None:
return ["","","",""]

try:
data = data.decode( "utf-8" )
except:
pass
data = try_decode( data )

skinid = None
lasttranslation = None
Expand Down Expand Up @@ -1093,10 +1088,8 @@ def upgrade_xmlfile( self, path, mixedVersion = False, saveFile = True ):
actionTree = xmltree.SubElement( root, "shortcut" )

# Action
try:
action = urllib.unquote( shortcut[4] ).decode( "utf-8" )
except:
action = urllib.unquote( shortcut[4] )
action = try_decode( urllib.unquote( shortcut[4] ) )

xmltree.SubElement( actionTree, "action" ).text = action

# Label and label2, defaultID
Expand All @@ -1105,15 +1098,8 @@ def upgrade_xmlfile( self, path, mixedVersion = False, saveFile = True ):
xmltree.SubElement( actionTree, "defaultID" ).text = DataFunctions()._get_labelID( DataFunctions().local( shortcut[0] )[3], action, True )

# Icon and thumbnail
try:
xmltree.SubElement( actionTree, "icon" ).text = shortcut[2].decode( "utf-8" )
except:
xmltree.SubElement( actionTree, "icon" ).text = shortcut[2]

try:
xmltree.SubElement( actionTree, "thumb" ).text = shortcut[3].decode( "utf-8" )
except:
xmltree.SubElement( actionTree, "thumb" ).text = shortcut[3]
xmltree.SubElement( actionTree, "icon" ).text = try_decode( shortcut[2] )
xmltree.SubElement( actionTree, "thumb" ).text = try_decode( shortcut[3] )

# mixedVersion will be True if we're upgrading a skin's defaults
if mixedVersion == True:
Expand Down
60 changes: 20 additions & 40 deletions resources/lib/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from xml.sax.saxutils import escape as escapeXML
import thread
from traceback import print_exc
from unidecode import unidecode
from unicodeutils import try_decode
import random

import datafunctions
Expand Down Expand Up @@ -465,7 +465,7 @@ def _save_shortcuts( self, weEnabledSystemDebug = False, weEnabledScriptDebug =
else:
# Inform user menu couldn't be saved
xbmcgui.Dialog().ok( __addon__.getAddonInfo( "name" ), __language__( 32097 ), __language__( 32094 ) )

def _save_shortcuts_function( self ):
# Save shortcuts
if self.changeMade == True:
Expand All @@ -485,15 +485,13 @@ def _save_shortcuts_function( self ):
for listitem in self.allListItems:

# If the item has a label...
if listitem.getLabel().decode('utf-8') != __language__(32013):
if try_decode( listitem.getLabel() ) != __language__(32013):
# Generate labelID, and mark if it has changed
labelID = listitem.getProperty( "labelID" )
newlabelID = labelID

# defaultID
try:
defaultID = listitem.getProperty( "defaultID" ).decode( "utf-8" )
except:
defaultID = listitem.getProperty( "defaultID" )
defaultID = try_decode( listitem.getProperty( "defaultID" ) )

localizedString = listitem.getProperty( "localizedString" )
if localizedString is None or localizedString == "":
Expand All @@ -515,21 +513,18 @@ def _save_shortcuts_function( self ):

# Icon and thumbnail
if listitem.getProperty( "untranslatedIcon" ):
xmltree.SubElement( shortcut, "icon" ).text = listitem.getProperty( "untranslatedIcon" )
icon = listitem.getProperty( "untranslatedIcon" )
else:
if listitem.getProperty( "original-icon" ):
xmltree.SubElement( shortcut, "icon" ).text = listitem.getProperty( "original-icon" )
icon = listitem.getProperty( "original-icon" )
else:
xmltree.SubElement( shortcut, "icon" ).text = listitem.getProperty( "icon" )

xmltree.SubElement( shortcut, "thumb" ).text = listitem.getProperty( "thumbnail" )
icon = listitem.getProperty( "icon" )

xmltree.SubElement( shortcut, "icon" ).text = try_decode( icon )
xmltree.SubElement( shortcut, "thumb" ).text = try_decode( listitem.getProperty( "thumbnail" ) )

# Action
try:
action = listitem.getProperty( "path" ).decode( "utf-8" )
except:
action = listitem.getProperty( "path" )
xmltree.SubElement( shortcut, "action" ).text = action
xmltree.SubElement( shortcut, "action" ).text = try_decode( listitem.getProperty( "path" ) )

# Visible
if listitem.getProperty( "visible-condition" ):
Expand All @@ -546,11 +541,7 @@ def _save_shortcuts_function( self ):
# Save the shortcuts
DATA.indent( root )
path = os.path.join( __datapath__ , DATA.slugify( self.group ) + ".DATA.xml" )

try:
path = path.decode( "utf-8" )
except:
pass
path = try_decode( path )

tree.write( path.replace( ".shortcuts", ".DATA.xml" ), encoding="UTF-8" )

Expand Down Expand Up @@ -587,20 +578,11 @@ def _save_shortcuts_function( self ):
paths = [[os.path.join( __datapath__, DATA.slugify( labelIDFrom ) + "." + str( i ) + ".DATA,xml" ).encode( "utf-8" ), "Move"], [os.path.join( __skinpath__, DATA.slugify( defaultIDFrom ) + "." + str( i ) + ".DATA.xml" ).encode( "utf-8" ), "Copy"], [os.path.join( __defaultpath__, DATA.slugify( defaultIDFrom ) + "." + str( i ) + ".DATA.xml" ).encode( "utf-8" ), "Copy"]]
target = os.path.join( __datapath__, DATA.slugify( labelIDTo ) + "." + str( i ) + ".DATA.xml" ).encode( "utf-8" )

try:
target = target.decode( "utf-8" )
except:
pass
target = try_decode( target )

for path in paths:
try:
path[ 0 ] = path[ 0 ].decode( "utf-8" )
except:
pass
try:
path[ 1 ] = path[ 1 ].decode( "utf-8" )
except:
pass
path[0] = try_decode( path[0] )
path[1] = try_decode( path[1] )

if path[1] == "New":
tree = xmltree.ElementTree( xmltree.Element( "shortcuts" ) )
Expand Down Expand Up @@ -975,7 +957,7 @@ def onClick(self, controlID):
oldlabelID = listitem.getProperty( "labelID" )

# If the item is blank, set the current label to empty
if label == __language__(32013):
if try_decode( label ) == __language__(32013):
label = ""

# Get new label from keyboard dialog
Expand Down Expand Up @@ -1026,15 +1008,13 @@ def onClick(self, controlID):
keyboard.doModal()

if ( keyboard.isConfirmed() ):
try:
action = keyboard.getText().decode( "utf-8" )
except:
action = keyboard.getText()
action = try_decode( keyboard.getText() )

if action == "":
action = "noop"

# Check that a change was really made
if action == listitem.getProperty( "path" ):
if action == try_decode( listitem.getProperty( "path" ) ):
return
else:
return
Expand Down
10 changes: 10 additions & 0 deletions resources/lib/unicodeutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# coding=utf-8


def try_decode(text, encoding="utf-8"):
if isinstance(text, str):
try:
return text.decode(encoding)
except:
pass
return text
31 changes: 10 additions & 21 deletions resources/lib/xmlfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from xml.sax.saxutils import escape as escapeXML
import copy
from traceback import print_exc
from unicodeutils import try_decode

if sys.version_info < (2, 7):
import simplejson
Expand Down Expand Up @@ -691,10 +692,7 @@ def buildElement( self, item, groupName, visibilityCondition, profileVisibility,
# Group name
group = xmltree.SubElement( newelement, "property" )
group.set( "name", "group" )
try:
group.text = groupName.decode( "utf-8" )
except:
group.text = groupName
group.text = try_decode( groupName )

if groupName == "mainmenu":
self.MAINWIDGET = {}
Expand All @@ -707,17 +705,11 @@ def buildElement( self, item, groupName, visibilityCondition, profileVisibility,
for property in properties:
if property[0] == "node.visible":
visibleProperty = xmltree.SubElement( newelement, "visible" )
try:
visibleProperty.text = property[1].decode( "utf-8" )
except:
visibleProperty.text = property[1]
visibleProperty.text = try_decode( property[1] )
else:
additionalproperty = xmltree.SubElement( newelement, "property" )
additionalproperty.set( "name", property[0].decode( "utf-8" ) )
try:
additionalproperty.text = DATA.local( property[1].decode( "utf-8" ) )[1]
except:
additionalproperty.text = DATA.local( property[1] )[1]
additionalproperty.text = DATA.local( property[1] )[1]

# If this is a widget or background, set a skin setting to say it's enabled
if property[0] == "widget":
Expand All @@ -727,7 +719,10 @@ def buildElement( self, item, groupName, visibilityCondition, profileVisibility,
xbmc.executebuiltin( "Skin.SetString(skinshortcuts-widget-" + str( self.widgetCount ) + "," + property[ 1 ] + ")" )
self.widgetCount += 1
elif property[0] == "background":
xbmc.executebuiltin( "Skin.SetBool(skinshortcuts-background-" + property[1] + ")" )
try:
xbmc.executebuiltin( "Skin.SetBool(skinshortcuts-background-" + property[1] + ")" )
except UnicodeEncodeError:
xbmc.executebuiltin( "Skin.SetBool(skinshortcuts-background-" + property[1].encode('utf-8') + ")" )

# If this is the main menu, and we're cloning widgets or backgrounds...
if groupName == "mainmenu":
Expand All @@ -744,18 +739,12 @@ def buildElement( self, item, groupName, visibilityCondition, profileVisibility,
for key in self.MAINWIDGET:
additionalproperty = xmltree.SubElement( newelement, "property" )
additionalproperty.set( "name", key )
try:
additionalproperty.text = self.MAINWIDGET[ key ].decode( "utf-8" )
except:
additionalproperty.text = self.MAINWIDGET[ key ]
additionalproperty.text = try_decode( self.MAINWIDGET[ key ] )
if "clonebackgrounds" in options and len( self.MAINBACKGROUND ) is not 0:
for key in self.MAINBACKGROUND:
additionalproperty = xmltree.SubElement( newelement, "property" )
additionalproperty.set( "name", key )
try:
additionalproperty.text = DATA.local( self.MAINBACKGROUND[ key ].decode( "utf-8" ) )[1]
except:
additionalproperty.text = DATA.local( self.MAINBACKGROUND[ key ] )[1]
additionalproperty.text = DATA.local( self.MAINBACKGROUND[ key ] )[1]

propertyPatterns = self.getPropertyPatterns(labelID.text, groupName)
if len(propertyPatterns) > 0:
Expand Down

0 comments on commit 1b4ffef

Please sign in to comment.