Advertisement
Guest User

Logging?

a guest
Aug 20th, 2010
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. def log(self, what):
  2.         "Save a line in the log file."
  3.         if not self.log_file:
  4.             f = 'worklog.log'
  5.             self.log_file = \
  6.                  codecs.open(f, encoding='utf-8', mode="a")
  7.  
  8.         self.log_file.write(what+'\r\n')
  9.         self.log_file.flush()
  10.  
  11. # Pa onda jedna od metoda:
  12.  
  13. def set_encoding(self, in_enc):
  14.         """Sets the encoding.
  15.  
  16.        in_enc is passed from the user.
  17.        
  18.        """
  19.         # Encoding
  20.         # - if not specified, use default OS settings
  21.         self.log("Checking the encoding...")
  22.         if in_enc == None or in_enc.lower() == 'default':
  23.             if 'win' in sys.platform:
  24.                 self.ENC = 'utf-16'
  25.             else:
  26.                 self.ENC = 'utf-8'
  27.             mode = 'OS detection'
  28.         else:
  29.             self.ENC = in_enc
  30.             mode = 'user'
  31.            
  32.         self.log("Encoding was set to %s by %s." % (self.ENC, mode))
  33.        
  34.         # Check files for encoding and see if all is OK
  35.         try:
  36.             self.log('Attempting to load file with %s...' % self.ENC)
  37.             tmp = codecs.open(self.gloss_in, encoding = self.ENC).readlines()
  38.             self.log('OK. File loaded correctly. Encodind is %s.' % self.ENC)
  39.         except:
  40.             msg = "Error: Encoding was set to %s by %s, " % (mode, self.ENC)
  41.             msg = msg + 'but it did not load corretly.'
  42.             self.log(msg)
  43.             self.log('Inspecting the BOF...')
  44.             self.ENC = self.bof_check(self.gloss_in)
  45.             self.log('Discovered encoding is %s' % self.ENC)
  46.             if self.ENC == None:
  47.                 self.log('Encoding cannot be None. Is this a text file? Exiting.')
  48.                 sys.exit(0)
  49.             msg = ('Now it is detected by program and set to %s' % self.ENC)
  50.             self.log(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement