Package libxyz :: Package core :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.utils

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  import sys 
 18  import os 
 19  import termios 
 20  import copy 
 21  import types 
 22   
23 -def ustring(string, enc=None):
24 """ 25 Return unicode string 26 """ 27 28 if isinstance(string, unicode): 29 return string 30 31 if enc is None: 32 enc = xyzenc 33 34 if not isinstance(string, str): 35 return unicode(string) 36 37 # String 38 return string.decode(enc, 'replace')
39 40 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41
42 -def bstring(bstr, enc=None):
43 """ 44 Return encoded byte string 45 """ 46 47 if isinstance(bstr, str): 48 return bstr 49 50 if enc is None: 51 enc = xyzenc 52 53 if not isinstance(bstr, unicode): 54 return str(bstr) 55 56 return bstr.encode(enc, 'replace')
57 58 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 59
60 -def term_settings():
61 """ 62 Return current terminal settings 63 """ 64 65 stdin = sys.stdin.fileno() 66 67 # WTF? 68 if not os.isatty(stdin): 69 return None 70 71 return termios.tcgetattr(stdin)
72 73 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 74
75 -def setup_term():
76 """ 77 Terminal initialization 78 @return: Old terminal settings 79 """ 80 81 term = term_settings() 82 stdin = sys.stdin.fileno() 83 84 if term is None: 85 return None 86 87 try: 88 vdisable = os.fpathconf(stdin, "PC_VDISABLE") 89 except ValueError: 90 return 91 92 _saved_term = copy.deepcopy(term[-1]) 93 94 # Disable special symbols 95 _todisable = [getattr(termios, x) for x in ("VQUIT", # ^\ 96 "VINTR", # ^C 97 "VSUSP", # ^Z 98 "VLNEXT", # ^V 99 "VSTART", # ^Q 100 "VSTOP", # ^S 101 "VDISCARD", # ^O 102 )] 103 104 for _key in _todisable: 105 term[-1][_key] = vdisable 106 107 termios.tcsetattr(stdin, termios.TCSANOW, term) 108 109 return _saved_term
110 111 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 112
113 -def restore_term(term_data):
114 """ 115 Restore terminal settings 116 """ 117 118 stdin = sys.stdin.fileno() 119 120 term = term_settings() 121 122 if term is None: 123 return None 124 125 term[-1] = term_data 126 127 if os.isatty(stdin): 128 termios.tcsetattr(stdin, termios.TCSANOW, term)
129 130 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 131
132 -def is_func(obj):
133 """ 134 Check if object is of function type 135 """ 136 137 return isinstance(obj, types.FunctionType) or \ 138 isinstance(obj, types.MethodType)
139