Module iof
[hide private]
[frames] | no frames]

Source Code for Module iof

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #-------------------------------------------------------------------- 
  4  # Filename: __main__.py 
  5  # Author: Mazhugin Aleksey 
  6  # Created: 2019/07/25 
  7  # ID: $Id: iof.py 63 2019-09-09 15:31:40Z Aleksey $ 
  8  # URL: $URL: file:///D:/svn_/pybag/trunc/src/iof.py $ 
  9  # Copyright: Copyright (c) 2019, Mazhugin Aleksey. 
 10  # License: BSD 
 11  #-------------------------------------------------------------------- 
 12   
 13   
 14  """ 
 15  IO functions. 
 16  """ 
 17   
 18  #__docformat__ = "restructuredtext" # "epytext" 
 19   
 20  # Importing 
 21  import sys 
 22  import codecs 
 23  import locale 
 24  import traceback 
 25  import os 
 26  import threading 
 27   
 28  import cfg 
 29   
 30   
 31   
 32  # Define debug and log functions. 
 33  # All log and debug messages written into file "./pybag.log". 
 34   
35 -def z_u(a, enc=None):
36 """ 37 Convert L{a} to unicode if C{a} is not unicode. 38 Use default locale encoding if not given L{enc}. 39 """ 40 if not enc: 41 enc = cfg.z_epdl 42 if type(a) == str: 43 a = unicode(a, enc, u'replace') 44 elif type(a) != unicode: 45 try: 46 a = unicode(a) 47 except Exception,exc: # @UnusedVariable 48 try: 49 a = unicode(str(a), enc, u'replace') 50 except: 51 a = u'ERROR then convert to unicode in z_u().' 52 return a
53
54 -def log(*arg):
55 """ 56 Print all concatenated L{z_u}(arg) if L{cfg.PYBAG_LOG} True. 57 58 @param arg: Object to output. All objects convert to string I{str()} function. 59 @type arg: *object 60 @return: None. 61 @rtype: None 62 """ 63 if cfg.PYBAG_LOG or cfg.PYBAG_DEBUG: 64 log_writeln(u'# ', *arg)
65
66 -def dbg(*arg):
67 """ 68 Print all concatenated L{z_u}(arg) if L{cfg.PYBAG_DEBUG} True. 69 70 @param arg: Object to output. All objects convert to string I{str()} function. 71 @type arg: *object 72 @return: None. 73 @rtype: None 74 """ 75 if cfg.PYBAG_DEBUG: 76 log_writeln(u'-->', *arg)
77
78 -def log_writeln(*arg):
79 """ 80 Used only in first call, and replace themself by L{log_writeln_1} if 81 L{log_init} pass, or L{log_writeln_0} otherwise. 82 """ 83 _module = sys.modules[__name__] 84 try: 85 log_init() 86 _module.log_writeln = log_writeln_1 87 except: 88 _module.log_writeln = log_writeln_0 89 _module.log_writeln(*arg)
90 91 _loglock = threading.Lock() 92 """ 93 Lock object for threading output. 94 """ 95
96 -def log_writeln_1(*arg):
97 """ 98 Main writeln function for logging. 99 Write concatenated arguments into outstream and append new line ('\\n'). 100 101 @param arg: Object to output. All objects convert to string L{z_u()} function. 102 @type arg: *object 103 @return: None. 104 @rtype: None 105 """ 106 _u_ = z_u 107 def red(a,b): 108 return a + _u_(b)
109 try: 110 ##log_init() 111 _loglock.acquire() 112 cfg.fstream.write( reduce(red, arg, u'') ) 113 cfg.fstream.write(u'\n' ) 114 except Exception, exc: # @UnusedVariable 115 # Quiet for safe. 116 pass 117 finally: 118 _loglock.release() 119
120 -def log_writeln_0(*arg):
121 """ 122 Used if L{log_init} was fail in L{log_writeln} and do nothing. 123 """ 124 pass
125
126 -def log_init():
127 """ 128 Initialize file output stream. 129 130 Default is "pybag.log". 131 132 """ 133 from cli import dir_root 134 135 if not cfg.fstream: 136 try: 137 p=os.path.join(dir_root, u'pybag.log') 138 cfg.fstream=codecs.open(p,'w', 'utf_8', 'strict', 0) 139 140 except Exception, e: 141 cfg.fstream=sys.stderr 142 err=u'Can\'t open file for debug logging.\nerror:' + \ 143 unicode(e) + u'\n\n' 144 # Multithread not safe 145 cfg.fstream.write(err)
146
147 -def log_clear():
148 """ 149 Do clean up for fstream: close if need. 150 151 """ 152 if cfg.fstream and cfg.fstream != sys.stderr: 153 cfg.fstream.close()
154
155 -def prt(*arg, **kwd):
156 """ 157 Print all C{arg} consecutively with C{print} builtin function. 158 Also L{log} all output. 159 160 @keyword verbose: Verbosity level. {default VERBOSE == 3}. 161 @keyword place: Function name from called print. 162 @keyword error: Error description or exception. 163 """ 164 _u_ = z_u 165 def red(a,b): 166 return a + _u_(b)
167 try: 168 verb = cfg.VERBOSE 169 if kwd.has_key('verbose'): 170 verb = kwd['verbose'] 171 s = reduce(red, arg, u'') 172 sl = None 173 if kwd.has_key('error'): 174 tbs = traceback.format_exc() 175 s += u'\n\tERROR: %s' % (_u_(kwd['error']),) 176 sl = u'\n\tTRACEBACK: %s\n' % (_u_(tbs),) 177 se = s.encode(cfg.z_outep, u'strict') 178 if kwd.has_key('place'): 179 log(u'%d>%s: ' % (verb,kwd['place']), s) 180 else: 181 log(u'%d>' % (verb,), s) 182 if sl: 183 log(sl) # Log traceback. 184 if verb <= cfg.VERBOSE: 185 print se 186 except: 187 pass 188
189 -def prt0(*arg, **kwd):
190 """ 191 Print arguments L{arg} with L{prt} with C{verbose = 0}. 192 """ 193 kwd.update({'verbose':0}) 194 prt(*arg, **kwd)
195
196 -def prt1(*arg, **kwd):
197 """ 198 Print arguments L{arg} with L{prt} with C{verbose = 1}. 199 """ 200 kwd.update({'verbose':1}) 201 prt(*arg, **kwd)
202
203 -def prt2(*arg, **kwd):
204 """ 205 Print arguments L{arg} with L{prt} with C{verbose = 2}. 206 """ 207 kwd.update({'verbose':2}) 208 prt(*arg, **kwd)
209
210 -def prt3(*arg, **kwd):
211 """ 212 Print arguments L{arg} with L{prt} with C{verbose = 3}. 213 """ 214 kwd.update({'verbose':3}) 215 prt(*arg, **kwd)
216
217 -def prt4(*arg, **kwd):
218 """ 219 Print arguments L{arg} with L{prt} with C{verbose = 4}. 220 """ 221 kwd.update({'verbose':4}) 222 prt(*arg, **kwd)
223
224 -def prt5(*arg, **kwd):
225 """ 226 Print arguments L{arg} with L{prt} with C{verbose = 5}. 227 """ 228 kwd.update({'verbose':5}) 229 prt(*arg, **kwd)
230 231
232 -def init_encoding():
233 """ 234 Get system enconings. 235 """ 236 237 (_,cfg.z_epdl) = locale.getdefaultlocale() 238 if not cfg.z_epdl: 239 prt3(u'<y>WARNING: Can not obtain default locale. Used "ascii" encoding.</y>') 240 cfg.z_epdl = 'ascii' 241 242 cfg.z_epde = sys.getdefaultencoding() 243 if not cfg.z_epde: 244 prt3(u'<y>WARNING: Can not obtain default encoding. Used default locale encoding.</y>') 245 cfg.z_epde = cfg.z_epdl 246 247 cfg.z_epse = sys.getfilesystemencoding() 248 if not cfg.z_epse: 249 prt3(u'<y>WARNING: Can not obtain default system encoding. Used default encoding.</y>') 250 cfg.z_epse = cfg.z_epde 251 252 cfg.Z_INEP = None 253 if hasattr(sys.stdin, u'encoding'): 254 cfg.Z_INEP = sys.stdin.encoding 255 cfg.Z_INEP = cfg.Z_INEP or cfg.z_epdl 256 257 cfg.z_outep = None 258 if hasattr(sys.stdout, u'encoding'): 259 cfg.z_outep = sys.stdout.encoding 260 cfg.z_outep = cfg.z_outep or cfg.z_epdl 261 262 #prt4(u'') # First logging initialization. This string will not be out. 263 prt4(u'Encoding for default locale is ', cfg.z_epdl) 264 prt4(u'Encoding for default encoding is ', cfg.z_epde) 265 prt4(u'Encoding for default file system encoding is ', cfg.z_epse) 266 prt4(u'Encoding for std input is ', cfg.Z_INEP) 267 prt4(u'Encoding for std output is ', cfg.z_outep)
268 269 # For Windows console. 270 # -------------------- 271 # Encoding for default locale is cp1251 272 # Encoding for default encoding is ascii 273 # Encoding for default system encoding is mbcs 274 # Encoding for input is cp866 275 # Encoding for output is cp866 276