plain text parsing to html (newbie problem)

J

João

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 (e-mail address removed) < mail", shell=True)
output_file.close()
 
M

MRAB

João said:
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".
 
T

Terry Reedy

João said:
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
 
A

akean

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
 
J

João

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
 
J

João

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
 
L

Lie Ryan

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..

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
 
J

João

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
 
J

João

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
 
L

Lie Ryan

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( said:
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"?
 
J

João

Lie said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top