
de python 2 a python 3 ayuda!!
Publicado por Pedro (4 intervenciones) el 04/09/2021 16:39:16
hola amigos, yo no se casi nada de código por no decir nada y resulta que ya no se que puedo hacer para que un pequeño código que funciona en kodi 18 (python 2) hacerlo funcionar en kodi 19 ( python 3), he utlizado la herramienta 2to3 pero nada no lo hago funcionar en kodi 19, por favor podrían ayudarme con el código y hacerlo funcionar en kodi 19 ?? me resulta muy útil este script. El código que ven es recién sacado de la herramienta 2to3.
# -*- coding: utf-8 -*-
import xbmc,xbmcaddon,xbmcgui,xbmcplugin,urllib.request,urllib.parse,urllib.error,os,re,sys
def list_addons():
#info directory
addDir('[COLOR lime]%s[/COLOR]' % (translate(30001)),'None',None,os.path.join(xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('path')).decode('utf-8'),'icon.png'))
#get the path of addons
pathofaddons = xbmc.translatePath('special://home/addons').decode('utf-8')
#list with addons
listofaddons = os.listdir(pathofaddons)
for individual_addon in listofaddons:
#path to individual addon, cycle for all the addons
path_to_addon = os.path.join(pathofaddons, individual_addon)
#define addon.xml path
addon_xml_path=os.path.join(path_to_addon,'addon.xml')
#check the existence of addon.xml, if true, we continue
if os.path.exists(addon_xml_path):
#get addon.xml content
xml_content=openfile(addon_xml_path)
if re.search('point="xbmc.service"',xml_content):
#addon with service on
addDir('%s (on)' % (individual_addon),path_to_addon,1,os.path.join(path_to_addon,'icon.png'))
elif re.search('point="xbmc.pass"',xml_content):
#addon with service off
addDir('[COLOR lime]%s (off)[/COLOR]' % (individual_addon),path_to_addon,1,os.path.join(path_to_addon,'icon.png'))
else:
#addon with no service
pass
def change_state(name,path):
#define addon.xml path to change
addon_xml_path=os.path.join(path,'addon.xml')
#get addon.xml content
content=openfile(addon_xml_path)
if re.search('COLOR lime',name):
#service off to on, so we change from fake variable to service variable
content=content.replace('point="xbmc.pass"','point="xbmc.service"')
else:
#service on to off, so we change from service variable to fake variable
content=content.replace('point="xbmc.service"','point="xbmc.pass"')
#change state on addon.xml
savefile(addon_xml_path,content)
#refresh the list
xbmc.executebuiltin("Container.Refresh")
def openfile(path_to_the_file):
try:
fh = open(path_to_the_file, 'rb')
contents=fh.read()
fh.close()
return contents
except:
print("Wont open: %s" % filename)
return None
def savefile(path_to_the_file,content):
try:
fh = open(path_to_the_file, 'wb')
fh.write(content)
fh.close()
except: print("Wont save: %s" % filename)
def addDir(name,path,mode,iconimage):
return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url="%s?path=%s&mode=%s&name=%s" % (sys.argv[0],urllib.parse.quote_plus(path),mode,urllib.parse.quote_plus(name)),listitem=xbmcgui.ListItem(name,iconImage="DefaultFolder.png", thumbnailImage=iconimage),isFolder=False)
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def translate(text):
return xbmcaddon.Addon().getLocalizedString(text).encode('utf-8')
params=get_params()
path=None
name=None
mode=None
try: path=urllib.parse.unquote_plus(params["path"])
except: pass
try: name=urllib.parse.unquote_plus(params["name"])
except: pass
try: mode=int(params["mode"])
except: pass
if mode==None: list_addons()
elif mode==1: change_state(name,path)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
# -*- coding: utf-8 -*-
import xbmc,xbmcaddon,xbmcgui,xbmcplugin,urllib.request,urllib.parse,urllib.error,os,re,sys
def list_addons():
#info directory
addDir('[COLOR lime]%s[/COLOR]' % (translate(30001)),'None',None,os.path.join(xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('path')).decode('utf-8'),'icon.png'))
#get the path of addons
pathofaddons = xbmc.translatePath('special://home/addons').decode('utf-8')
#list with addons
listofaddons = os.listdir(pathofaddons)
for individual_addon in listofaddons:
#path to individual addon, cycle for all the addons
path_to_addon = os.path.join(pathofaddons, individual_addon)
#define addon.xml path
addon_xml_path=os.path.join(path_to_addon,'addon.xml')
#check the existence of addon.xml, if true, we continue
if os.path.exists(addon_xml_path):
#get addon.xml content
xml_content=openfile(addon_xml_path)
if re.search('point="xbmc.service"',xml_content):
#addon with service on
addDir('%s (on)' % (individual_addon),path_to_addon,1,os.path.join(path_to_addon,'icon.png'))
elif re.search('point="xbmc.pass"',xml_content):
#addon with service off
addDir('[COLOR lime]%s (off)[/COLOR]' % (individual_addon),path_to_addon,1,os.path.join(path_to_addon,'icon.png'))
else:
#addon with no service
pass
def change_state(name,path):
#define addon.xml path to change
addon_xml_path=os.path.join(path,'addon.xml')
#get addon.xml content
content=openfile(addon_xml_path)
if re.search('COLOR lime',name):
#service off to on, so we change from fake variable to service variable
content=content.replace('point="xbmc.pass"','point="xbmc.service"')
else:
#service on to off, so we change from service variable to fake variable
content=content.replace('point="xbmc.service"','point="xbmc.pass"')
#change state on addon.xml
savefile(addon_xml_path,content)
#refresh the list
xbmc.executebuiltin("Container.Refresh")
def openfile(path_to_the_file):
try:
fh = open(path_to_the_file, 'rb')
contents=fh.read()
fh.close()
return contents
except:
print("Wont open: %s" % filename)
return None
def savefile(path_to_the_file,content):
try:
fh = open(path_to_the_file, 'wb')
fh.write(content)
fh.close()
except: print("Wont save: %s" % filename)
def addDir(name,path,mode,iconimage):
return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url="%s?path=%s&mode=%s&name=%s" % (sys.argv[0],urllib.parse.quote_plus(path),mode,urllib.parse.quote_plus(name)),listitem=xbmcgui.ListItem(name,iconImage="DefaultFolder.png", thumbnailImage=iconimage),isFolder=False)
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def translate(text):
return xbmcaddon.Addon().getLocalizedString(text).encode('utf-8')
params=get_params()
path=None
name=None
mode=None
try: path=urllib.parse.unquote_plus(params["path"])
except: pass
try: name=urllib.parse.unquote_plus(params["name"])
except: pass
try: mode=int(params["mode"])
except: pass
if mode==None: list_addons()
elif mode==1: change_state(name,path)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
Valora esta pregunta


0