1import os
2import platform
3
4
5if os.name == "posix":
6    if platform.system() == "Darwin":
7        DEFAULT_SOCKET_DIRS = ("/tmp",)
8    else:
9        DEFAULT_SOCKET_DIRS = ("/var/run", "/var/lib")
10else:
11    DEFAULT_SOCKET_DIRS = ()
12
13
14def list_path(root_dir):
15    """List directory if exists.
16
17    :param root_dir: str
18    :return: list
19
20    """
21    res = []
22    if os.path.isdir(root_dir):
23        for name in os.listdir(root_dir):
24            res.append(name)
25    return res
26
27
28def complete_path(curr_dir, last_dir):
29    """Return the path to complete that matches the last entered component.
30
31    If the last entered component is ~, expanded path would not
32    match, so return all of the available paths.
33
34    :param curr_dir: str
35    :param last_dir: str
36    :return: str
37
38    """
39    if not last_dir or curr_dir.startswith(last_dir):
40        return curr_dir
41    elif last_dir == '~':
42        return os.path.join(last_dir, curr_dir)
43
44
45def parse_path(root_dir):
46    """Split path into head and last component for the completer.
47
48    Also return position where last component starts.
49
50    :param root_dir: str path
51    :return: tuple of (string, string, int)
52
53    """
54    base_dir, last_dir, position = '', '', 0
55    if root_dir:
56        base_dir, last_dir = os.path.split(root_dir)
57        position = -len(last_dir) if last_dir else 0
58    return base_dir, last_dir, position
59
60
61def suggest_path(root_dir):
62    """List all files and subdirectories in a directory.
63
64    If the directory is not specified, suggest root directory,
65    user directory, current and parent directory.
66
67    :param root_dir: string: directory to list
68    :return: list
69
70    """
71    if not root_dir:
72        return [os.path.abspath(os.sep), '~', os.curdir, os.pardir]
73
74    if '~' in root_dir:
75        root_dir = os.path.expanduser(root_dir)
76
77    if not os.path.exists(root_dir):
78        root_dir, _ = os.path.split(root_dir)
79
80    return list_path(root_dir)
81
82
83def dir_path_exists(path):
84    """Check if the directory path exists for a given file.
85
86    For example, for a file /home/user/.cache/mycli/log, check if
87    /home/user/.cache/mycli exists.
88
89    :param str path: The file path.
90    :return: Whether or not the directory path exists.
91
92    """
93    return os.path.exists(os.path.dirname(path))
94
95
96def guess_socket_location():
97    """Try to guess the location of the default mysql socket file."""
98    socket_dirs = filter(os.path.exists, DEFAULT_SOCKET_DIRS)
99    for directory in socket_dirs:
100        for r, dirs, files in os.walk(directory, topdown=True):
101            for filename in files:
102                name, ext = os.path.splitext(filename)
103                if name.startswith("mysql") and ext in ('.socket', '.sock'):
104                    return os.path.join(r, filename)
105            dirs[:] = [d for d in dirs if d.startswith("mysql")]
106    return None
107