VisionEgg.Deprecated

Package VisionEgg :: Module Deprecated |
[frames] | no frames]

# Source Code for Module VisionEgg.Deprecated

      1  # The Vision Egg: Deprecated
      2  #
      3  # Copyright (C) 2001-2003 Andrew Straw.
      4  # Copyright (C) 2008 California Institute of Technology
      5  #
      6  # URL: <http://www.visionegg.org/>
      7  #
      8  # Distributed under the terms of the GNU Lesser General Public License
      9  # (LGPL). See LICENSE.TXT that came with this file.
     10
     11  """
     12  Bits of code which will be removed in the future.
     13
     14  """
     15
     16  import VisionEgg
     17  import sys, os
     18  import logging                              # available in Python 2.3
     19
     20  ####################################################################
     21  #
     22  #        Error handling and assumption checking
     23  #
     24  ####################################################################
     25
     26 -class Message:
     27      """DEPRECATED Handles message/warning/error printing, exception raising."""
     28
     29      # Levels are:
     30      TRIVIAL = 0
     31      NAG = 1
     32      INFO = 2
     33      DEPRECATION = 3
     34      WARNING = 4
     35      ERROR = 5
     36      FATAL = 6
     37
     38 -    def __init__(self):
     39  ##        script_name = sys.argv[0]
     40  ##        if not script_name:
     41  ##            script_name = "(interactive shell)"
     42          self.pid = os.getpid()
     43  ##        self.logger.info("Script "+script_name+" started Vision Egg %s with process id %d."%(VisionEgg.release_name,self.pid))
     44
     45 -    def add(self,message,level=INFO,preserve_formatting=0,no_sys_stderr=0):
     46          level_translate = {
     47              # convert from old VisionEgg levels to new logging module levels
     48              Message.TRIVIAL     : logging.DEBUG,
     49              Message.NAG         : logging.DEBUG,
     50              Message.INFO        : logging.INFO,
     51              Message.DEPRECATION : logging.WARNING,
     52              Message.WARNING     : logging.WARNING,
     53              Message.ERROR       : logging.ERROR,
     54              Message.FATAL       : logging.CRITICAL,
     55              }
     56          new_level = level_translate[ level ]
     57          if not hasattr(self,"logger"):
     58              self.logger = logging.getLogger('VisionEgg.Deprecated')
     59          self.logger.log(new_level,message + '\n(sent using deprecated VisionEgg.Core.Message class)')
     60
     61 -    def format_string(self,in_str):
     62          # This probably a slow way to do things, but it works!
     63          min_line_length = 70
     64          in_list = in_str.split()
     65          out_str = ""
     66          cur_line = ""
     67          for word in in_list:
     68              cur_line = cur_line + word + " "
     69              if len(cur_line) > min_line_length:
     70                  out_str = out_str + cur_line[:-1] + "\n"
     71                  cur_line = "    "
     72          if cur_line.strip():
     73              # only add another newline if the last line done is non-empty
     74              out_str = out_str + cur_line + "\n"
     75          return out_str
     76
     77 -    def handle(self):
     78          if not self._sent_initial_newline:
     79              self.output_stream.write("\n",_no_sys_stderr=1)
     80              self.output_stream.flush()
     81              self._sent_initial_newline = 1
     82          while len(self.message_queue) > 0:
     83              my_str = ""
     84              level,text,preserve_formatting,date_str,no_sys_stderr = self.message_queue.pop(0)
     85              if level >= self.print_level:
     86                  my_str= my_str+date_str+" "
     87                  if self.pid:
     88                      my_str += "(%d) "%(self.pid,)
     89                  #my_str=my_str+self.prefix+" "
     90                  if level == Message.TRIVIAL:
     91                      my_str=my_str+"trivial"
     92                  elif level == Message.INFO:
     93                      my_str=my_str+"info"
     94                  elif level == Message.NAG:
     95                      my_str=my_str+"nag"
     96                  elif level == Message.DEPRECATION:
     97                      my_str=my_str+"DEPRECATION WARNING"
     98                  elif level == Message.WARNING:
     99                      my_str=my_str+"WARNING"
    100                  elif level == Message.ERROR:
    101                      my_str=my_str+"ERROR"
    102                  elif level == Message.FATAL:
    103                      my_str=my_str+"FATAL"
    104                  my_str += ": "
    105                  my_str += text
    106                  if not preserve_formatting:
    107                      my_str = self.format_string(my_str)
    108                  self.output_stream.write(my_str,_no_sys_stderr=no_sys_stderr)
    109                  self.output_stream.flush()
    110              if level >= self.exception_level:
    111                  raise RuntimeError(text)
    112              if level == Message.FATAL:
    113                  sys.exit(-1)
    114