"""
Common functions
"""
import os,sys,re
import xbmc
from urllib import unquote_plus, urlopen
from xml.sax.saxutils import unescape
__plugin__ = sys.modules["__main__"].__plugin__
__date__ = '15-10-2009'
#HOME_DIR = os.getcwd()
HOME_DIR = os.path.dirname(os.path.dirname(__file__)) # until XBMC getwd() bug fixed (affects linux)
#################################################################################################################
def log(msg):
xbmc.log("[%s]: %s" % (__plugin__, msg), xbmc.LOGDEBUG)
log("Module: %s Dated: %s loaded!" % (__name__, __date__))
log("HOME_DIR lib=" + HOME_DIR)
#################################################################################################################
def logError(msg=""):
log("ERROR: %s (%d) - %s" % (sys.exc_info()[ 2 ].tb_frame.f_code.co_name, sys.exc_info()[ 2 ].tb_lineno, sys.exc_info()[ 1 ], ) )
if msg:
log(msg)
#################################################################################################################
def handleException(msg=""):
import traceback
import xbmcgui
traceback.print_exc()
xbmcgui.Dialog().ok(__plugin__ + " ERROR!", msg, str(sys.exc_info()[ 1 ]))
#################################################################################################################
class Info:
def __init__( self, *args, **kwargs ):
self.__dict__.update( kwargs )
log( "Info() dict=%s" % self.__dict__ )
def has_key(self, key):
return self.__dict__.has_key(key)
#################################################################################################################
def loadFileObj( filename ):
log( "loadFileObj() " + filename)
try:
file_handle = open( filename, "r" )
loadObj = eval( file_handle.read() )
file_handle.close()
except Exception, e:
log( "loadFileObj() " + str(e) )
loadObj = None
return loadObj
#################################################################################################################
def saveFileObj( filename, saveObj ):
log( "saveFileObj() " + filename)
try:
file_handle = open( filename, "w" )
file_handle.write( repr( saveObj ) )
file_handle.close()
return True
except Exception, e:
log( "save_file_obj() " + str(e) )
return False
#################################################################################################################
def readURL( url ):
log("readURL() url=" + url)
if not url:
return ""
try:
sock = urlopen( url )
doc = sock.read()
sock.close()
if ( "404 Not Found" in doc ):
log("readURL() 404, Not found")
doc = ""
return doc
except:
log("readURL() %s" % sys.exc_info()[ 1 ])
return None
#################################################################################################################
def readFile(filename):
try:
f = xbmc.translatePath(filename)
log("readFile() " + f)
return file(f).read()
except:
log("readFile() not found ")
return ""
#################################################################################################################
def deleteFile( fn ):
try:
os.remove( fn )
log("deleteFile() deleted: " + fn)
except: pass
#####################################################################################################
def get_repo_info( repo ):
# path to info file
# repopath = os.path.join( os.getcwd(), "resources", "repositories", repo, "repo.xml" )
repopath = os.path.join( HOME_DIR, "resources", "repositories", repo, "repo.xml" )
try:
info = open( repopath, "r" ).read()
# repo's base url
REPO_URL = re.findall( '([^<]+)', info )[ 0 ]
# root of repository
REPO_ROOT = re.findall( '([^<]*)', info )[ 0 ]
# structure of repo
REPO_STRUCTURES = re.findall( '(.*?)" % tag))
else:
return None
#################################################################################################################
def parseDescriptionXML(doc):
""" Parse Description.xml to get info """
if not doc:
return None
info = {}
# singles lines
tags = ("guid","type","title","version","summary","description","minrevision","license")
for tag in tags:
info[tag] = parseXMLTag(doc, tag)
# convert type number to a name
typeNames=("visualization","skin","pvrdll","script","scraper","screensaver","plugin-pvr",
"plugin-video","plugin-music","plugin-program","plugin-pictures","plugin-weather")
try:
i = int(info["type"])-1
info["typename"] = typeNames[i]
except:
info["typename"] = ""
# add XBMC_Revision from minversion
try:
info["XBMC_Revision"] = int(info["minrevision"])
except:
info["XBMC_Revision"] = 0
# multiple lines, saved as list
tags = ("tags","platforms")
for tag in tags:
try:
# may be multiple section cos of an example in comments. Use last section found
section = findAllRegEx(doc, "<%s>(.*?)%s>" % (tag, tag))[-1]
if section:
info[tag] = findAllRegEx(section, "<%s>(.*?)" % tag[:-1])
except: pass
# unique regex reqd.
tag = "authors"
info[tag] = findAllRegEx(doc, 'name="(.*?)" email="(.*?)"')
log("description.xml info=%s" % info)
return info