root/galaxy-central/eggs/WebError-0.8a-py2.6.egg/weberror/exceptions/reporter.py

リビジョン 3, 4.0 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3
4from email.MIMEText import MIMEText
5from email.MIMEMultipart import MIMEMultipart
6import smtplib
7import time
8from weberror.exceptions import formatter
9
10class Reporter(object):
11
12    def __init__(self, **conf):
13        for name, value in conf.items():
14            if not hasattr(self, name):
15                raise TypeError(
16                    "The keyword argument %s was not expected"
17                    % name)
18            setattr(self, name, value)
19        self.check_params()
20
21    def check_params(self):
22        pass
23
24    def format_date(self, exc_data):
25        return time.strftime('%c', exc_data.date)
26
27    def format_html(self, exc_data, **kw):
28        return formatter.format_html(exc_data, **kw)
29
30    def format_text(self, exc_data, **kw):
31        return formatter.format_text(exc_data, **kw)
32
33class EmailReporter(Reporter):
34
35    to_addresses = None
36    from_address = None
37    smtp_server = 'localhost'
38    subject_prefix = ''
39
40    def report(self, exc_data):
41        msg = self.assemble_email(exc_data)
42        server = smtplib.SMTP(self.smtp_server)
43        server.sendmail(self.from_address,
44                        self.to_addresses, msg.as_string())
45        server.quit()
46
47    def check_params(self):
48        if not self.to_addresses:
49            raise ValueError("You must set to_addresses")
50        if not self.from_address:
51            raise ValueError("You must set from_address")
52        if isinstance(self.to_addresses, (str, unicode)):
53            self.to_addresses = [self.to_addresses]
54
55    def assemble_email(self, exc_data):
56        short_html_version = self.format_html(
57            exc_data, show_hidden_frames=False)[0]
58        long_html_version = self.format_html(
59            exc_data, show_hidden_frames=True)[0]
60        text_version = self.format_text(
61            exc_data, show_hidden_frames=False)[0]
62        msg = MIMEMultipart()
63        msg.set_type('multipart/alternative')
64        msg.preamble = msg.epilogue = ''
65        text_msg = MIMEText(text_version)
66        text_msg.set_type('text/plain')
67        text_msg.set_param('charset', 'ASCII')
68        msg.attach(text_msg)
69        html_msg = MIMEText(short_html_version)
70        html_msg.set_type('text/html')
71        # @@: Correct character set?
72        html_msg.set_param('charset', 'UTF-8')
73        html_long = MIMEText(long_html_version)
74        html_long.set_type('text/html')
75        html_long.set_param('charset', 'UTF-8')
76        msg.attach(html_msg)
77        msg.attach(html_long)
78        subject = '%s: %s' % (exc_data.exception_type,
79                           formatter.truncate(str(exc_data.exception_value)))
80        msg['Subject'] = self.subject_prefix + subject
81        msg['From'] = self.from_address
82        msg['To'] = ', '.join(self.to_addresses)
83        return msg
84
85class LogReporter(Reporter):
86
87    filename = None
88    show_hidden_frames = True
89
90    def check_params(self):
91        assert self.filename is not None, (
92            "You must give a filename")
93
94    def report(self, exc_data):
95        text, head_text = self.format_text(
96            exc_data, show_hidden_frames=self.show_hidden_frames)
97        f = open(self.filename, 'a')
98        try:
99            f.write(text + '\n' + '-'*60 + '\n')
100        finally:
101            f.close()
102
103class FileReporter(Reporter):
104
105    file = None
106    show_hidden_frames = True
107
108    def check_params(self):
109        assert self.file is not None, (
110            "You must give a file object")
111
112    def report(self, exc_data):
113        text = self.format_text(
114            exc_data, show_hidden_frames=self.show_hidden_frames)
115        print text
116        self.file.write(text + '\n' + '-'*60 + '\n')
117
118class WSGIAppReporter(Reporter):
119
120    def __init__(self, exc_data):
121        self.exc_data = exc_data
122
123    def __call__(self, environ, start_response):
124        start_response('500 Server Error', [('Content-type', 'text/html')])
125        return [formatter.format_html(self.exc_data)]
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。