Skip to content

Commit

Permalink
Moved try_decode to external module
Browse files Browse the repository at this point in the history
Moved try_decode to external module. Use it across script code.
  • Loading branch information
roee88 committed Jul 28, 2015
1 parent 2de3a8b commit eae500f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 75 deletions.
28 changes: 7 additions & 21 deletions resources/lib/datafunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -137,10 +138,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 @@ -809,10 +807,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 @@ -1107,10 +1102,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 @@ -1119,15 +1112,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
53 changes: 15 additions & 38 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 @@ -472,12 +472,6 @@ 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 _try_decode(self, text, encoding="utf-8"):
try:
return text.decode(encoding)
except:
return text

def _save_shortcuts_function( self ):
# Save shortcuts
Expand All @@ -498,15 +492,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 Down Expand Up @@ -535,11 +527,11 @@ def _save_shortcuts_function( self ):
else:
icon = listitem.getProperty( "icon" )

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

# Action
xmltree.SubElement( shortcut, "action" ).text = self._try_decode(listitem.getProperty( "path" ))
xmltree.SubElement( shortcut, "action" ).text = try_decode( listitem.getProperty( "path" ) )

# Visible
if listitem.getProperty( "visible-condition" ):
Expand All @@ -556,11 +548,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 @@ -597,20 +585,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 @@ -985,7 +964,7 @@ def onClick(self, controlID):
oldlabelID = listitem.getProperty( "labelID" )

# If the item is blank, set the current label to empty
if label.decode('utf-8') == __language__(32013):
if try_decode( label ) == __language__(32013):
label = ""

# Get new label from keyboard dialog
Expand Down Expand Up @@ -1036,15 +1015,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
21 changes: 5 additions & 16 deletions resources/lib/xmlfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import xml.etree.ElementTree as xmltree
from xml.sax.saxutils import escape as escapeXML
from traceback import print_exc
from unicodeutils import try_decode

if sys.version_info < (2, 7):
import simplejson
Expand Down Expand Up @@ -673,10 +674,7 @@ def buildElement( self, item, Tree, groupName, visibilityCondition, profileVisib
# 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 @@ -689,10 +687,7 @@ def buildElement( self, item, Tree, groupName, visibilityCondition, profileVisib
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" ) )
Expand Down Expand Up @@ -726,18 +721,12 @@ def buildElement( self, item, Tree, groupName, visibilityCondition, profileVisib
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 eae500f

Please sign in to comment.