Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

211 lignes
4.5 KiB

il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. import errno
  5. import os
  6. import platform
  7. import re
  8. import shutil
  9. import sys
  10. import unicodedata
  11. from itertools import islice
  12. if sys.version_info[0] == 3:
  13. imap = map
  14. os.getcwdu = os.getcwd
  15. else:
  16. from itertools import imap
  17. def create_dir(path):
  18. """Creates a directory atomically."""
  19. try:
  20. os.makedirs(path)
  21. except OSError as exception:
  22. if exception.errno != errno.EEXIST:
  23. raise
  24. def encode_local(string):
  25. """Converts string into user's preferred encoding."""
  26. if is_python3():
  27. return string
  28. return string.encode(sys.getfilesystemencoding() or 'utf-8')
  29. def first(xs):
  30. it = iter(xs)
  31. try:
  32. if is_python3():
  33. return it.__next__()
  34. return it.next()
  35. except StopIteration:
  36. return None
  37. def get_tab_entry_info(entry, separator):
  38. """
  39. Given a tab entry in the following format return needle, index, and path:
  40. [needle]__[index]__[path]
  41. """
  42. needle, index, path = None, None, None
  43. match_needle = re.search(r'(.*?)' + separator, entry)
  44. match_index = re.search(separator + r'([0-9]{1})', entry)
  45. match_path = re.search(
  46. separator + r'[0-9]{1}' + separator + r'(.*)',
  47. entry,
  48. )
  49. if match_needle:
  50. needle = match_needle.group(1)
  51. if match_index:
  52. index = int(match_index.group(1))
  53. if match_path:
  54. path = match_path.group(1)
  55. return needle, index, path
  56. def get_pwd():
  57. try:
  58. return os.getcwdu()
  59. except OSError:
  60. print('Current directory no longer exists.', file=sys.stderr)
  61. raise
  62. def has_uppercase(string):
  63. if is_python3():
  64. return any(ch.isupper() for ch in string)
  65. return any(unicodedata.category(c) == 'Lu' for c in unicode(string))
  66. def in_bash():
  67. return 'bash' in os.getenv('SHELL')
  68. def is_autojump_sourced():
  69. return '1' == os.getenv('AUTOJUMP_SOURCED')
  70. def is_python2():
  71. return sys.version_info[0] == 2
  72. def is_python3():
  73. return sys.version_info[0] == 3
  74. def is_linux():
  75. return platform.system() == 'Linux'
  76. def is_osx():
  77. return platform.system() == 'Darwin'
  78. def is_windows():
  79. return platform.system() == 'Windows'
  80. def last(xs):
  81. it = iter(xs)
  82. tmp = None
  83. try:
  84. if is_python3():
  85. while True:
  86. tmp = it.__next__()
  87. else:
  88. while True:
  89. tmp = it.next()
  90. except StopIteration:
  91. return tmp
  92. def move_file(src, dst):
  93. """
  94. Atomically move file.
  95. Windows does not allow for atomic file renaming (which is used by
  96. os.rename / shutil.move) so destination paths must first be deleted.
  97. """
  98. if is_windows() and os.path.exists(dst):
  99. # raises exception if file is in use on Windows
  100. os.remove(dst)
  101. shutil.move(src, dst)
  102. def print_entry(entry):
  103. print_local('%.1f:\t%s' % (entry.weight, entry.path))
  104. def print_local(string):
  105. print(encode_local(string))
  106. def print_tab_menu(needle, tab_entries, separator):
  107. """
  108. Prints the tab completion menu according to the following format:
  109. [needle]__[index]__[possible_match]
  110. The needle (search pattern) and index are necessary to recreate the results
  111. on subsequent calls.
  112. """
  113. for i, entry in enumerate(tab_entries):
  114. print_local(
  115. '%s%s%d%s%s' % (
  116. needle,
  117. separator,
  118. i + 1,
  119. separator,
  120. entry.path,
  121. ),
  122. )
  123. def sanitize(directories):
  124. # edge case to allow '/' as a valid path
  125. clean = lambda x: unico(x) if x == os.sep else unico(x).rstrip(os.sep)
  126. return list(imap(clean, directories))
  127. def second(xs):
  128. it = iter(xs)
  129. try:
  130. if is_python2():
  131. it.next()
  132. return it.next()
  133. elif is_python3():
  134. next(it)
  135. return next(it)
  136. except StopIteration:
  137. return None
  138. def surround_quotes(string):
  139. """
  140. Bash has problems dealing with certain paths so we're surrounding all
  141. path outputs with quotes.
  142. """
  143. if in_bash() and string:
  144. # Python 2.6 requres field numbers
  145. return '"{0}"'.format(string)
  146. return string
  147. def take(n, iterable):
  148. """Return first n items of an iterable."""
  149. return islice(iterable, n)
  150. def unico(string):
  151. """Converts into Unicode string."""
  152. if is_python2() and not isinstance(string, unicode):
  153. return unicode(string, encoding='utf-8', errors='replace')
  154. return string