1"""
2PySCeS - Python Simulator for Cellular Systems (http://pysces.sourceforge.net)
3
4Copyright (C) 2004-2020 B.G. Olivier, J.M. Rohwer, J.-H.S Hofmeyr all rights reserved,
5
6Brett G. Olivier (bgoli@users.sourceforge.net)
7Triple-J Group for Molecular Cell Physiology
8Stellenbosch University, South Africa.
9
10Permission to use, modify, and distribute this software is given under the
11terms of the PySceS (BSD style) license. See LICENSE.txt that came with
12this distribution for specifics.
13
14NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
15Brett G. Olivier
16"""
17from __future__ import division, print_function
18from __future__ import absolute_import
19from __future__ import unicode_literals
20
21from pysces.version import __version__
22__doc__ = '''network and internet oriented utilities'''
23
24from time import strftime
25from getpass import getuser
26
27class PyscesHTML:
28    """PySCeS HTML formatting class: contains some basic html elements that can be used in generated reports."""
29    __version__ = __version__
30
31    def HTML_header(self,File):
32        """
33        HTML_header(File)
34
35        Write an HTML page header to file (use with HTML_footer)
36
37        Arguments:
38        =========
39        File: an open, writable Python file object
40
41        """
42        header = '\n'
43        header += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
44        header += '<html>\n'
45        header += '<head>\n'
46        header += '<title>PySCeS data generated at ' + strftime("%H:%M:%S (%Z)") + '</title>\n'
47        header += '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n'
48        header += '</head>\n'
49        header += '<body>\n\n'
50        header += '<h4><a href="http://pysces.sourceforge.net">PySCeS</a></h4>\n\n'
51        File.write(header)
52        File.write('<!-- PySCeS data generated at ' + strftime("%H:%M:%S") + '-->\n\n')
53        return File
54
55    def HTML_footer(self,File):
56        """
57        HTML_footer(File)
58
59        Write an HTML page footer to file (use with HTML_header)
60
61        Arguments:
62        =========
63        File: an open, writable Python file object
64
65        """
66        File.write('\n<p><a href="http://pysces.sourceforge.net"><font size="3">PySCeS '+__version__+\
67                   '</font></a><font size="2"> output\n generated at ' + strftime("%H:%M:%S")+\
68                   ' by <i>')
69        try:
70            File.write(getuser())
71        except:
72            File.write('PySCeS')
73        File.write('</i>)</font></p>\n')
74        File.write('</body>\n')
75        File.write('</html>\n\n')
76        return File
77
78    def par(self,str,File=None,align='l',txtout=0):
79        """
80        par(str,File=None,align='l',txtout=0)
81
82        Format <par> text and write it to a file (or string)
83
84        Arguments:
85        =========
86        str: the string of text to be written
87        File [default=None]: an open, writable, Python file object
88        align [default='l']: HTML alignment attribute ('l','c','r')
89        txtout [default=0]: do not write to file (1) return formatted HTML string
90
91        """
92        if not txtout:
93            assert type(File) == file, 'The 2nd argument needs to be an open file'
94        if align == 'l':
95            align = 'left'
96        elif align == 'r':
97            align == 'right'
98        elif align == 'c':
99            align = 'center'
100        else:
101            align = ''
102
103        strout = '\n<p align="'+align+'">'
104        cntr = 0
105        max_str_len = 75
106        seeker_active = 0
107        for x in range(len(str)):
108            cntr += 1
109            strout += str[x]
110            if seeker_active:
111                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
112                    cntr = max_str_len
113                    seeker_active = 0
114            if cntr >= max_str_len:
115                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
116                    strout += '\n '
117                else:
118                    seeker_active = 1
119                cntr = 0
120
121        strout += '\n</p>\n'
122        if txtout:
123            return strout
124        else:
125            File.write(strout)
126        del str
127        del strout
128
129    def h1(self,str,File=None,align='l',txtout=0):
130        """
131        h1(str,File=None,align='l',txtout=0)
132
133        Format <h1> text and write it to a file (or string)
134
135        Arguments:
136        =========
137        str: the string of text to be written
138        File [default=None]: an open, writable, Python file object
139        align [default='l']: HTML alignment attribute ('l','c','r')
140        txtout [default=0]: do not write to file (1) return formatted HTML string
141
142        """
143        if not txtout:
144            assert type(File) == file, 'The 2nd argument needs to be an open file'
145        if align == 'l':
146            align = 'left'
147        elif align == 'r':
148            align == 'right'
149        elif align == 'c':
150            align = 'center'
151        else:
152            align = ''
153
154        strout = '\n<h1 align="'+align+'">'
155
156        cntr = 0
157        max_str_len = 75
158        seeker_active = 0
159        for x in range(len(str)):
160            cntr += 1
161            strout += str[x]
162            if seeker_active:
163                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
164                    cntr = max_str_len
165                    seeker_active = 0
166            if cntr >= max_str_len:
167                print(str[x])
168                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
169                    strout += '\n '
170                else:
171                    seeker_active = 1
172                cntr = 0
173
174        strout += '\n</h1>\n'
175        if txtout:
176            return strout
177        else:
178            File.write(strout)
179        del str
180        del strout
181
182
183    def h2(self,str,File=None,align='l',txtout=0):
184        """
185        h2(str,File=None,align='l',txtout=0)
186
187        Format <h2> text and write it to a file (or string)
188
189        Arguments:
190        =========
191        str: the string of text to be written
192        File [default=None]: an open, writable, Python file object
193        align [default='l']: HTML alignment attribute ('l','c','r')
194        txtout [default=0]: do not write to file (1) return formatted HTML string
195
196        """
197        if not txtout:
198            assert type(File) == file, 'The 2nd argument needs to be an open file'
199        if align == 'l':
200            align = 'left'
201        elif align == 'r':
202            align == 'right'
203        elif align == 'c':
204            align = 'center'
205        else:
206            align = ''
207
208        strout = '\n<h2 align="'+align+'">'
209        cntr = 0
210        max_str_len = 75
211        seeker_active = 0
212        for x in range(len(str)):
213            cntr += 1
214            strout += str[x]
215            if seeker_active:
216                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
217                    cntr = max_str_len
218                    seeker_active = 0
219            if cntr >= max_str_len:
220                print(str[x])
221                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
222                    strout += '\n '
223                else:
224                    seeker_active = 1
225                cntr = 0
226
227        strout += '\n</h2>\n'
228        if txtout:
229            return strout
230        else:
231            File.write(strout)
232        del str
233        del strout
234
235
236    def h3(self,str,File=None,align='l',txtout=0):
237        """
238        h3(str,File=None,align='l',txtout=0)
239
240        Format <h3> text and write it to a file (or string)
241
242        Arguments:
243        =========
244        str: the string of text to be written
245        File [default=None]: an open, writable, Python file object
246        align [default='l']: HTML alignment attribute ('l','c','r')
247        txtout [default=0]: do not write to file (1) return formatted HTML string
248
249        """
250        if not txtout:
251            assert type(File) == file, 'The 2nd argument needs to be an open file'
252        if align == 'l':
253            align = 'left'
254        elif align == 'r':
255            align == 'right'
256        elif align == 'c':
257            align = 'center'
258        else:
259            align = ''
260
261        strout = '\n<h3 align="'+align+'">'
262        cntr = 0
263        max_str_len = 75
264        seeker_active = 0
265        for x in range(len(str)):
266            cntr += 1
267            strout += str[x]
268            if seeker_active:
269                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
270                    cntr = max_str_len
271                    seeker_active = 0
272            if cntr >= max_str_len:
273                print(str[x])
274                if str[x] == ' ' or str[x] == '.' or str[x] == ',':
275                    strout += '\n'
276                else:
277                    seeker_active = 1
278                cntr = 0
279
280        strout += '\n</h3>\n'
281        if txtout:
282            return strout
283        else:
284            File.write(strout)
285        del str
286        del strout
287
288
289import email
290#import email.mime.base.MIMEBase
291#import email.encoders
292#import email.mime.multipart.MIMEMultipart
293import email.utils
294import mimetypes
295
296import smtplib
297from email.mime.text import MIMEText
298
299from time import sleep, strftime
300from getpass import getuser
301
302import os
303
304class PyscesSMTP:
305    """A purely experimental class that extends PySCeS with SMTP mailer capabilities. Initialise with
306    sender address and local mail server name."""
307    __smtp_active = 0
308    def __init__(self,fromadd,server):
309        self.server = server
310        try:
311            self.userstr = getuser()
312        except:
313            self.userstr = 'PySCeS '
314        self.msgintro = ''
315        self.fromhead = self.userstr + ' <'+fromadd+'>'
316        self.signature = 3*'\n' + '---\nSent using PySCeS 0.2.2 (http://pysces.sourceforge.net/)\n '
317
318        # auto-open connection now closed
319        #self.SMTPOpen()
320
321    def GenericMail(self, toadd, msgtxt, subj='PySCeS generated email'):
322        """
323        GenericMail( toadd, msgtxt, subj='PySCeS generated email')
324
325        Generate and send a text (non-mime) email message
326
327        Arguments:
328        =========
329        toadd: recipient address
330        msgtxt: the message body as a string
331        subj [default='PySCeS generated email']: message subject line
332
333        """
334        assert type(msgtxt) == str, '\nMessage text must be a string'
335        assert self.__smtp_active, 'SMTP Server not active\n'
336
337        msgtxt = self.msgintro + msgtxt
338
339        msgtxt += self.signature
340        outer = MIMEText(msgtxt)
341
342        outer['Subject'] = subj
343        outer['To'] = toadd
344        outer['From'] = self.fromhead
345        outer['Date'] = email.Utils.formatdate(localtime='true')
346        outer.epilogue = ' '
347
348        if self.CheckGo():
349            try:
350                self.__SMTPserver.sendmail(self.fromhead,toadd,outer.as_string())
351            except SMTPServerDisconnected as e:
352                print(e)
353                self.SMTPOpen()
354                self.__SMTPserver.sendmail(self.fromhead,toadd,outer.as_string())
355            sleep(0.2)
356        else:
357            print('\nEmail send aborted')
358
359    def CheckGo(self):
360        """
361        CheckGo()
362
363        Do you want to continue yes or no?
364        Returns 1 or 0
365
366        Arguments:
367        None
368
369        """
370        GO = 1
371        while GO:
372            resp = input('\nDo you want to continue (yes/no): ')
373            if resp.lower() == 'yes':
374                print('OK.')
375                GO = 0
376                return 1
377            elif resp.lower() == 'no':
378                print('Skipped.')
379                GO = 0
380                return 0
381            else:
382                print('\nyes to continue, no to exit')
383
384##	def GenericMailHTML(self, toadd, msgtxt, htmltxt, subj='PySCeS generated email'):
385##        """
386##        GenericMailHTML( toadd, msgtxt, htmltxt, subj='PySCeS generated email')
387##
388##        Generate a mime-compliant HTML email message
389##
390##        Arguments:
391##        =========
392##        toadd: recipient address
393##        msgtxt: text only message string
394##        htmltxt: html formatted message string
395##        subj [default='PySCeS generated email']: the subject line
396##
397##        """
398##		assert type(msgtxt) == str, '\nMessage text must be a string'
399##		assert self.__smtp_active, 'SMTP Server not active\n'
400##		# Create the enclosing (outer) message
401##		outer = email.MIMEMultipart.MIMEMultipart()
402##		outer['Subject'] = subj
403##		outer['To'] = toadd
404##		outer['From'] = self.fromhead
405##		outer['Date'] = email.Utils.formatdate(localtime='true')
406##		outer.preamble = ' \n'
407##		outer.epilogue = '\n---\nGenerated by PySCeS 0.2.2\n '
408##
409##		msgtxt += self.signature
410##		msg = email.MIMEText.MIMEText(msgtxt)
411##		msg.add_header('Content-Disposition', 'inline')
412##		outer.attach(msg)
413##
414##		self.__SMTPserver.sendmail(self.fromhead,toadd,outer.as_string())
415##
416##		ctype='text/plain'
417##		maintype, subtype = ctype.split('/', 1)
418##		fp = open(infile, 'r')
419##		att = email.MIMEBase.MIMEBase(maintype, subtype)
420##		att.set_payload(fp.read())
421##		fp.close()
422##		# Encode the payload using Base64
423##		#email.Encoders.encode_base64(att)
424##		# Set the filename parameter
425##		att.add_header('Content-Disposition', 'attachment', filename=infile)
426##		outer.attach(att)
427##
428##		SMTPserver.sendmail(fromhead,toadd,outer.as_string())
429##
430##		sleep(0.2)      #seconds
431
432
433    def SMTPOpen(self):
434        """
435        SMTPOpen()
436
437        Start client and connect to an SMTP server
438
439        Arguments:
440        None
441
442        """
443        self.__SMTPserver = smtplib.SMTP(self.server)
444        self.__smtp_active = 1
445        print('\nSMTP server connection opened\n')
446
447    def SMTPClose(self):
448        """
449        SMTPClose()
450
451        Close connection to SMTP server
452
453        Arguments:
454        None
455
456        """
457        self.__SMTPserver.close()
458        self.__smtp_active = 0
459        print('\nSMTP server connection closed\n')
460
461if __name__ == '__main__':
462    replyTo = 'bgoli@sun.ac.za'
463    server = 'mail.sun.ac.za'
464    print('Reply to:', replyTo)
465    print('SMTP server:',server)
466    smtp = PyscesSMTP(replyTo,server)
467    smtp.GenericMail('bgoli@sun.ac.za','This test message created: '+ strftime("%a, %d %b %Y %H:%M:%S"))
468    #smtp.GenericMail('jr@sun.ac.za','This test message created: '+ strftime("%a, %d %b %Y %H:%M:%S"))
469    smtp.SMTPClose()
470
471
472
473