-
Notifications
You must be signed in to change notification settings - Fork 4
/
catxml-new.py
62 lines (55 loc) · 2.39 KB
/
catxml-new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python
# -*- coding: <utf-8> -*-
import os, re, sys, glob
from optparse import OptionParser
import xml.etree.cElementTree as ET
ADDONSXMLHEADER = '<?xml version="1.0" encoding="UTF-8"?>\n<addons>'
ADDONSXMLFOOTER = '</addons>\n'
DEFAULTDESCNAME = 'addon.xml'
def getDescPaths(path):
"""crawls path for addon.xml files, then returns a list of them"""
descPaths = list()
for item in os.listdir(path):
if os.path.isfile(os.path.join(path, item, DEFAULTDESCNAME)):
descPaths.append(os.path.join(path, item, DEFAULTDESCNAME))
descPaths.sort(key=lambda p: os.stat(p).st_mtime, reverse=True)
return descPaths
def get_xml_from_file(path):
elem = ET.parse(path).getroot()
for subelement in elem:
if subelement.tag == "extension" and (subelement.attrib["point"] == "xbmc.addon.metadata" or subelement.attrib["point"] == "kodi.addon.metadata"):
if not os.path.exists(os.path.join(os.path.dirname(path),'icon.png')):
noicon = ET.SubElement(subelement,'noicon')
noicon.text = "true"
if not os.path.exists(os.path.join(os.path.dirname(path),'fanart.jpg')):
nofanart = ET.SubElement(subelement,'nofanart')
nofanart.text = "true"
if not glob.glob(os.path.join(os.path.dirname(path),'changelog*.txt')):
nochangelog = ET.SubElement(subelement,'nochangelog')
nochangelog.text = "true"
return ET.tostring(elem,encoding='utf-8')
def remove_xml_header(xml):
return re.sub('.*<\?xml.+?\>', '', xml, 1)
def write_xml(xml_string, xml_dest):
if (xml_dest != ""):
try:
f = open(xml_dest, 'w')
f.write(xml_string)
f.close()
except:
print "Unexpected error:", sys.exc_info()[0]
raise
else:
sys.stdout.write(xml_string)
if __name__ == '__main__':
addonsxml = str()
parser = OptionParser()
parser.add_option('-p', '--addons-path', dest='addons_path', default='.')
parser.add_option('-d', '--dest', dest='dest', default='')
(options, args) = parser.parse_args()
for addon_path in getDescPaths(options.addons_path):
addon = get_xml_from_file(addon_path)
addon = remove_xml_header(addon)
addonsxml = addonsxml + addon
addonsxml = ADDONSXMLHEADER + addonsxml + ADDONSXMLFOOTER
write_xml(addonsxml, options.dest)