# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # # import xml.etree.ElementTree, datetime, os.path, os ns = "./{http://tuscany.apache.org/samples/alerter}" def getSavedAlerts(): # Set up some default config data alertsData = "\n" if os.path.isfile(os.environ['TUSCANY_SCACPP_ROOT']+"/sample.display/alerts.xml"): # Retrieve the configuration from a file f = open(os.environ['TUSCANY_SCACPP_ROOT']+"/sample.display/alerts.xml") try: alertsData = f.read() finally: f.close() alertsElem = xml.etree.ElementTree.XML(alertsData) return alertsElem def saveAlerts(alertsElem): alertsData = xml.etree.ElementTree.tostring(alertsElem) # Save the configuration to a file f = open(os.environ['TUSCANY_SCACPP_ROOT']+"/sample.display/alerts.xml", "w") if not f: print "Failed to open alerts file for writing" try: f.write(alertsData) finally: f.close() def updateAllAlertsHTMLTable (): alertsElem = getSavedAlerts() # Use the alertService reference newAlertsElem = alertService.getAllNewAlerts() #newAlertsElem = getAllNewAlerts(); # For testing for alert in newAlertsElem.findall(ns+"alert"): alert.attrib["unread"]="True" alertsElem.append(alert) return generateHTMLTable(alertsElem) def updateSourceAlertsHTMLTable (sourceId): alertsElem = getSavedAlerts() # Use the alertService reference newAlertsElem = alertService.getNewAlerts(sourceId); #newAlertsElem = getAllNewAlerts(); # For testing for alert in newAlertsElem.findall(ns+"alert"): alert.attrib["unread"]="True" alertsElem.append(alert) return generateHTMLTable(alertsElem) def generateHTMLTable (alertsElem): returnData = "" # If we have more than x alerts, clear out any that have been read # x is determined by the showNumberOfReadAlerts property alerts = alertsElem.findall(ns+"alert") if len(alerts) > int(showNumberOfReadAlerts): for alert in alerts: if alert.attrib["unread"]!="True": alertsElem.remove(alert) alertIDNumber = 0 for alert in alertsElem.findall(ns+"alert"): date = datetime.datetime.strptime(alert.find(ns+"date").text, "%Y-%m-%dT%H:%M:%S") alertID = "alert_"+str(alertIDNumber) alert.attrib["id"]= alertID alertIDNumber += 1 returnData += "\n" returnData += "
" title = alert.find(ns+"title").text noOfChars = 75-len(title) if noOfChars>0: returnData += title returnData += " - " returnData += alert.find(ns+"summary").text[0:noOfChars] returnData += "..." else: returnData += title[0:80] returnData += "" returnData += date.strftime("%d/%m/%Y %I:%M %p") returnData += "
" saveAlerts(alertsElem) #print xml.etree.ElementTree.tostring(alertsElem) return returnData def readAlert (alertID): alertsElem = getSavedAlerts() returnData = "" for alert in alertsElem.findall(ns+"alert"): if alert.attrib["id"]==alertID: alert.attrib["unread"] = "False" saveAlerts(alertsElem) srcType = getSourceType(alert.attrib["sourceid"]) if srcType=="pop": returnData += "
"
                returnData += alert.find(ns+"summary").text
                returnData += "
" return returnData return def getSourceType(sourceid): # Use the alertService reference alertSources = alertService.getAlertSources() #alertSources = getAlertSources() #testing for source in alertSources.findall(ns+"source"): if sourceid == source.attrib["id"]: return source.attrib["type"] return "none" def getAlertSourcesHTMLTable(): returnData = "\n" # Use the alertService reference alertSources = alertService.getAlertSources() #alertSources = getAlertSources() #testing srcIdList = [] for source in alertSources.findall(ns+"source"): srcId = source.attrib["id"] srcIdList.append(srcId) srcType = source.attrib["type"] # Write out the source data row returnData += "\n" # Now write out the row that gets shown when "edit" is pressed returnData += "" # Get the first unused srcId srcId = "0" while srcId in srcIdList: srcId = str(int(srcId)+1) # Now write out the "add new source" row returnData += "" # Add the (initially hidden) new source details row returnData += "" returnData += "
" returnData += "  " returnData += source.find(ns+"name").text returnData += "Update AlertsEdit SourceDelete Source
" if srcType=="rss": returnData += "" elif srcType=="pop": returnData += "" returnData += "" returnData += "" returnData += "
Source name:
Source address:
Feed address:
POP3 server:
Username:
Password:
Add new Alert Source
" # Section for RSS feeds returnData += "" # Section for POP3 email returnData += "" # Last row for buttons returnData += "
Source name:
Source address:
Source type:
Feed address:
" returnData += "" returnData += "
POP3 server:
Username:
Password:
\n" return returnData def deleteAlertSource (sourceId): # Use the alertService reference alertService.removeAlertSource(sourceId) # Remove all alerts with this sourceid alertsElem = getSavedAlerts() for alert in alertsElem.findall(ns+"alert"): if alert.attrib["sourceid"]==sourceId: alertsElem.remove(alert) saveAlerts(alertsElem) def addAlertSource(alertSourceElem): if xml.etree.ElementTree.iselement(alertSourceElem): # Use the alertService reference alertService.addAlertSource(alertSourceElem) return def updateAlertSource(alertSourceElem): if xml.etree.ElementTree.iselement(alertSourceElem): alertsElem = getSavedAlerts() # Get the sourceid sourceId = alertSourceElem.attrib["id"] # Remove all alerts with this sourceid - they may have changed for alert in alertsElem.findall(ns+"alert"): if alert.attrib["sourceid"]==sourceId: alertsElem.remove(alert) # Use the alertService reference to update the config alertService.updateAlertSource(alertSourceElem) saveAlerts(alertsElem) return # # For testing # showNumberOfReadAlerts = 3 # # def getAllNewAlerts (): # # data = "" # # data += "Arrests over 'terror kidnap plot'" # data += "Anti-terror police arrest eight people in Birmingham over an alleged plot to kidnap a member of the armed forces." # data += "
http://news.bbc.co.uk/go/rss/-/1/hi/uk/6315989.stm
2007-01-31T15:34:18
" # # data += "
" # # return xml.etree.ElementTree.XML(data) # # print getAlertsHTMLTable () + "\n" # # readAlert("http://news.bbc.co.uk/go/rss/-/1/hi/uk/6315989.stm") # # print getAlertsHTMLTable () + "\n" # # def getAlertSources (): # # data = "" # data += "BBC News" # data += "
http://news.bbc.co.uk/
" # data += "2007-02-02T10:08:40" # data += "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml" # data += "
" # return xml.etree.ElementTree.XML(data) # # print getAlertSourcesHTMLTable()