1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/bin/utils.py

141 lines
2.8 KiB
Python
Raw Normal View History

2013-12-16 17:20:40 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
2013-12-16 17:20:40 +00:00
from collections import Iterable
import errno
from itertools import islice
import os
2013-12-16 17:20:40 +00:00
import platform
import shutil
2013-12-16 17:20:40 +00:00
import sys
2013-12-17 16:35:43 +00:00
import unicodedata
2013-12-16 17:20:40 +00:00
def create_dir(path):
"""Creates a directory atomically."""
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
2013-12-16 21:39:49 +00:00
def decode(string):
"""Converts byte string to Unicode string."""
if is_python2():
return string.decode('utf-8', errors='replace')
return string
def encode(string):
"""Converts Unicode string to byte string."""
if is_python2():
return string.encode('utf-8', errors='replace')
return string
def encode_local(string, encoding=None):
"""Converts string into local filesystem encoding."""
if is_python2():
return decode(string).encode(encoding or sys.getfilesystemencoding())
return string
def first(xs):
it = iter(xs)
try:
return it.next()
except StopIteration:
return None
2013-12-17 20:48:12 +00:00
def get_pwd():
try:
return os.getcwdu()
except OSError:
print("Current directory no longer exists.", file=sys.stderr)
sys.exit(1)
2013-12-17 16:35:43 +00:00
def has_uppercase(string):
return any(unicodedata.category(c) == 'Lu' for c in unicode(string))
2013-12-17 22:06:27 +00:00
def in_bash():
return 'bash' in os.getenv('SHELL')
2013-12-16 21:39:49 +00:00
def is_python2():
return sys.version_info[0] == 2
2013-12-16 17:20:40 +00:00
def is_linux():
return platform.system() == 'Linux'
def is_osx():
return platform.system() == 'Darwin'
def is_windows():
return platform.system() == 'Windows'
2013-12-17 21:57:36 +00:00
def last(xs):
it = iter(xs)
tmp = None
try:
while True:
tmp = it.next()
except StopIteration:
return tmp
def move_file(src, dst):
"""
Atomically move file.
Windows does not allow for atomic file renaming (which is used by
os.rename / shutil.move) so destination paths must first be deleted.
"""
if is_windows() and os.path.exists(dst):
# raises exception if file is in use on Windows
os.remove(dst)
shutil.move(src, dst)
2013-12-16 22:54:27 +00:00
2013-12-17 20:48:12 +00:00
def print_entry(entry):
print(encode_local("%.1f:\t%s" % (entry.weight, entry.path)))
2013-12-17 22:46:01 +00:00
def print_tab_completion_menu(separator, entries):
for i, entry in enumerate(entries):
print(encode_local(surround_quotes(
'%s%d%s%s' % (
separator,
i,
separator,
entry.path))))
def second(xs):
it = iter(xs)
try:
it.next()
return it.next()
except StopIteration:
return None
2013-12-17 22:06:27 +00:00
def surround_quotes(string):
if in_bash():
return '"{}"'.format(string)
return string
def take(n, iterable):
"""Return first n items of an iterable."""
return islice(iterable, n)