Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:48

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 level.py
0023 ========================================
0024 """
0025 
0026 import os, sys, logging
0027 from opticks.ana.log import fatal_, error_, warning_, info_, debug_
0028 from opticks.ana.log import underline_, blink_ 
0029 log = logging.getLogger(__name__)
0030 
0031 class Level(object):
0032     FATAL = 20
0033     ERROR = 10
0034     WARNING = 0 
0035     INFO = -10
0036     DEBUG = -20
0037 
0038     MISSING = -30   ## expedient whilst debugging missing levels
0039 
0040     level2name = { FATAL:"FATAL", ERROR:"ERROR", WARNING:"WARNING", INFO:"INFO", DEBUG:"DEBUG", MISSING:"MISSING" }
0041     name2level = { "FATAL":FATAL, "ERROR":ERROR, "WARNING":WARNING, "INFO":INFO, "DEBUG":DEBUG, "MISSING":MISSING  }
0042     level2func = { FATAL:fatal_, ERROR:error_, WARNING:warning_, INFO:info_, DEBUG:debug_, MISSING:fatal_ }
0043 
0044 
0045     @classmethod
0046     def FromName(cls, name):
0047         if name is None:
0048             name = "MISSING"
0049         pass     
0050         level = cls.name2level[name] 
0051         return cls(name, level) 
0052     @classmethod
0053     def FromLevel(cls, level):
0054         if level is None:
0055             level = MISSING
0056         pass
0057         name = cls.level2name[level] 
0058         return cls(name, level) 
0059 
0060     def __init__(self, name, level):
0061         self.name = name
0062         self.nam = "_%s_" % name if name == "FATAL" else name    # hack for alignment in %16s columns
0063         self.level = level
0064         self.fn_ = self.level2func[level]
0065 
0066 
0067 if __name__ == '__main__':
0068     pass
0069     for nam in "FATAL ERROR WARNING INFO DEBUG".split():
0070         lev = Level.FromName(nam)
0071         if nam == "FATAL": nam = "_" + nam + "_"   # the undescores are a hack that succeeds to get columns to align 
0072         fmt = " %4d : %8s : %16s : %4d "  
0073         print(fmt % ( lev.level, lev.name, lev.fn_(nam), lev.level ))  
0074 
0075     # for levels other than fatal , just padding by 9 extra chars gets things to line up 
0076 
0077     for nam in "FATAL ERROR WARNING INFO DEBUG".split():
0078         lev = Level.FromName(nam)
0079 
0080         if nam is "FATAL": nam = nam + " "
0081 
0082         for w in range(8,30):
0083             fmt = " %4d : %" + str(w) + "s : "
0084             print( fmt % ( w, lev.fn_(nam))  ) 
0085  
0086 
0087 
0088