Completed
Push — dev-4.1-unstable ( b6e12b...aed85b )
by Felipe A.
01:04
created

isnonstriterable()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
import os
5
import os.path
6
import sys
7
import itertools
8
9
PY_LEGACY = sys.version_info < (3, )
10
ENV_PATH = []  # populated later
11
12
try:
13
    from scandir import scandir, walk
14
except ImportError:
15
    if not hasattr(os, 'scandir'):
16
        raise
17
    scandir = os.scandir
18
    walk = os.walk
19
20
fs_encoding = sys.getfilesystemencoding()
21
22
23
def isexec(path):
24
    '''
25
    Check if given path points to an executable file.
26
27
    :param path: file path
28
    :type path: str
29
    :return: True if executable, False otherwise
30
    :rtype: bool
31
    '''
32
    return os.path.isfile(path) and os.access(path, os.X_OK)
33
34
35
def which(name,
36
          env_path=ENV_PATH,
37
          is_executable_fnc=isexec,
38
          path_join_fnc=os.path.join):
39
    '''
40
    Get command absolute path.
41
42
    :param name: name of executable command
43
    :type name: str
44
    :param env_path: OS environment executable paths, defaults to autodetected
45
    :type env_path: list of str
46
    :param is_executable_fnc: callable will be used to detect if path is
47
                              executable, defaults to `isexec`
48
    :type is_executable_fnc: Callable
49
    :param path_join_fnc: callable will be used to join path components
50
    :type path_join_fnc: Callable
51
    :return: absolute path
52
    :rtype: str or None
53
    '''
54
    for path in env_path:
55
        exe_file = path_join_fnc(path, name)
56
        if is_executable_fnc(exe_file):
57
            return exe_file
58
    return None
59
60
61
def fsdecode(path, os_name=os.name, fs_encoding=fs_encoding, errors=None):
62
    '''
63
    Decode given path.
64
65
    :param path: path will be decoded if using bytes
66
    :type path: bytes or str
67
    :param os_name: operative system name, defaults to os.name
68
    :type os_name: str
69
    :param fs_encoding: current filesystem encoding, defaults to autodetected
70
    :type fs_encoding: str
71
    :return: decoded path
72
    :rtype: str
73
    '''
74
    if not isinstance(path, bytes):
75
        return path
76
    if not errors:
77
        use_strict = PY_LEGACY or os_name == 'nt'
78
        errors = 'strict' if use_strict else 'surrogateescape'
79
    return path.decode(fs_encoding, errors=errors)
80
81
82
def fsencode(path, os_name=os.name, fs_encoding=fs_encoding, errors=None):
83
    '''
84
    Encode given path.
85
86
    :param path: path will be encoded if not using bytes
87
    :type path: bytes or str
88
    :param os_name: operative system name, defaults to os.name
89
    :type os_name: str
90
    :param fs_encoding: current filesystem encoding, defaults to autodetected
91
    :type fs_encoding: str
92
    :return: encoded path
93
    :rtype: bytes
94
    '''
95
    if isinstance(path, bytes):
96
        return path
97
    if not errors:
98
        use_strict = PY_LEGACY or os_name == 'nt'
99
        errors = 'strict' if use_strict else 'surrogateescape'
100
    return path.encode(fs_encoding, errors=errors)
101
102
103
def getcwd(fs_encoding=fs_encoding, cwd_fnc=os.getcwd):
104
    '''
105
    Get current work directory's absolute path.
106
    Like os.getcwd but garanteed to return an unicode-str object.
107
108
    :param fs_encoding: filesystem encoding, defaults to autodetected
109
    :type fs_encoding: str
110
    :param cwd_fnc: callable used to get the path, defaults to os.getcwd
111
    :type cwd_fnc: Callable
112
    :return: path
113
    :rtype: str
114
    '''
115
    path = cwd_fnc()
116
    if isinstance(path, bytes):
117
        path = fsdecode(path, fs_encoding=fs_encoding)
118
    return os.path.abspath(path)
119
120
ENV_PATH[:] = (
121
  fsdecode(path.strip('"'))
122
  for path in os.environ['PATH'].split(os.pathsep)
123
  )
124
125
if PY_LEGACY:
126
    FileNotFoundError = type('FileNotFoundError', (OSError,), {})
127
    range = xrange  # noqa
128
    filter = itertools.ifilter
129
else:
130
    FileNotFoundError = FileNotFoundError
131
    range = range
132
    filter = filter
133
    basestring = str
134
    unicode = str
135