From 42e295743852291c7788d89a2a71f596d68b72e4 Mon Sep 17 00:00:00 2001 From: Moritz Venn Date: Mon, 7 Jul 2008 09:45:35 +0000 Subject: [PATCH] Configuration parser factored out to AutoTimerConfiguration, adjust to previous commit... --- autotimer/src/AutoTimer.py | 555 ++-------------------------- autotimer/src/AutoTimerComponent.py | 7 + autotimer/src/AutoTimerOverview.py | 4 +- 3 files changed, 38 insertions(+), 528 deletions(-) diff --git a/autotimer/src/AutoTimer.py b/autotimer/src/AutoTimer.py index 3f83cc2b..f9fbb728 100644 --- a/autotimer/src/AutoTimer.py +++ b/autotimer/src/AutoTimer.py @@ -1,14 +1,14 @@ # Plugins Config from xml.dom.minidom import parse as minidom_parse -from Tools.XMLTools import stringToXML from os import path as os_path +from AutoTimerConfiguration import parseConfig, writeConfig # Navigation (RecordTimer) import NavigationInstance # Timer from ServiceReference import ServiceReference -from RecordTimer import RecordTimerEntry, AFTEREVENT +from RecordTimer import RecordTimerEntry from Components.TimerSanityCheck import TimerSanityCheck # Timespan @@ -24,30 +24,6 @@ from Components.config import config from AutoTimerComponent import AutoTimerComponent XML_CONFIG = "/etc/enigma2/autotimer.xml" -CURRENT_CONFIG_VERSION = "5" - -def getValue(definitions, default): - # Initialize Output - ret = "" - - # How many definitions are present - try: - childNodes = definitions.childNodes - except: - Len = len(definitions) - if Len > 0: - childNodes = definitions[Len-1].childNodes - else: - childNodes = [] - - # Iterate through nodes of last one - for node in childNodes: - # Append text if we have a text node - if node.nodeType == node.TEXT_NODE: - ret = ret + node.data - - # Return stripped output or (if empty) default - return ret.strip() or default def getTimeDiff(timer, begin, end): if begin <= timer.begin <= end: @@ -84,9 +60,12 @@ class AutoTimer: True # Enabled ) +# Configuration + def readXml(self): # Abort if no config found if not os_path.exists(XML_CONFIG): + print "[AutoTimer] No configuration file present" return # Parse if mtime differs from whats saved @@ -104,316 +83,33 @@ class AutoTimer: # Empty out timers and reset Ids del self.timers[:] self.uniqueTimerId = 0 - self.defaultTimer = AutoTimerComponent( - 0, # Id - "", # Name - "", # Match - True # Enabled - ) + self.defaultTimer.clear(-1, True) # Get Config Element for configuration in dom.getElementsByTagName("autotimer"): - # Parse old configuration files - if configuration.getAttribute("version") != CURRENT_CONFIG_VERSION: - from OldConfigurationParser import parseConfig - parseConfig(configuration, self.timers, configuration.getAttribute("version"), self.uniqueTimerId) - if not self.uniqueTimerId: - self.uniqueTimerId = len(self.timers) - continue - # Read in defaults for a new timer - for defaults in configuration.getElementsByTagName("defaults"): - # Read out timespan - start = defaults.getAttribute("from") - end = defaults.getAttribute("to") - if start and end: - start = [int(x) for x in start.split(':')] - end = [int(x) for x in end.split(':')] - self.defaultTimer.timespan = (start, end) - - # Read out max length - maxduration = defaults.getAttribute("maxduration") or None - if maxduration: - self.defaultTimer.maxduration = int(maxlen)*60 - - # Read out recording path - self.defaultTimer.destination = defaults.getAttribute("location").encode("UTF-8") or None - - # Read out offset - offset = defaults.getAttribute("offset") or None - if offset: - offset = offset.split(",") - if len(offset) == 1: - before = after = int(offset[0] or 0) * 60 - else: - before = int(offset[0] or 0) * 60 - after = int(offset[1] or 0) * 60 - self.defaultTimer.offset = (before, after) - - # Read out counter - self.defaultTimer.matchCount = int(defaults.getAttribute("counter") or '0') - self.defaultTimer.matchFormatString = defaults.getAttribute("counterFormat") - - # Read out justplay - justplay = int(defaults.getAttribute("justplay") or '0') - - # Read out avoidDuplicateDescription - self.defaultTimer.avoidDuplicateDescription = bool(defaults.getAttribute("avoidDuplicateDescription") or False) - - # Read out allowed services - servicelist = self.defaultTimer.services - for service in defaults.getElementsByTagName("serviceref"): - value = getValue(service, None) - if value: - # strip all after last : - pos = value.rfind(':') - if pos != -1: - value = value[:pos+1] - - servicelist.append(value) - self.defaultTimer.services = servicelist # We might have got a dummy list above - - # Read out allowed bouquets - bouquets = self.defaultTimer.bouquets - for bouquet in defaults.getElementsByTagName("bouquet"): - value = getValue(bouquet, None) - if value: - bouquets.append(value) - self.defaultTimer.bouquets = bouquets - - # Read out afterevent - idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY} - afterevent = self.defaultTimer.afterevent - for element in defaults.getElementsByTagName("afterevent"): - value = getValue(element, None) - - try: - value = idx[value] - start = element.getAttribute("from") - end = element.getAttribute("to") - if start and end: - start = [int(x) for x in start.split(':')] - end = [int(x) for x in end.split(':')] - afterevent.append((value, (start, end))) - else: - afterevent.append((value, None)) - except KeyError, ke: - print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition' - continue - self.defaultTimer.afterevent = afterevent - - # Read out exclude - idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3} - excludes = (self.defaultTimer.getExcludedTitle(), self.defaultTimer.getExcludedShort(), self.defaultTimer.getExcludedDescription(), self.defaultTimer.getExcludedDays()) - for exclude in defaults.getElementsByTagName("exclude"): - where = exclude.getAttribute("where") - value = getValue(exclude, None) - if not (value and where): - continue - - try: - excludes[idx[where]].append(value.encode("UTF-8")) - except KeyError, ke: - pass - self.defaultTimer.excludes = excludes - - # Read out includes (use same idx) - includes = (self.defaultTimer.getIncludedTitle(), self.defaultTimer.getIncludedShort(), self.defaultTimer.getIncludedDescription(), self.defaultTimer.getIncludedDays()) - for include in defaults.getElementsByTagName("include"): - where = include.getAttribute("where") - value = getValue(include, None) - if not (value and where): - continue - - try: - includes[idx[where]].append(value.encode("UTF-8")) - except KeyError, ke: - pass - self.defaultTimer.includes = includes - - # Read out recording tags (needs my enhanced tag support patch) - tags = self.defaultTimer.tags - for tag in defaults.getElementsByTagName("tag"): - value = getValue(tag, None) - if not value: - continue - - tags.append(value.encode("UTF-8")) + parseConfig( + configuration, + self.timers, + configuration.getAttribute("version"), + self.uniqueTimerId, + self.defaultTimer + ) + self.uniqueTimerId = len(self.timers) - # Iterate Timers - for timer in configuration.getElementsByTagName("timer"): - # Increment uniqueTimerId - self.uniqueTimerId += 1 - - # Read out match - match = timer.getAttribute("match").encode("UTF-8") - if not match: - print '[AutoTimer] Erroneous config is missing attribute "match", skipping entry' - continue - - # Read out name - name = timer.getAttribute("name").encode("UTF-8") - if not name: - print '[AutoTimer] Timer is missing attribute "name", defaulting to match' - name = match - - # Read out enabled - enabled = timer.getAttribute("enabled") or "yes" - if enabled == "no": - enabled = False - elif enabled == "yes": - enabled = True - else: - print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', disabling' - enabled = False - - # Read out timespan - start = timer.getAttribute("from") - end = timer.getAttribute("to") - if start and end: - start = [int(x) for x in start.split(':')] - end = [int(x) for x in end.split(':')] - timetuple = (start, end) - else: - timetuple = None - - # Read out max length - maxlen = timer.getAttribute("maxduration") or None - if maxlen: - maxlen = int(maxlen)*60 - - # Read out recording path - destination = timer.getAttribute("location").encode("UTF-8") or None - - # Read out offset - offset = timer.getAttribute("offset") or None - if offset: - offset = offset.split(",") - if len(offset) == 1: - before = after = int(offset[0] or 0) * 60 - else: - before = int(offset[0] or 0) * 60 - after = int(offset[1] or 0) * 60 - offset = (before, after) - - # Read out counter - counter = int(timer.getAttribute("counter") or '0') - counterLeft = int(timer.getAttribute("left") or counter) - counterLimit = timer.getAttribute("lastActivation") - counterFormat = timer.getAttribute("counterFormat") - lastBegin = int(timer.getAttribute("lastBegin") or 0) - - # Read out justplay - justplay = int(timer.getAttribute("justplay") or '0') - - # Read out avoidDuplicateDescription - avoidDuplicateDescription = bool(timer.getAttribute("avoidDuplicateDescription") or False) - - # Read out allowed services - servicelist = [] - for service in timer.getElementsByTagName("serviceref"): - value = getValue(service, None) - if value: - # strip all after last : - pos = value.rfind(':') - if pos != -1: - value = value[:pos+1] - - servicelist.append(value) - - # Read out allowed bouquets - bouquets = [] - for bouquet in timer.getElementsByTagName("bouquet"): - value = getValue(bouquet, None) - if value: - bouquets.append(value) - - # Read out afterevent - idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY} - afterevent = [] - for element in timer.getElementsByTagName("afterevent"): - value = getValue(element, None) - - try: - value = idx[value] - start = element.getAttribute("from") - end = element.getAttribute("to") - if start and end: - start = [int(x) for x in start.split(':')] - end = [int(x) for x in end.split(':')] - afterevent.append((value, (start, end))) - else: - afterevent.append((value, None)) - except KeyError, ke: - print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition' - continue - - # Read out exclude - idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3} - excludes = ([], [], [], []) - for exclude in timer.getElementsByTagName("exclude"): - where = exclude.getAttribute("where") - value = getValue(exclude, None) - if not (value and where): - continue - - try: - excludes[idx[where]].append(value.encode("UTF-8")) - except KeyError, ke: - pass - - # Read out includes (use same idx) - includes = ([], [], [], []) - for include in timer.getElementsByTagName("include"): - where = include.getAttribute("where") - value = getValue(include, None) - if not (value and where): - continue + def writeXml(self): + writeConfig(XML_CONFIG, self.defaultTimer, self.timers) - try: - includes[idx[where]].append(value.encode("UTF-8")) - except KeyError, ke: - pass +# Manage List - # Read out recording tags (needs my enhanced tag support patch) - tags = [] - for tag in timer.getElementsByTagName("tag"): - value = getValue(tag, None) - if not value: - continue + def add(self, timer): + self.timers.append(timer) - tags.append(value.encode("UTF-8")) - - # Finally append tuple - self.timers.append(AutoTimerComponent( - self.uniqueTimerId, - name, - match, - enabled, - timespan = timetuple, - services = servicelist, - offset = offset, - afterevent = afterevent, - exclude = excludes, - include = includes, - maxduration = maxlen, - destination = destination, - matchCount = counter, - matchLeft = counterLeft, - matchLimit = counterLimit, - matchFormatString = counterFormat, - lastBegin = lastBegin, - justplay = justplay, - avoidDuplicateDescription = avoidDuplicateDescription, - bouquets = bouquets, - tags = tags - )) + def getEnabledTimerList(self): + return [x for x in self.timers if x.enabled] def getTimerList(self): return self.timers - def getEnabledTimerList(self): - return [x for x in self.timers if x.enabled] - def getTupleTimerList(self): return [(x,) for x in self.timers] @@ -421,8 +117,13 @@ class AutoTimer: self.uniqueTimerId += 1 return self.uniqueTimerId - def add(self, timer): - self.timers.append(timer) + def remove(self, uniqueId): + idx = 0 + for timer in self.timers: + if timer.id == uniqueId: + self.timers.pop(idx) + return + idx += 1 def set(self, timer): idx = 0 @@ -433,205 +134,7 @@ class AutoTimer: idx += 1 self.timers.append(timer) - def remove(self, uniqueId): - idx = 0 - for timer in self.timers: - if timer.id == uniqueId: - self.timers.pop(idx) - return - idx += 1 - - def writeXml(self): - # Generate List in RAM - list = ['\n\n\n'] - - # XXX: we might want to make sure that we don't save empty default here - list.extend([' \n') - - # Services - for serviceref in self.defaultTimer.getServices(): - list.extend([' ', serviceref, '']) - ref = ServiceReference(str(serviceref)) - list.extend([' \n']) - - # Bouquets - for bouquet in self.defaultTimer.getBouquets(): - list.extend([' ', str(bouquet), '']) - ref = ServiceReference(str(bouquet)) - list.extend([' \n']) - - # AfterEvent - if self.defaultTimer.hasAfterEvent(): - idx = {AFTEREVENT.NONE: "none", AFTEREVENT.STANDBY: "standby", AFTEREVENT.DEEPSTANDBY: "shutdown"} - for afterevent in self.defaultTimer.getCompleteAfterEvent(): - action, timespan = afterevent - list.append(' ', idx[action], '\n']) - - # Excludes - for title in self.defaultTimer.getExcludedTitle(): - list.extend([' ', stringToXML(title), '\n']) - for short in self.defaultTimer.getExcludedShort(): - list.extend([' ', stringToXML(short), '\n']) - for desc in self.defaultTimer.getExcludedDescription(): - list.extend([' ', stringToXML(desc), '\n']) - for day in self.defaultTimer.getExcludedDays(): - list.extend([' ', stringToXML(day), '\n']) - - # Includes - for title in self.defaultTimer.getIncludedTitle(): - list.extend([' ', stringToXML(title), '\n']) - for short in self.defaultTimer.getIncludedShort(): - list.extend([' ', stringToXML(short), '\n']) - for desc in self.defaultTimer.getIncludedDescription(): - list.extend([' ', stringToXML(desc), '\n']) - for day in self.defaultTimer.getIncludedDays(): - list.extend([' ', stringToXML(day), '\n']) - - # Tags - for tag in self.defaultTimer.tags: - list.extend([' ', stringToXML(tag), '\n']) - - # End of Timer - list.append(' \n\n') - - # Iterate timers - for timer in self.timers: - # Common attributes (match, enabled) - list.extend([' \n') - - # Services - for serviceref in timer.getServices(): - list.extend([' ', serviceref, '']) - ref = ServiceReference(str(serviceref)) - list.extend([' \n']) - - # Bouquets - for bouquet in timer.getBouquets(): - list.extend([' ', str(bouquet), '']) - ref = ServiceReference(str(bouquet)) - list.extend([' \n']) - - # AfterEvent - if timer.hasAfterEvent(): - idx = {AFTEREVENT.NONE: "none", AFTEREVENT.STANDBY: "standby", AFTEREVENT.DEEPSTANDBY: "shutdown"} - for afterevent in timer.getCompleteAfterEvent(): - action, timespan = afterevent - list.append(' ', idx[action], '\n']) - - # Excludes - for title in timer.getExcludedTitle(): - list.extend([' ', stringToXML(title), '\n']) - for short in timer.getExcludedShort(): - list.extend([' ', stringToXML(short), '\n']) - for desc in timer.getExcludedDescription(): - list.extend([' ', stringToXML(desc), '\n']) - for day in timer.getExcludedDays(): - list.extend([' ', stringToXML(day), '\n']) - - # Includes - for title in timer.getIncludedTitle(): - list.extend([' ', stringToXML(title), '\n']) - for short in timer.getIncludedShort(): - list.extend([' ', stringToXML(short), '\n']) - for desc in timer.getIncludedDescription(): - list.extend([' ', stringToXML(desc), '\n']) - for day in timer.getIncludedDays(): - list.extend([' ', stringToXML(day), '\n']) - - # Tags - for tag in timer.tags: - list.extend([' ', stringToXML(tag), '\n']) - - # End of Timer - list.append(' \n\n') - - # End of Configuration - list.append('\n') - - # Save to Flash - file = open(XML_CONFIG, 'w') - file.writelines(list) - - file.close() +# Main function def parseEPG(self, simulateOnly = False): if NavigationInstance.instance is None: diff --git a/autotimer/src/AutoTimerComponent.py b/autotimer/src/AutoTimerComponent.py index 89b22129..7ce5b101 100644 --- a/autotimer/src/AutoTimerComponent.py +++ b/autotimer/src/AutoTimerComponent.py @@ -15,6 +15,13 @@ class AutoTimerComponent(object): self._afterevent = [] self.setValues(name, match, enabled, *args, **kwargs) + """ + Unsets all Attributes + """ + def clear(self, id = -1, enabled = False): + self.id = id + self.setValues('', '', enabled) + """ Create a deep copy of this instance """ diff --git a/autotimer/src/AutoTimerOverview.py b/autotimer/src/AutoTimerOverview.py index d56b3276..84035066 100644 --- a/autotimer/src/AutoTimerOverview.py +++ b/autotimer/src/AutoTimerOverview.py @@ -4,7 +4,7 @@ from Screens.HelpMenu import HelpableScreen from Screens.MessageBox import MessageBox from Screens.ChoiceBox import ChoiceBox from AutoTimerEditor import AutoTimerEditor -from AutoTimerConfiguration import AutoTimerConfiguration +from AutoTimerSettings import AutoTimerSettings from AutoTimerPreview import AutoTimerPreview from AutoTimerImporter import AutoTimerImportSelector @@ -181,7 +181,7 @@ class AutoTimerOverview(Screen, HelpableScreen): ) elif ret == "setup": self.session.open( - AutoTimerConfiguration + AutoTimerSettings ) elif ret == "defaults": self.session.open( -- 2.20.1