Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plain text parsing to html (newbie problem)

2 views
Skip to first unread message

João

unread,
Dec 9, 2009, 9:59:48 AM12/9/09
to
I apologize for my newbiness but I'm banging my head making this work :
(
What change must I made for the tag enforcement being reflected to the
'mail' file? Am I using the WritableObject class correctly?
(I'm getting a blank 'mail' file after running the .py script)
How can I see the output run in debug mode like in perl?

#!/usr/bin/env python

import sys, os
import subprocess
import re
import datetime

# simple class with write method to redirect print output to a file
class WritableObject:
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)

x=datetime.date.today()
yyyy, dd = x.year, x.day

mail = WritableObject()
print >>mail, "To: %s" % (mail1)
print >>mail, "Subject: %s day %s " % (yyyy, dd)
print >>mail, "Content-Type: text/html; charset=\"us-ascii\""

subprocess.call("php $HOME/report.php > temp.txt", shell=True)
body = subprocess.call("cat $HOME/.txt", shell=True)

print >>mail, ""
print >>mail, "<html>"
print >>mail, "<body>"

#copies common header in the Problem html to the OK one
subprocess.call("cp $HOME/mail $HOME/OK.html", shell=True)

input_file = open('mail', 'r+')
lines = input_file.readlines()
input_file.close()
output_file = open("mail", "w+")

for line in lines:
if line.startswith('<p>'):
line = '<p>' + line.rstrip('\n') + '</p>' + '\n'
output_file.write(line)

if not body:
print >>mail, '''</p> <b>[No problems detected ].<b /> All monitored
metrics
<p><font size=\"4\">
<font color=\"darkgreen\">
<b>OK<b>
</p>
</font>
</body>
</html>'''
else:
print >>mail, '''
<table border=\"1\"> <tr>
<th> Table</th>
<th> Table2</th>
<th> Table3</th>
</tr>
</table>
%s
</body>
</html>''' % (body)

subprocess.call("sendmail m...@my.mail < mail", shell=True)
output_file.close()

MRAB

unread,
Dec 9, 2009, 11:11:53 AM12/9/09
to pytho...@python.org
Jo�o wrote:
> I apologize for my newbiness but I'm banging my head making this work :
> (
> What change must I made for the tag enforcement being reflected to the
> 'mail' file? Am I using the WritableObject class correctly?
> (I'm getting a blank 'mail' file after running the .py script)
> How can I see the output run in debug mode like in perl?
>
[snip]

You're storing the text in the 'mail' object (an instance of
WritableObject), but at no point do you use what you have stored.

I can also see that you're writing to a file call "mail" via
output_file, but you're not closing the file before passing it to
"sendmail".

Terry Reedy

unread,
Dec 9, 2009, 12:04:21 PM12/9/09
to pytho...@python.org
João wrote:
> I apologize for my newbiness but I'm banging my head making this work :
> (
> What change must I made for the tag enforcement being reflected to the
> 'mail' file? Am I using the WritableObject class correctly?
> (I'm getting a blank 'mail' file after running the .py script)
> How can I see the output run in debug mode like in perl?
>
> #!/usr/bin/env python
>
> import sys, os
> import subprocess
> import re
> import datetime
>
> # simple class with write method to redirect print output to a file
> class WritableObject:
> def __init__(self):
> self.content = []
> def write(self, string):
> self.content.append(string)

Python comes with a stringio class that does this *AND* has a method to
retrieve the result. StringIO in 2.x, io module in 3.x

akean

unread,
Dec 9, 2009, 4:03:17 PM12/9/09
to
On Dec 10, 3:59 am, João <joao...@gmail.com> wrote:
> I apologize for my newbiness but I'm banging my head making this work :
> (
...

> How can I see the output run in debug mode like in perl?
>


One method: install ipython (another python shell, but with some
useful extra features)
and then run the program inside ipython in debug mode:
---------
$ ipython


In [1]: run -d filename.py


Breakpoint 1 at /path/to/filename.py:3
NOTE: Enter 'c' at the ipdb> prompt to start your script.
> <string>(1)<module>()

ipdb>
--------
(You type c to continue)
--------
ipdb> c
> /path/to/filename.py(3)<module>()
2
1---> 3 import sys, os
4 import subprocess

--------
and you can see you're now on line 3.
Press h for help to see what commands you can type.
Press n for next line if you want to step.
Press s to step into a function when it's being called. Etc.

--
Anita

João

unread,
Dec 10, 2009, 7:16:40 AM12/10/09
to
Thanks for the output.
akean, I've installed ipython and I'm exploring it. Thanks.

Terry,
from what I've read stringIO allows us to store strings in a 'virtual'
file.
Can you please write just 2 lines exemplifying a write to and a read
from an OS level file?

MRAB, that 'mail' object should've been the os level 'mail' file. But
as I'm a beginner I was using it wrong..

Thanks

Message has been deleted

Lie Ryan

unread,
Dec 10, 2009, 2:55:25 PM12/10/09
to

it's actually easier to use real files than creating a virtual file:
f = open('mail.txt', 'w')
f.write("foo\n") # or print >> f, "foo"

and, is there any reason why you're not using the email and smtplib?
http://docs.python.org/library/email-examples.html

João

unread,
Dec 11, 2009, 4:43:16 AM12/11/09
to
On Dec 10, 7:55 pm, Lie Ryan <lie.1...@gmail.com> wrote:
>
> and, is there any reason why you're not using the email and smtplib?http://docs.python.org/library/email-examples.html

Mainly because I was unaware of them :(

I just read about them and I found all the Subject, From, To classes,
but what about Content-Type?
I need Content-Type: text/html and charset="utf-8" so that I can pass
a basic CSS formatting.
How can I do that? With email.mime.base.MIMEBase?

I'll have to update that in my code later, as I already got it to work
and my current RHEL5.3 default
Python 2.4.3 (#1, Sep 17 2008) install is complaining with
"ImportError: No module named mime.multipart"

Thank you for your patience and feedback

João

unread,
Dec 11, 2009, 4:43:42 AM12/11/09
to

Lie Ryan

unread,
Dec 11, 2009, 5:33:43 AM12/11/09
to
On 12/11/2009 8:43 PM, Jo�o wrote:
> On Dec 10, 7:55 pm, Lie Ryan<lie.1...@gmail.com> wrote:
>>
>> and, is there any reason why you're not using the email and smtplib?http://docs.python.org/library/email-examples.html
>
> Mainly because I was unaware of them :(
>
> I just read about them and I found all the Subject, From, To classes,
> but what about Content-Type?
> I need Content-Type: text/html and charset="utf-8" so that I can pass
> a basic CSS formatting.
> How can I do that? With email.mime.base.MIMEBase?

You can set MIME type and encoding from the MIME constructor
email.mime.Text.MIMEText("<b>Bold Text</b>", "html", "utf-8")

> I'll have to update that in my code later, as I already got it to work
> and my current RHEL5.3 default
> Python 2.4.3 (#1, Sep 17 2008) install is complaining with
> "ImportError: No module named mime.multipart"

are you importing "import mime" or "import email.mime" or "import
email.MIMEMultipart"?

João

unread,
Dec 11, 2009, 10:22:49 AM12/11/09
to
Lie Ryan wrote:
> You can set MIME type and encoding from the MIME constructor
> email.mime.Text.MIMEText("<b>Bold Text</b>", "html", "utf-8")
>

> are you importing "import mime" or "import email.mime" or "import
> email.MIMEMultipart"?

Hi Lie.
I was importing as,
'from email.mime.text import MIMEText' which was returning that error
because I don't have
a mime subdir in my /usr/lib64/python2.4/email/ as I was expecting.

'import email.MIMEMultipart' worked perfectly (the mime methods are in
the /email/ root).
I'll try to redo my script right after I get some urgent things done.

Thanks :)

0 new messages