So I'm new to doing creating classes in python and took a script I had
written to parse a log file and tried to create a class just to understand
it a little better. The first script below works fine.

It looks at the log, checks the date and if it's too old, raises the
exception and returns a set value or if the file doesn't exist it raises the
exception.  If the file is there and is no more than a day old, it parses
the log and returns what was backed up by the utility I'm using.

The Second script written here, always raises the exception and I'm missing
why, any advice?

*FIRST SCRIPT*

import re, os
import stat
import sys
from datetime import date, timedelta

log_file = 'd:/backup_logs/backups.log'
re_backup_status = re.compile(r'^\s+Files\s+:\s+\d', re.IGNORECASE)
"""Example of return from regexFiles:  Succeeded"168933 failed:0 """

def main():

  try:
    s = os.stat(log_file)
    mtime = date.fromtimestamp(s[stat.ST_MTIME])
    yesterday = date.today() - timedelta(days=1)
    mode = s[stat.ST_MODE]


    if stat.S_ISREG(mode):
      if mtime < yesterday:
        print "%s" % ('succeeded:0 failed:10000')
      else:
        log1 = (open(log_file, 'r'))
        for line in log1:
          if re_backup_status.search(line):
            status_out = line.split()
            print '%s succeeded:%s failed:%s'  % (
                  'map:files', status_out[2], status_out[6])
        log1.close()
  except:  print "%s" %('succeeded:0 failed:10000')

if __name__ == '__main__':
  main()
*
*
*SECOND SCRIPT*
*
*
import re
import os
import stat
import sys
from datetime import date, timedelta

class Log_Parser:
    def __init__(self):
        self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
re.IGNORECASE)
        """Example of return from regexFiles:  Succeeded"168933 failed:0 """

    def log_parse(self, log_file):

      try:
        s = os.stat(log_file)
        mtime = date.fromtimestamp(s[stat.ST_MTIME])
        yesterday = date.today() - timedelta(days=1)
        mode = s[stat.ST_MODE]

        if stat.S_ISREG(mode):
          print ('file is there')
          if mtime < yesterday:
            print('file is the wrong date')
            print "%s" % ('succeeded:0 failed:10000')
          else:
            log1 = (open(log_file, 'r'))
            for line in log1:
              if re_backup_status.search(line):
                status_out = line.split()
                print '%s succeeded:%s failed:%s'  % (
                      'map:files', status_out[2], status_out[6])
        log1.close()
      except:  print "%s" %('succeeded:0 failed:10000')


backup_log_parser = Log_Parser()
backup_log_parser.log_parse('d:/backup_logs/backups.log')
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to