From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../sample.alerter/AlertConfigImpl.py | 175 +++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 tags/native-sca-1.0.incubating-M3-RC1/samples/AlertAggregator/sample.alerter/AlertConfigImpl.py (limited to 'tags/native-sca-1.0.incubating-M3-RC1/samples/AlertAggregator/sample.alerter/AlertConfigImpl.py') diff --git a/tags/native-sca-1.0.incubating-M3-RC1/samples/AlertAggregator/sample.alerter/AlertConfigImpl.py b/tags/native-sca-1.0.incubating-M3-RC1/samples/AlertAggregator/sample.alerter/AlertConfigImpl.py new file mode 100644 index 0000000000..2800a940b4 --- /dev/null +++ b/tags/native-sca-1.0.incubating-M3-RC1/samples/AlertAggregator/sample.alerter/AlertConfigImpl.py @@ -0,0 +1,175 @@ +# 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, os.path, os + +def getAlertConfig (): + + # Set up some default config data + configData = "\n" + + if os.path.isfile(os.environ['TUSCANY_SCACPP_ROOT']+"/config.xml"): + # Retrieve the configuration from a file + f = open(os.environ['TUSCANY_SCACPP_ROOT']+"/config.xml") + try: + configData = f.read() + finally: + f.close() + + configElem = xml.etree.ElementTree.XML(configData) + + return configElem + +def getSourceConfig (sourceId): + + configElem = getAlertConfig() + + for sourceElem in configElem.findall("./{http://tuscany.apache.org/samples/alerter}source"): + if sourceElem.attrib["id"] == sourceId: + return sourceElem + + print "Source with id matching",sourceId,"not found" + return + +def updateSourceConfig (alertSourceConfigElem): + + configElem = getAlertConfig() + sourceId = alertSourceConfigElem.attrib["id"] + + for sourceElem in configElem.findall("./{http://tuscany.apache.org/samples/alerter}source"): + if sourceElem.attrib["id"] == sourceId: + configElem.remove(sourceElem) + configElem.append(alertSourceConfigElem) + saveConfigData(configElem) + return + print "Source with id matching",sourceId,"not found" + return + +def addSourceConfig (alertSourceConfigElem): + configElem = getAlertConfig() + + #check for source ID, make sure it's not the same as others and create one if it's missing + if "id" in alertSourceConfigElem.attrib: + configId = alertSourceConfigElem.attrib["id"] + else: + configId = "0" + + gotConfigId = False + + while gotConfigId == False: + + config = getSourceConfig(configId) + if config: + configId = str( int(configId)+1 ) + else: + gotConfigId = True + + + alertSourceConfigElem.attrib["id"] = configId + + configElem.append(alertSourceConfigElem) + saveConfigData(configElem) + + return configId + +def saveConfigData (configElem): + configData = xml.etree.ElementTree.tostring(configElem) + + # Save the configuration to a file + f = open(os.environ['TUSCANY_SCACPP_ROOT']+"/config.xml", "w") + + if not f: + print "Failed to open config file for writing" + try: + f.write(configData) + finally: + f.close() + +def removeSourceConfig(sourceId): + configElem = getAlertConfig() + for sourceElem in configElem.findall("./{http://tuscany.apache.org/samples/alerter}source"): + if sourceElem.attrib["id"] == sourceId: + configElem.remove(sourceElem) + saveConfigData(configElem) + return + +# # Testing +# if os.path.isfile("config.xml"): +# os.remove("config.xml") +# data = getAlertConfig() +# print xml.etree.ElementTree.tostring(data), "\n" +# +# newsrcxml = "\n" +# newsrcxml += "PSP Updates\n" +# newsrcxml += "
http://pspupdates.qj.net
\n" +# newsrcxml += "http://pspupdates.qj.net/rss.xml" +# +# newsrc = xml.etree.ElementTree.XML(newsrcxml) +# ID = addSourceConfig(newsrc) +# print "Added src with ID:", ID, "\n" +# +# newsrcxml = "\n" +# newsrcxml += "PSP Updates\n" +# newsrcxml += "
http://pspupdates.qj.net!
\n" +# newsrcxml += "http://pspupdates.qj.net/atom.xml" +# +# newsrc = xml.etree.ElementTree.XML(newsrcxml) +# ID = addSourceConfig(newsrc) +# print "Added src with ID:", ID, "\n" +# +# data = getAlertConfig() +# print xml.etree.ElementTree.tostring(data), "\n" +# +# data = getSourceConfig("0") +# print xml.etree.ElementTree.tostring(data), "\n" +# print dir(data), "\n" +# +# name = data.find("./{http://tuscany.apache.org/samples/alerter}name") +# name.text = "Hello world!" +# +# updateSourceConfig(data) +# data = getSourceConfig("1") +# print xml.etree.ElementTree.tostring(data), "\n" +# +# removeSourceConfig("1") +# data = getAlertConfig() +# print xml.etree.ElementTree.tostring(data), "\n" +# +# ID = addSourceConfig(newsrc) +# print "Added src with ID:", ID, "\n" +# +# ID = addSourceConfig(newsrc) +# print "Added src with ID:", ID, "\n" +# +# data = getAlertConfig() +# print xml.etree.ElementTree.tostring(data), "\n" +# +# +# newsrcxml = "\n" +# newsrcxml += "PSP Updates\n" +# newsrcxml += "
http://pspupdates.qj.net!
\n" +# newsrcxml += "http://pspupdates.qj.net/atom.xml" +# +# newsrc = xml.etree.ElementTree.XML(newsrcxml) +# ID = addSourceConfig(newsrc) +# print "Added src with ID:", ID, "\n" +# +# data = getAlertConfig() +# print xml.etree.ElementTree.tostring(data), "\n" +# + -- cgit v1.2.3