PushService: Initial release
[enigma2-plugins.git] / pushservice / src / PushServiceConfigFile.py
1 import os
2
3 # XML
4 from xml.etree.cElementTree import ElementTree, tostring, parse
5
6 # Plugin internal
7 from . import _
8 from PluginModules import PluginModules
9
10
11 def indent(elem, level=0):
12         i = "\n" + level*"  "
13         if len(elem):
14                 if not elem.text or not elem.text.strip():
15                         elem.text = i + "  "
16                 if not elem.tail or not elem.tail.strip():
17                         elem.tail = i
18                 for elem in elem:
19                         indent(elem, level+1)
20                 if not elem.tail or not elem.tail.strip():
21                         elem.tail = i
22         else:
23                 if level and (not elem.tail or not elem.tail.strip()):
24                         elem.tail = i
25
26
27 class PushServiceConfigFile(object):
28
29         def __init__(self):
30                 self.path = ""
31                 self.mtime = -1
32                 self.cache = ""
33
34         def __setPath(self, path):
35                 if self.path != path:
36                         self.path = path
37                         self.mtime = -1
38                         self.cache = ""
39
40         def readXML(self, path):
41                 self.__setPath(path)
42                 
43                 # Abort if no config found
44                 if not os.path.exists(path):
45                         print _("[PushService] No configuration file present")
46                         return None
47                 
48                 # Parse if mtime differs from whats saved
49                 mtime = os.path.getmtime(path)
50                 if mtime == self.mtime:
51                         # No changes in configuration, won't read again
52                         return self.cache
53                 
54                 # Parse XML
55                 try:
56                         etree = parse(path).getroot()
57                 except Exception, e:
58                         print _("[PushService] Exception in readXML: ") + str(e)
59                         etree = None
60                         mtime = -1
61                 
62                 # Save time and cache file content
63                 self.mtime = mtime
64                 self.cache = etree
65                 return self.cache
66
67         def writeXML(self, etree, path):
68                 self.__setPath(path)
69                 
70                 indent(etree)
71                 data = tostring(etree, 'utf-8')
72                 
73                 f = None
74                 try:
75                         f = open(path, 'w')
76                         if data:
77                                 f.writelines(data)
78                 except Exception, e:
79                         print _("[PushService] Exception in writeXML: ") + str(e)
80                 finally:
81                         if f is not None:
82                                 f.close()
83                 
84                 # Save time and cache file content
85                 self.mtime = os.path.getmtime( path )
86                 self.cache = etree
87