예제 #1
0
    def log_message(self, jid, nick, msg, date=None, typ=1):
        """
        log the message in the appropriate jid's file
        type:
              0 = Don’t log
              1 = Message
              2 = Status/whatever
        """
        if not typ:
            return True

        jid = str(jid).replace('/', '\\')
        if not config.get_by_tabname('use_log', jid):
            return True
        if jid in self.fds.keys():
            fd = self.fds[jid]
        else:
            fd = self.check_and_create_log_dir(jid)
        if not fd:
            return True
        try:
            msg = clean_text(msg)
            if date is None:
                str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
            else:
                str_time = common.get_utc_time(date).strftime(
                    '%Y%m%dT%H:%M:%SZ')
            if typ == 1:
                prefix = 'MR'
            else:
                prefix = 'MI'
            lines = msg.split('\n')
            first_line = lines.pop(0)
            nb_lines = str(len(lines)).zfill(3)

            if nick:
                nick = '<' + nick + '>'
                fd.write(' '.join((prefix, str_time, nb_lines, nick,
                                   ' ' + first_line, '\n')))
            else:
                fd.write(' '.join(
                    (prefix, str_time, nb_lines, first_line, '\n')))
            for line in lines:
                fd.write(' %s\n' % line)
        except:
            log.error('Unable to write in the log file (%s)',
                      os.path.join(log_dir, jid),
                      exc_info=True)
            return False
        else:
            try:
                fd.flush()  # TODO do something better here?
            except:
                log.error('Unable to flush the log file (%s)',
                          os.path.join(log_dir, jid),
                          exc_info=True)
                return False
        return True
예제 #2
0
파일: logger.py 프로젝트: Perdu/poezio
    def log_message(self, jid, nick, msg, date=None, typ=1):
        """
        log the message in the appropriate jid's file
        type:
              0 = Don’t log
              1 = Message
              2 = Status/whatever
        """
        if not typ:
            return True

        jid = str(jid).replace('/', '\\')
        if not config.get_by_tabname('use_log', jid):
            return True
        if jid in self.fds.keys():
            fd = self.fds[jid]
        else:
            fd = self.check_and_create_log_dir(jid)
        if not fd:
            return True
        try:
            msg = clean_text(msg)
            if date is None:
                str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
            else:
                str_time = common.get_utc_time(date).strftime('%Y%m%dT%H:%M:%SZ')
            if typ == 1:
                prefix = 'MR'
            else:
                prefix = 'MI'
            lines = msg.split('\n')
            first_line = lines.pop(0)
            nb_lines = str(len(lines)).zfill(3)

            if nick:
                nick = '<' + nick + '>'
                fd.write(' '.join((prefix, str_time, nb_lines, nick, ' '+first_line, '\n')))
            else:
                fd.write(' '.join((prefix, str_time, nb_lines, first_line, '\n')))
            for line in lines:
                fd.write(' %s\n' % line)
        except:
            log.error('Unable to write in the log file (%s)',
                    os.path.join(log_dir, jid),
                    exc_info=True)
            return False
        else:
            try:
                fd.flush()          # TODO do something better here?
            except:
                log.error('Unable to flush the log file (%s)',
                        os.path.join(log_dir, jid),
                        exc_info=True)
                return False
        return True
예제 #3
0
파일: logger.py 프로젝트: Perdu/poezio
 def log_roster_change(self, jid, message):
     """
     Log a roster change
     """
     if not config.get_by_tabname('use_log', jid):
         return True
     self.check_and_create_log_dir('', open_fd=False)
     if not self.roster_logfile:
         try:
             self.roster_logfile = open(os.path.join(log_dir, 'roster.log'), 'a')
         except IOError:
             log.error('Unable to create the log file (%s)',
                     os.path.join(log_dir, 'roster.log'),
                     exc_info=True)
             return False
     try:
         str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
         message = clean_text(message)
         lines = message.split('\n')
         first_line = lines.pop(0)
         nb_lines = str(len(lines)).zfill(3)
         self.roster_logfile.write('MI %s %s %s %s\n' % (str_time, nb_lines, jid, first_line))
         for line in lines:
             self.roster_logfile.write(' %s\n' % line)
         self.roster_logfile.flush()
     except:
         log.error('Unable to write in the log file (%s)',
                 os.path.join(log_dir, 'roster.log'),
                 exc_info=True)
         return False
     return True
예제 #4
0
 def log_roster_change(self, jid, message):
     """
     Log a roster change
     """
     if not config.get_by_tabname('use_log', jid):
         return True
     self.check_and_create_log_dir('', open_fd=False)
     if not self.roster_logfile:
         try:
             self.roster_logfile = open(os.path.join(log_dir, 'roster.log'),
                                        'a')
         except IOError:
             log.error('Unable to create the log file (%s)',
                       os.path.join(log_dir, 'roster.log'),
                       exc_info=True)
             return False
     try:
         str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
         message = clean_text(message)
         lines = message.split('\n')
         first_line = lines.pop(0)
         nb_lines = str(len(lines)).zfill(3)
         self.roster_logfile.write('MI %s %s %s %s\n' %
                                   (str_time, nb_lines, jid, first_line))
         for line in lines:
             self.roster_logfile.write(' %s\n' % line)
         self.roster_logfile.flush()
     except:
         log.error('Unable to write in the log file (%s)',
                   os.path.join(log_dir, 'roster.log'),
                   exc_info=True)
         return False
     return True
예제 #5
0
def test_utc_time():
    delta = timedelta(seconds=-3600)
    d = datetime.datetime.now()
    time.timezone = -3600
    time.altzone = -3600
    assert get_utc_time(local_time=d) == d + delta
예제 #6
0
def test_utc_time():
    delta = timedelta(seconds=-3600)
    d = datetime.datetime.now()
    time.timezone = -3600; time.altzone = -3600
    assert get_utc_time(local_time=d) == d + delta