1 | from DateTime import DateTime |
---|
2 | from xml.dom.minidom import * |
---|
3 | |
---|
4 | def getAllTextFromTag(doc, tag_name): |
---|
5 | text = '' |
---|
6 | result = [] |
---|
7 | bad_char = False |
---|
8 | list_tag_elements = doc.getElementsByTagName(tag_name) |
---|
9 | for tag in list_tag_elements: |
---|
10 | list_subelements = tag.getElementsByTagName('*') |
---|
11 | list_subelements.insert(0, tag) |
---|
12 | for element in list_subelements: |
---|
13 | if element.childNodes[0].nodeValue: |
---|
14 | text += element.childNodes[0].nodeValue.strip() |
---|
15 | result.append(text) |
---|
16 | text = '' |
---|
17 | |
---|
18 | return result |
---|
19 | |
---|
20 | '''def response_message(id): |
---|
21 | |
---|
22 | doctype = DocumentType(""" xiamSMS SYSTEM "xiamSMSMessage.dtd" """) |
---|
23 | doc = Document() |
---|
24 | doc.appendChild(doctype) |
---|
25 | |
---|
26 | #create a 'xiamSMS' tag |
---|
27 | xiam_element = doc.createElement("xiamSMS") |
---|
28 | xiam_element.setAttribute("status", "OK") |
---|
29 | doc.appendChild(xiam_element) |
---|
30 | |
---|
31 | #create a 'submitRequest' tag |
---|
32 | response_element=doc.createElement("submitRsponse") |
---|
33 | response_element.setAttribute("id", id) |
---|
34 | response = doc.createTextNode('XML contained your response messages') |
---|
35 | response_element.appendChild(response) |
---|
36 | xiam_element.appendChild(response_element) |
---|
37 | |
---|
38 | return doc.toxml()''' |
---|
39 | |
---|
40 | """class xiamSMS: |
---|
41 | |
---|
42 | def __init__(self, id, originator, recipients, request, req_time = None, response = None, res_time = None): |
---|
43 | self.id = id |
---|
44 | self.originator = originator |
---|
45 | self.recipients = recipients |
---|
46 | self.request = request |
---|
47 | self.req_time = req_time |
---|
48 | self.response = response |
---|
49 | self.response_status = None |
---|
50 | self.res_time = res_time |
---|
51 | |
---|
52 | def setOriginator(self, originator): |
---|
53 | setattr(self, 'originator', originator) |
---|
54 | |
---|
55 | def setRecipients(self, recipients): |
---|
56 | setattr(self, 'recipients', recipients) |
---|
57 | |
---|
58 | def setRequest(self, request): |
---|
59 | setattr(self, 'request', request) |
---|
60 | |
---|
61 | def setReq_time(self, req_time): |
---|
62 | setattr(self, 'req_time', req_time) |
---|
63 | |
---|
64 | def setResponse(self, response): |
---|
65 | setattr(self, 'response', response) |
---|
66 | |
---|
67 | def setResponse_status(self, response_status): |
---|
68 | setattr(self, 'response_status', response_status) |
---|
69 | |
---|
70 | def setRes_time(self, res_time): |
---|
71 | setattr(self, 'res_time', res_time) |
---|
72 | |
---|
73 | def getOriginator(self): |
---|
74 | return getattr(self, 'originator') |
---|
75 | |
---|
76 | def getRecipients(self): |
---|
77 | return getattr(self, 'recipients') |
---|
78 | |
---|
79 | def getRequest(self): |
---|
80 | return getattr(self, 'request') |
---|
81 | |
---|
82 | def getReq_time(self): |
---|
83 | return getattr(self, 'req_time') |
---|
84 | |
---|
85 | def getResponse(self): |
---|
86 | return getattr(self, 'response') |
---|
87 | |
---|
88 | def getResponse_status(self): |
---|
89 | return getattr(self, 'response_status') |
---|
90 | |
---|
91 | def getRes_time(self): |
---|
92 | return getattr(self, 'res_time')""" |
---|
93 | |
---|
94 | class SMSsubmitRequest: |
---|
95 | def __init__(self, originator, destination, body): |
---|
96 | self.id = self.genSMS_id() |
---|
97 | self.originator=originator |
---|
98 | self.destination=destination |
---|
99 | self.body=body |
---|
100 | |
---|
101 | def genSMS_id(self): |
---|
102 | sms_id = "quinta%s" % DateTime().strftime("%Y%m%d%H%M%S") |
---|
103 | return sms_id |
---|
104 | |
---|
105 | def toXML(self): |
---|
106 | """convert SMSsubmitRequest to XML format""" |
---|
107 | if not self.id: |
---|
108 | id = "quinta5" |
---|
109 | |
---|
110 | #create a document |
---|
111 | doctype = DocumentType(""" xiamSMS SYSTEM "xiamSMSMessage.dtd" """) |
---|
112 | doc = Document() |
---|
113 | doc.appendChild(doctype) |
---|
114 | |
---|
115 | #create a 'xiamSMS' tag |
---|
116 | xiam_element = doc.createElement("xiamSMS") |
---|
117 | doc.appendChild(xiam_element) |
---|
118 | |
---|
119 | #create a 'submitRequest' tag |
---|
120 | request_element=doc.createElement("submitRequest") |
---|
121 | xiam_element.appendChild(request_element) |
---|
122 | request_element.setAttribute("id", self.id) |
---|
123 | |
---|
124 | #create a 'from' tag |
---|
125 | if not self.originator: |
---|
126 | raise NoOriginatorError("There is no sender number") |
---|
127 | originator_element=doc.createElement("from") |
---|
128 | number=doc.createTextNode(str(self.originator)) |
---|
129 | originator_element.appendChild(number) |
---|
130 | request_element.appendChild(originator_element) |
---|
131 | |
---|
132 | #create a 'to' tag |
---|
133 | if not self.destination: |
---|
134 | raise DestinatioNumberError ("There is no one destination number") |
---|
135 | for pn in self.destination: |
---|
136 | destination_element=doc.createElement("to") |
---|
137 | number=doc.createTextNode(str(pn)) |
---|
138 | destination_element.appendChild(number) |
---|
139 | request_element.appendChild(destination_element) |
---|
140 | |
---|
141 | #create a 'content' tag |
---|
142 | content_element=doc.createElement("content") |
---|
143 | content_element.setAttribute("type", "text") |
---|
144 | body=doc.createTextNode(str(self.body)) |
---|
145 | content_element.appendChild(body) |
---|
146 | request_element.appendChild(content_element) |
---|
147 | |
---|
148 | return doc.toxml(encoding = "UTF-8") |
---|
149 | |
---|
150 | class DestinatioNumberError(Exception): |
---|
151 | pass |
---|
152 | |
---|
153 | class NoOriginatorError(Exception): |
---|
154 | pass |
---|