Changeset 408


Ignore:
Timestamp:
07/19/10 17:23:08 (14 years ago)
Author:
bas
Message:

port self.DEBUG output to the new logging module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/email2trac.py.in

    r407 r408  
    191191                self.setup_parameters()
    192192
    193                 self.DRY_RUN = parameters.dry_run
    194193
    195194        def setup_log(self):
     
    222221                        self.log_handler = logging.handlers.BufferingHandler(0)
    223222
     223
    224224                if self.parameters.log_format:
    225225                        self.parameters.log_format = self.parameters.log_format.replace('$(', '%(')
     
    227227                        self.parameters.log_format = 'Email2trac: %(message)s'
    228228
    229 
    230229                self.logger = logging.getLogger('email2trac')
    231230
     
    237236                        self.parameters.log_level = self.parameters.log_level.upper()
    238237
    239                 if self.parameters.log_level in ['DEBUG', 'ALL']:
     238                if self.parameters.log_level in ['DEBUG', 'ALL'] and self.parameters.debug > 0:
    240239                        self.logger.setLevel(logging.DEBUG)
    241240
     
    265264                        os.umask(int(self.parameters['umask'], 8))
    266265
    267                 if self.parameters.has_key('debug'):
    268                         self.DEBUG = int(self.parameters['debug'])
    269                 else:
    270                         self.DEBUG = 0
     266                if self.parameters.debug:
     267                        self.parameters.debug = int(self.parameters.debug)
     268                else:
     269                        self.parameters.debug = 0
    271270
    272271                if self.parameters.has_key('mailto_link'):
     
    445444                #
    446445                if self.DROP_SPAM and spam:
    447                         if self.DEBUG > 2 :
    448                                 print 'This message is a SPAM. Automatic ticket insertion refused (SPAM level > %d' % self.SPAM_LEVEL
     446                        if self.parameters.debug > 2 :
     447                                set.logger.debug('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' % self.SPAM_LEVEL)
    449448
    450449                        return 'drop'   
     
    472471
    473472                except KeyError, detail:
    474                         if self.DEBUG > 2 :
    475                                 print 'TD: %s not defined, all messages are allowed.' %(keyword)
     473                        if self.parameters.debug > 2 :
     474                                self.logger.debug('%s not defined, all messages are allowed.' %(keyword))
    476475
    477476                        return default
     
    544543                Set all the right fields for a new ticket
    545544                """
    546                 if self.DEBUG:
    547                         print 'TD: set_reply_fields'
     545                self.logger.info('function set_reply_fields')
    548546
    549547                ## Only use name or email adress
     
    584582
    585583                                if addr == self.trac_smtp_from:
    586                                         if self.DEBUG:
    587                                                 print "Skipping %s mail address for CC-field" %(addr)
     584                                        if self.parameters.debug:
     585                                                self.logger.debug("Skipping %s mail address for CC-field" %(addr))
    588586                                        continue
    589587
     
    594592
    595593                        if email_cc:
    596                                 if self.DEBUG:
    597                                         print 'TD: set_reply_fields: %s' %email_cc
     594                                if self.parameters.debug:
     595                                                self.logger.debug('set_reply_fields: %s' %email_cc)
    598596
    599597                                ticket['cc'] = self.email_to_unicode(email_cc)
     
    775773                """
    776774                try:
    777                         print s
     775                        self.logger.debug(s)
    778776                except UnicodeEncodeError, detail:
    779                         print util.text.unicode_quote(s)
     777                        self.logger.debug(util.text.unicode_quote(s))
    780778
    781779########## TRAC ticket functions  ###########################################################
     
    785783                Check if the mailer is allowed to update the ticket
    786784                """
     785                self.logger.info('function check_permission_participants')
    787786
    788787                if tkt['reporter'].lower() in [self.author, self.email_addr]:
    789                         if self.DEBUG:
    790                                 print 'ALLOW, %s is the ticket reporter' %(self.email_addr)
     788                        if self.parameters.debug:
     789                                self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr))
     790
    791791                        return True
    792792
    793793                perm = PermissionSystem(self.env)
    794794                if perm.check_permission('TICKET_MODIFY', self.author):
    795                         if self.DEBUG:
    796                                 print 'ALLOW, %s has trac permission to update the ticket' %(self.author)
     795                        if self.parameters.debug:
     796                                self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author))
     797
    797798                        return True
     799
    798800                else:
    799801                        return False
     
    806808                                if self.email_addr.lower() in cc.strip():
    807809
    808                                         if self.DEBUG:
    809                                                 print 'ALLOW, %s is in the CC' %(self.email_addr)
     810                                        if self.parameters.debug:
     811                                                self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr))
    810812
    811813                                        return True
     
    875877                #
    876878                for field,value in user_dict.items():
    877                         if self.DEBUG >= 10:
     879                        if self.parameters.debug:
    878880                                s = 'TD: user_field\t %s = %s' %(field,value)
    879881                                self.print_unicode(s)
     
    886888
    887889                                if self.trac_smtp_from in cc_list:
    888                                         if self.DEBUG > 10:
    889                                                 print 'TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from)
     890                                        if self.parameters.debug:
     891                                                self.logger.debug('TD: MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from))
     892
    890893                                        cc_list.remove(self.trac_smtp_from)
    891894
     
    915918                                        ticket[field] = value
    916919
    917                                 if self.DEBUG >= 10:
     920                                if self.parameters.debug:
    918921                                        s = 'ticket_field\t %s = %s' %(field,  ticket[field])
    919922                                        self.print_unicode(s)
     
    10041007                                fields = controller.get_ticket_changes(req, tkt, self.WORKFLOW)
    10051008
    1006                                 if self.DEBUG:
    1007                                         print 'TD: Workflow ticket update fields: ', fields
     1009                                if self.parameters.debug:
     1010                                        self.logger.debug('Workflow ticket update fields: ')
    10081011
    10091012                                for key in fields.keys():
     1013                                        if self.parameters.debug:
     1014                                                self.logger.debug('\t %s : %s' %(key, fields[key]))
     1015
    10101016                                        tkt[key] = fields[key]
    10111017
     
    10741080                                         value = options[int(value)]
    10751081       
    1076                         if self.DEBUG > 10:
     1082                        if self.parameters.debug:
    10771083                                s = 'TD: trac.ini name %s = %s' %(name, value)
    10781084                                self.print_unicode(s)
     
    10831089                        try:
    10841090                                value = self.parameters['%s_%s' %(prefix, name)]
    1085                                 if self.DEBUG > 10:
     1091                                if self.parameters.debug > 10:
    10861092                                        s = 'TD: email2trac.conf %s = %s ' %(name, value)
    10871093                                        self.print_unicode(s)
     
    10901096                                pass
    10911097               
    1092                         if self.DEBUG:
     1098                        if self.parameters.debug:
    10931099                                s = 'TD: user_dict[%s] = %s' %(name, value)
    10941100                                self.print_unicode(s)
     
    11241130                                orig_subject = result.group(3)
    11251131
    1126                                 if self.DEBUG:
    1127                                         print 'TD: subject search string: %s' %(orig_subject)
     1132                                if self.parameters.debug:
     1133                                        self.logger.debug('subject search string: %s' %(orig_subject))
    11281134
    11291135                                cursor = self.db.cursor()
     
    11371143
    11381144                                for summary in summaries:
    1139                                         if self.DEBUG:
    1140                                                 print 'TD: Looking for summary matching: "%s"' % summary
     1145                                        if self.parameters.debug:
     1146                                                self.logger.debug('Looking for summary matching: "%s"' % summary)
     1147
    11411148                                        sql = """SELECT id FROM ticket
    11421149                                                        WHERE changetime >= %s AND summary LIKE %s
     
    11461153                                        for row in cursor:
    11471154                                                (matched_id,) = row
    1148                                                 if self.DEBUG:
    1149                                                         print 'TD: Found matching ticket id: %d' % matched_id
     1155
     1156                                                if self.parameters.debug:
     1157                                                        self.logger.debug('Found matching ticket id: %d' % matched_id)
     1158
    11501159                                                break
    11511160
     
    11731182                if self.TICKET_PERMISSION_SYSTEM:
    11741183                        if not self.check_permission(tkt, 'TICKET_CREATE'):
    1175                                 print 'Reporter: %s has no permission to create tickets' %self.author
     1184                                self.logger.error('Reporter: %s has no permission to create tickets' %self.author)
    11761185                                return False
    11771186
     
    12101219                                self.update_ticket_fields(tkt, self.properties)
    12111220
    1212                 if self.DEBUG:
    1213                         print 'TD: self.get_message_parts ',
    1214                         print message_parts
    1215 
    12161221                message_parts = self.unique_attachment_names(message_parts)
    1217                 if self.DEBUG:
    1218                         print 'TD: self.unique_attachment_names',
    1219                         print message_parts
    12201222               
    12211223                if self.EMAIL_HEADER > 0:
     
    12311233                when = datetime.now(utc)
    12321234
    1233                 if not self.parameters.dry_run:
     1235                if self.parameters.dry_run:
     1236                        print 'DRY_RUN: tkt.insert()'
     1237                else:
    12341238                        self.id = tkt.insert()
    12351239       
     
    12631267
    12641268                if changed:
    1265                         if self.parameters.dry_run
     1269                        if self.parameters.dry_run:
    12661270                                print 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author)
    12671271                        else:
     
    14061410                from trac.util.datefmt import to_timestamp, utc
    14071411
    1408                 if self.DEBUG:
    1409                         print 'TD: Creating a new topic in forum:', self.id
     1412                if self.parameters.debug:
     1413                        self.logger.debug('Creating a new topic in forum:', self.id)
    14101414
    14111415                # Get dissussion API component.
     
    14161420                forum = api.get_forum(context, self.id)
    14171421
    1418                 if not forum and self.DEBUG:
    1419                         print 'ERROR: Replied forum doesn\'t exist'
     1422                if not forum and self.parameters.debug:
     1423                        self.logger.debug("ERROR: Replied forum doesn't exist")
    14201424
    14211425                # Prepare topic.
     
    14371441                from trac.util.datefmt import to_timestamp, utc
    14381442
    1439                 if self.DEBUG:
    1440                         print 'TD: Replying to discussion topic', self.id
     1443                if self.parameters.debug:
     1444                        self.logger.debug('Replying to discussion topic', self.id)
    14411445
    14421446                # Get dissussion API component.
     
    14471451                topic = api.get_topic(context, self.id)
    14481452
    1449                 if not topic and self.DEBUG:
    1450                         print 'ERROR: Replied topic doesn\'t exist'
     1453                if not topic and self.parameters.debug:
     1454                        self.logger.debug("ERROR: Replied topic doesn't exist")
    14511455
    14521456                # Prepare message.
     
    14681472                from trac.util.datefmt import to_timestamp, utc
    14691473
    1470                 if self.DEBUG:
    1471                         print 'TD: Replying to discussion message', self.id
     1474                if self.parameters.debug:
     1475                        self.loggger.debug('TD: Replying to discussion message', self.id)
    14721476
    14731477                # Get dissussion API component.
     
    14781482                message = api.get_message(context, self.id)
    14791483
    1480                 if not message and self.DEBUG:
    1481                         print 'ERROR: Replied message doesn\'t exist'
     1484                if not message and self.parameters.debug:
     1485                        self.logger.debug("ERROR: Replied message doesn't exist")
    14821486
    14831487                # Prepare message.
     
    16001604               
    16011605                if not m:
    1602                         if self.DEBUG:
    1603                                 print "TD: This is not a valid email message format"
     1606                        if self.parameters.debug:
     1607                                self.logger.debug('This is not a valid email message format')
     1608
    16041609                        return
    16051610                       
     
    16101615                        pass
    16111616
    1612                 if self.DEBUG > 1:        # save the entire e-mail message text
     1617                if self.parameters.debug:         # save the entire e-mail message text
    16131618                        self.save_email_for_debug(m, True)
    16141619
     
    16171622
    16181623                if not self.email_header_acl('white_list', self.email_addr, True):
    1619                         if self.DEBUG > 1 :
    1620                                 print 'Message rejected : %s not in white list' %(self.email_addr)
     1624                        if self.parameters.debug:
     1625                                self.logger.debug('Message rejected : %s not in white list' %(self.email_addr))
     1626
    16211627                        return False
    16221628
    16231629                if self.email_header_acl('black_list', self.email_addr, False):
    1624                         if self.DEBUG > 1 :
    1625                                 print 'Message rejected : %s in black list' %(self.email_addr)
     1630                        if self.parameters.debug:
     1631                                self.logger.debug('Message rejected : %s in black list' %(self.email_addr))
     1632
    16261633                        return False
    16271634
    16281635                if not self.email_header_acl('recipient_list', self.to_email_addr, True):
    1629                         if self.DEBUG > 1 :
    1630                                 print 'Message rejected : %s not in recipient list' %(self.to_email_addr)
     1636                        if self.parameters.debug:
     1637                                self.logger.debug('Message rejected : %s not in recipient list' %(self.to_email_addr))
     1638
    16311639                        return False
    16321640
     
    16461654                        subject  = self.email_to_unicode(m['Subject'])
    16471655
    1648                 if self.DEBUG:
    1649                          print "TD:", subject
     1656                if self.parameters.debug:
     1657                        self.logger.debug('subject: %s' %subject)
    16501658
    16511659                #
     
    18081816                Parse text if we use inline keywords to set ticket fields
    18091817                """
    1810                 if self.DEBUG:
    1811                         print 'TD: inline_properties function'
     1818                if self.parameters.debug:
     1819                        self.logger.info('function inline_properties')
    18121820
    18131821                properties = dict()
     
    18211829                                keyword, value = match.groups()
    18221830                                self.properties[keyword] = value.strip()
    1823                                 if self.DEBUG:
    1824                                         print "TD: inline properties: %s : %s" %(keyword,value)
     1831
     1832                                if self.parameters.debug:
     1833                                        self.logger.debug('inline properties: %s : %s' %(keyword,value))
    18251834                        else:
    18261835                                body.append(line)
     
    18741883
    18751884                for part in msg.walk():
    1876                         if self.DEBUG:
    1877                                 print 'TD: Message part: Main-Type: %s' % part.get_content_maintype()
    1878                                 print 'TD: Message part: Content-Type: %s' % part.get_content_type()
     1885                        if self.parameters.debug:
     1886                                self.logger.debug('Message part: Main-Type: %s' % part.get_content_maintype())
     1887                                self.logger.debug('Message part: Content-Type: %s' % part.get_content_type())
    18791888
    18801889                        ## Check content type
     
    18821891                        if part.get_content_type() in self.STRIP_CONTENT_TYPES:
    18831892
    1884                                 if self.DEBUG:
    1885                                         print "TD: A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename())
     1893                                if self.parameters.debug:
     1894                                        self.logger.debug("A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename()))
    18861895
    18871896                                continue
     
    18961905                        #
    18971906                        if part.get_content_maintype() == 'multipart':
    1898                                 if self.DEBUG:
    1899                                         print "TD: Skipping multipart container"
     1907                                if self.parameters.debug:
     1908                                        self.logger.debug("Skipping multipart container")
    19001909                                continue
    19011910                       
     
    19081917                        if ALTERNATIVE_MULTIPART and self.DROP_ALTERNATIVE_HTML_VERSION:
    19091918                                if part.get_content_type() == 'text/html':
    1910                                         if self.DEBUG:
    1911                                                 print "TD: Skipping alternative HTML message"
     1919                                        if self.parameters.debug:
     1920                                                self.logger.debug('Skipping alternative HTML message')
    19121921
    19131922                                        ALTERNATIVE_MULTIPART = False
     
    19171926                        #
    19181927                        if part.get_content_type() == 'text/plain' and inline:
    1919                                 if self.DEBUG:
    1920                                         print 'TD:               Inline body part'
     1928                                if self.parameters.debug:
     1929                                        self.logger.debug('               Inline body part')
    19211930
    19221931                                # Try to decode, if fails then do not decode
     
    19641973                                        message_parts.append('%s' %ubody_text)
    19651974                        else:
    1966                                 if self.DEBUG:
     1975                                if self.parameters.debug:
    19671976                                        s = 'TD:               Filename: %s' % part.get_filename()
    19681977                                        self.print_unicode(s)
     
    20372046                                unique_filename = "%s-%s%s" % (dummy_filename, num, ext)
    20382047                               
    2039                         if self.DEBUG:
     2048                        if self.parameters.debug:
    20402049                                s = 'TD: Attachment with filename %s will be saved as %s' % (filename, unique_filename)
    20412050                                self.print_unicode(s)
     
    20502059        def attachment_exists(self, filename):
    20512060
    2052                 if self.DEBUG:
     2061                if self.parameters.debug:
    20532062                        s = 'TD: attachment already exists: Id : %s, Filename : %s' %(self.id, filename)
    20542063                        self.print_unicode(s)
Note: See TracChangeset for help on using the changeset viewer.