summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/gen_lib_xml.py
blob: 1cc4516486f324a7923f8b04a4816f64b781bda6 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /usr/bin/python2
#Helper program that generates library.xml thats custom to linhes.
#Contents are read from /etc/gen_lib_xml.d
#This script should be run everytime an entry is added or removed


import os, sys
import glob

class Gen_lib_xml:
    def __init__(self,snippit_dir,orig_theme_file):
        self.snippit_dir = snippit_dir
        self.orig_theme_file =  orig_theme_file
        self.xml_snippets=[]
        self.orig_theme_xml=[]
        self.new_xml=[]

    def get_new_xml(self):
        return self.new_xml
    def get_orig_xml(self):
        return self.orig_theme_xml
    def get_snippits(self):
        return self.xml_snippets

    def read_snippets(self):
        xml_snippets=""
        lines=[]
        try:
            os.chdir(self.snippit_dir)
        except:
            print "    gen_lib_xml: Couldn't change dir to %s" %self.snippit_dir
            print "    Exiting"
            sys.exit(0)
        file_list=glob.glob("*.conf")
        for conf_file in file_list:
            try:
                print "    gen_lib_xml: reading in %s" %conf_file
                f=open(conf_file,'r')
                line=f.readlines()
                f.close()
            except:
                print "    gen_lib_xml: Couldn't open %s for reading" %conf_file
                print "    Exiting"
                sys.exit(0)
            lines.extend(line)

        if len(file_list) == 0:
            print "    gen_lib_xml:  no conf files found"
            lines = []
        self.xml_snippets = lines

    def read_orig_xml(self):
        try:
            print "    gen_lib_xml:  reading in %s" %self.orig_theme_file
            f=open(self.orig_theme_file,'r')
            lines=f.readlines()
            f.close()

        except:
            print "    gen_lib_xml: Couldn't open %s for reading" %self.orig_theme_file
            print "    Exiting"
            sys.exit(2)
        #print lines
        for i in lines:
            if i.strip() == "</mythmenu>":
                lines.remove(i)
                print "    gen_lib_xml:  Removing /mythmenu tag "
                break
        self.orig_theme_xml=lines

    def make_new_xml(self):
        self.new_xml = self.orig_theme_xml + self.xml_snippets
        self.new_xml.append("</mythmenu>\n")
        pass








def write_xml(xml,filename):
    try:
        f=open(filename, 'w')
    except:
        print "    gen_lib_xml: Couldn't open %s" %(filename)
        print "    Exiting"
        sys.exit(2)
    print "    gen_lib_xml:  Writing %s" %(filename)
    for i in xml:
        f.write(i)
    f.close()

def main():
    sys.path.append('/usr/MythVantage/bin/')
    config_file = "mv_config"
    data_config = __import__(config_file,  globals(),  locals(),  [])

    filename="%s/.mythtv/library.xml" %data_config.MYTHHOME
    #filename="/tmp/library.xml"
    orig_theme_file="/usr/share/mythtv/themes/defaultmenu/library.xml"
    lib_xml_dir="/etc/gen_lib_xml.d/"

    lib_xml = Gen_lib_xml(lib_xml_dir,orig_theme_file)
    lib_xml.read_snippets()
    lib_xml.read_orig_xml()
    lib_xml.make_new_xml()
    lib_xml.make_new_xml()
    new_xml = lib_xml.get_new_xml()

    #a = lib_xml.get_new_xml()
    #b = lib_xml.get_orig_xml()
    #c = lib_xml.get_snippits()

    write_xml(new_xml,filename)

if __name__ == "__main__":
    main()