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