-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathutil.py
36 lines (32 loc) · 901 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from os import devnull
import subprocess
def encode(s):
try:
return s.encode('utf-8')
except (AttributeError, SyntaxError):
pass
return s
# In Python 3, all strings are sequences of Unicode characters.
# There is a bytes type that holds raw bytes.
# In Python 2, a string may be of type str or of type unicode.
def decode(s):
try:
return s.decode('utf-8')
except (AttributeError, SyntaxError, UnicodeEncodeError):
pass
return s
def is_command_valid(command):
"""
Checks if command is recognized on machine. Used to determine installations
of 'less' pager.
"""
if not command:
return False
try:
# call command silentyly
with open(devnull, 'wb') as no_out:
subprocess.call(command, stdout=no_out, stderr=no_out)
except OSError:
return False
else:
return True