1 | # Author: Melnychuk Taras |
---|
2 | # Contact: fenix@quintagroup.com |
---|
3 | # Date: $Date: 2005-12-23 11:56:23 +0200 (Fri, 23 Dec 2005) $ |
---|
4 | # Copyright: quintagroup.com |
---|
5 | |
---|
6 | """ |
---|
7 | pyXIAM is module that defines such classes and functions: |
---|
8 | |
---|
9 | - `SMSsubmitRequest`, this class define SMSsubmitRepuest type and takes responsibility |
---|
10 | for creating instance of this class and converting it to xml format. |
---|
11 | |
---|
12 | Methods: |
---|
13 | |
---|
14 | - `SMSsubmitRequest.genSMS_id`: generate id for message |
---|
15 | - `SMSsubmitRequest.toXML`: convert SMSsubmitRequest instance to xml format |
---|
16 | Functions: |
---|
17 | |
---|
18 | - `getAllTextFromTag`: return list of text elements that is nested in 'tag_name' |
---|
19 | |
---|
20 | Exception classes: |
---|
21 | |
---|
22 | - `DestinatioNumberError` |
---|
23 | - `NoOriginatorError` |
---|
24 | |
---|
25 | """ |
---|
26 | |
---|
27 | __docformat__ = 'restructuredtext' |
---|
28 | |
---|
29 | from DateTime import DateTime |
---|
30 | from xml.dom.minidom import * |
---|
31 | |
---|
32 | def getAllTextFromTag(doc, tag_name): |
---|
33 | """ |
---|
34 | return list of text elements that is nested in 'tag_name' |
---|
35 | Parameters: |
---|
36 | |
---|
37 | - `doc`: this parameter must contain xml document |
---|
38 | - `tag_name`: this is the name of needed tag |
---|
39 | """ |
---|
40 | text = '' |
---|
41 | result = [] |
---|
42 | bad_char = False |
---|
43 | list_tag_elements = doc.getElementsByTagName(tag_name) |
---|
44 | for tag in list_tag_elements: |
---|
45 | list_subelements = tag.getElementsByTagName('*') |
---|
46 | list_subelements.insert(0, tag) |
---|
47 | for element in list_subelements: |
---|
48 | if element.childNodes[0].nodeValue: |
---|
49 | text += element.childNodes[0].nodeValue.strip() |
---|
50 | result.append(text) |
---|
51 | text = '' |
---|
52 | |
---|
53 | return result |
---|
54 | |
---|
55 | '''def response_message(id): |
---|
56 | |
---|
57 | doctype = DocumentType(""" xiamSMS SYSTEM "xiamSMSMessage.dtd" """) |
---|
58 | doc = Document() |
---|
59 | doc.appendChild(doctype) |
---|
60 | |
---|
61 | #create a 'xiamSMS' tag |
---|
62 | xiam_element = doc.createElement("xiamSMS") |
---|
63 | xiam_element.setAttribute("status", "OK") |
---|
64 | doc.appendChild(xiam_element) |
---|
65 | |
---|
66 | #create a 'submitRequest' tag |
---|
67 | response_element=doc.createElement("submitRsponse") |
---|
68 | response_element.setAttribute("id", id) |
---|
69 | response = doc.createTextNode('XML contained your response messages') |
---|
70 | response_element.appendChild(response) |
---|
71 | xiam_element.appendChild(response_element) |
---|
72 | |
---|
73 | return doc.toxml()''' |
---|
74 | |
---|
75 | """class xiamSMS: |
---|
76 | |
---|
77 | def __init__(self, id, originator, recipients, request, req_time = None, response = None, res_time = None): |
---|
78 | self.id = id |
---|
79 | self.originator = originator |
---|
80 | self.recipients = recipients |
---|
81 | self.request = request |
---|
82 | self.req_time = req_time |
---|
83 | self.response = response |
---|
84 | self.response_status = None |
---|
85 | self.res_time = res_time |
---|
86 | |
---|
87 | def setOriginator(self, originator): |
---|
88 | setattr(self, 'originator', originator) |
---|
89 | |
---|
90 | def setRecipients(self, recipients): |
---|
91 | setattr(self, 'recipients', recipients) |
---|
92 | |
---|
93 | def setRequest(self, request): |
---|
94 | setattr(self, 'request', request) |
---|
95 | |
---|
96 | def setReq_time(self, req_time): |
---|
97 | setattr(self, 'req_time', req_time) |
---|
98 | |
---|
99 | def setResponse(self, response): |
---|
100 | setattr(self, 'response', response) |
---|
101 | |
---|
102 | def setResponse_status(self, response_status): |
---|
103 | setattr(self, 'response_status', response_status) |
---|
104 | |
---|
105 | def setRes_time(self, res_time): |
---|
106 | setattr(self, 'res_time', res_time) |
---|
107 | |
---|
108 | def getOriginator(self): |
---|
109 | return getattr(self, 'originator') |
---|
110 | |
---|
111 | def getRecipients(self): |
---|
112 | return getattr(self, 'recipients') |
---|
113 | |
---|
114 | def getRequest(self): |
---|
115 | return getattr(self, 'request') |
---|
116 | |
---|
117 | def getReq_time(self): |
---|
118 | return getattr(self, 'req_time') |
---|
119 | |
---|
120 | def getResponse(self): |
---|
121 | return getattr(self, 'response') |
---|
122 | |
---|
123 | def getResponse_status(self): |
---|
124 | return getattr(self, 'response_status') |
---|
125 | |
---|
126 | def getRes_time(self): |
---|
127 | return getattr(self, 'res_time')""" |
---|
128 | |
---|
129 | class SMSsubmitRequest: |
---|
130 | """ |
---|
131 | This class define SMSsubmitRepuest type and takes responsibility |
---|
132 | for creating instance of this class and converting it to xml format. |
---|
133 | |
---|
134 | Fields: |
---|
135 | |
---|
136 | - id: sms id |
---|
137 | - originator: contains phone number of message originator |
---|
138 | - destination: contains phone number of destination |
---|
139 | - body: this is the text of your message |
---|
140 | """ |
---|
141 | def __init__(self, originator, destination, body): |
---|
142 | self.id = self.genSMS_id() |
---|
143 | self.originator=originator |
---|
144 | self.destination=destination |
---|
145 | self.body=body |
---|
146 | |
---|
147 | def genSMS_id(self): |
---|
148 | """ |
---|
149 | generate id for message |
---|
150 | """ |
---|
151 | sms_id = "quinta%s" % DateTime().strftime("%Y%m%d%H%M%S") |
---|
152 | return sms_id |
---|
153 | |
---|
154 | def toXML(self): |
---|
155 | """ |
---|
156 | convert SMSsubmitRequest instance to xml format |
---|
157 | """ |
---|
158 | if not self.id: |
---|
159 | id = "quinta5" |
---|
160 | |
---|
161 | #create a document |
---|
162 | doctype = DocumentType(""" xiamSMS SYSTEM "xiamSMSMessage.dtd" """) |
---|
163 | doc = Document() |
---|
164 | doc.appendChild(doctype) |
---|
165 | |
---|
166 | #create a 'xiamSMS' tag |
---|
167 | xiam_element = doc.createElement("xiamSMS") |
---|
168 | doc.appendChild(xiam_element) |
---|
169 | |
---|
170 | #create a 'submitRequest' tag |
---|
171 | request_element=doc.createElement("submitRequest") |
---|
172 | xiam_element.appendChild(request_element) |
---|
173 | request_element.setAttribute("id", self.id) |
---|
174 | |
---|
175 | #create a 'from' tag |
---|
176 | if not self.originator: |
---|
177 | raise NoOriginatorError("There is no sender number") |
---|
178 | originator_element=doc.createElement("from") |
---|
179 | number=doc.createTextNode(str(self.originator)) |
---|
180 | originator_element.appendChild(number) |
---|
181 | request_element.appendChild(originator_element) |
---|
182 | |
---|
183 | #create a 'to' tag |
---|
184 | if not self.destination: |
---|
185 | raise DestinatioNumberError ("There is no one destination number") |
---|
186 | for pn in self.destination: |
---|
187 | destination_element=doc.createElement("to") |
---|
188 | number=doc.createTextNode(str(pn)) |
---|
189 | destination_element.appendChild(number) |
---|
190 | request_element.appendChild(destination_element) |
---|
191 | |
---|
192 | #create a 'content' tag |
---|
193 | content_element=doc.createElement("content") |
---|
194 | content_element.setAttribute("type", "text") |
---|
195 | body=doc.createTextNode(str(self.body)) |
---|
196 | content_element.appendChild(body) |
---|
197 | request_element.appendChild(content_element) |
---|
198 | |
---|
199 | return doc.toxml(encoding = "UTF-8") |
---|
200 | |
---|
201 | class DestinatioNumberError(Exception): |
---|
202 | pass |
---|
203 | |
---|
204 | class NoOriginatorError(Exception): |
---|
205 | pass |
---|