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  from libxyz.parser import Lexer 
 24  from libxyz.exceptions import XYZValueError 
 25   
26 -def ustring(string, enc=None):
27 """ 28 Return unicode string 29 """ 30 31 if isinstance(string, unicode): 32 return string 33 34 if enc is None: 35 enc = xyzenc 36 37 if not isinstance(string, str): 38 return unicode(string) 39 40 # String 41 return string.decode(enc, 'replace')
42 43 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44
45 -def bstring(bstr, enc=None):
46 """ 47 Return encoded byte string 48 """ 49 50 if isinstance(bstr, str): 51 return bstr 52 53 if enc is None: 54 enc = xyzenc 55 56 if not isinstance(bstr, unicode): 57 return str(bstr) 58 59 return bstr.encode(enc, 'replace')
60 61 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 62
63 -def term_settings():
64 """ 65 Return current terminal settings 66 """ 67 68 stdin = sys.stdin.fileno() 69 70 # WTF? 71 if not os.isatty(stdin): 72 return None 73 74 return termios.tcgetattr(stdin)
75 76 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 77
78 -def setup_term():
79 """ 80 Terminal initialization 81 @return: Old terminal settings 82 """ 83 84 term = term_settings() 85 stdin = sys.stdin.fileno() 86 87 if term is None: 88 return None 89 90 try: 91 vdisable = os.fpathconf(stdin, "PC_VDISABLE") 92 except ValueError: 93 return 94 95 _saved_term = copy.deepcopy(term[-1]) 96 97 # Disable special symbols 98 _todisable = [getattr(termios, x) for x in ("VQUIT", # ^\ 99 "VINTR", # ^C 100 "VSUSP", # ^Z 101 "VLNEXT", # ^V 102 "VSTART", # ^Q 103 "VSTOP", # ^S 104 "VDISCARD", # ^O 105 )] 106 107 for _key in _todisable: 108 term[-1][_key] = vdisable 109 110 termios.tcsetattr(stdin, termios.TCSANOW, term) 111 112 return _saved_term
113 114 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 115
116 -def restore_term(term_data):
117 """ 118 Restore terminal settings 119 """ 120 121 stdin = sys.stdin.fileno() 122 123 term = term_settings() 124 125 if term is None: 126 return None 127 128 term[-1] = term_data 129 130 if os.isatty(stdin): 131 termios.tcsetattr(stdin, termios.TCSANOW, term)
132 133 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 134
135 -def is_func(obj):
136 """ 137 Check if object is of function type 138 """ 139 140 return isinstance(obj, types.FunctionType) or \ 141 isinstance(obj, types.MethodType)
142 143 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 144
145 -def intersect(s1, s2):
146 """ 147 Find intersection between two strings 148 """ 149 150 s1 = ustring(s1) 151 s2 = ustring(s2) 152 153 for index in range(len(s2) - 1, 0, -1): 154 if s1.endswith(s2[:index]): 155 return s2[index:] 156 157 return s2
158 159 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 160
161 -def split_cmd(cmd):
162 """ 163 Split command line 164 """ 165 166 lexer = Lexer(ustring(cmd), [], comment=None, macro=None) 167 lexer.escaping_on() 168 169 data = [] 170 171 while True: 172 res = lexer.lexer() 173 174 if res is None: 175 break 176 else: 177 __, val = res 178 data.append(val) 179 180 return data
181 182 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 183
184 -def typer(*types, **kw):
185 msg = kw.get('msg', (u"Invalid argument type %s")) 186 error = kw.get('error', XYZValueError) 187 188 def _wrap1(fun): 189 def _wrap2(*args, **kwargs): 190 for t, arg in zip(types, args): 191 if t is not None and not isinstance(arg, t): 192 raise error(msg % str(type(arg))) 193 194 return fun(*args, **kwargs)
195 196 return _wrap2 197 198 return _wrap1 199