-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpaths.py
53 lines (40 loc) · 1.38 KB
/
paths.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from os.path import isdir, expanduser, split, relpath, join, commonprefix
from os import listdir, sep
HOME_DIRECTORY = '~'
def directory_listing_with_slahes(path):
"""
Return directory listing with directories having trailing slashes.
"""
output = []
for filename in listdir(path):
if isdir(join(path, filename)):
output.append(filename + sep)
else:
output.append(filename)
return output
def get_current_directory(view_filename, folders, use_project_dir):
"""
Try to give a sensible estimate for 'current directory'.
If there is a single folder open, we return that.
Else, if there is an active file, return its path.
If all else fails, return the home directory.
"""
if folders and use_project_dir:
directory = folders[0]
elif view_filename is not None:
directory, _ = split(view_filename)
else:
directory = HOME_DIRECTORY
if directory != sep:
directory += sep
return get_path_relative_to_home(directory)
def get_path_relative_to_home(path):
home = expanduser(HOME_DIRECTORY)
if len(commonprefix([home, path])) > 1:
relative_path = relpath(path, home)
if len(relative_path) > 1:
return join(HOME_DIRECTORY, relpath(path, home)) + sep
else:
return HOME_DIRECTORY + sep
else:
return path