Correcting sources

With elementetree instead
This commit is contained in:
angelblue05 2015-11-03 07:22:03 -06:00
parent c10cee3cb3
commit 5a39208aa3

View file

@ -129,47 +129,68 @@ def stopProfiling(pr, profileName):
f.write(str(ncalls) + "\t" + "{0}".format(total_time) + "\t" + "{0}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n") f.write(str(ncalls) + "\t" + "{0}".format(total_time) + "\t" + "{0}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n")
f.close() f.close()
def indent(elem, level=0):
# Prettify xml trees
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def createSources(): def createSources():
# To make Master lock compatible # To make Master lock compatible
path = xbmc.translatePath("special://profile/").decode("utf-8") path = xbmc.translatePath("special://profile/").decode("utf-8")
xmlpath = "%ssources.xml" % path xmlpath = "%ssources.xml" % path
if xbmcvfs.exists(xmlpath): if xbmcvfs.exists(xmlpath):
# add some way to writing dummy path to existing sources.xml # Modify the existing file
pass try:
else: xmlparse = etree.parse(xmlpath)
sources = open(xmlpath, 'w') except:
sources.write( root = etree.Element('sources')
else:
root = xmlparse.getroot()
'<sources>\n\t' video = root.find('video')
'<programs>\n\t\t' if video is None:
'<default pathversion="1"></default>\n\t' video = etree.SubElement(root, 'video')
'</programs>\n\t' else:
'<video>\n\t\t' # We need to create the file
'<default pathversion="1"></default>\n\t\t' root = etree.Element('sources')
'<source>\n\t\t\t' video = etree.SubElement(root, 'video')
'<name>Emby</name>\n\t\t\t'
'<path pathversion="1">smb://embydummy/dummypath1/</path>\n\t\t\t'
'<allowsharing>true</allowsharing>\n\t\t' # Add elements
'</source>\n\t\t' etree.SubElement(video, 'default', attrib={'pathversion': "1"})
'<source>\n\t\t\t'
'<name>Emby</name>\n\t\t\t' # First dummy source
'<path pathversion="1">smb://embydummy/dummypath2/</path>\n\t\t\t' source_one = etree.SubElement(video, 'source')
'<allowsharing>true</allowsharing>\n\t\t' etree.SubElement(source_one, 'name').text = "Emby"
'</source>\n\t' etree.SubElement(source_one, 'path', attrib={'pathversion': "1"}).text = (
'</video>\n\t'
'<music>\n\t\t' "smb://embydummy/dummypath1/"
'<default pathversion="1"></default>\n\t'
'</music>\n\t'
'<pictures>\n\t\t'
'<default pathversion="1"></default>\n\t'
'</pictures>\n\t'
'<files>\n\t\t'
'<default pathversion="1"></default>\n\t'
'</files>\n'
'</sources>'
) )
sources.close() etree.SubElement(source_one, 'allowsharing').text = "true"
# Second dummy source
source_two = etree.SubElement(video, 'source')
etree.SubElement(source_two, 'name').text = "Emby"
etree.SubElement(source_two, 'path', attrib={'pathversion': "1"}).text = (
"smb://embydummy/dummypath2/"
)
etree.SubElement(source_two, 'allowsharing').text = "true"
indent(root)
etree.ElementTree(root).write(xmlpath, method="html")
def pathsubstitution(add=True): def pathsubstitution(add=True):