Skip to content

Commit

Permalink
[Dir Flattening] Workaround for joining paths on windows platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Miller committed Feb 15, 2019
1 parent de1ba52 commit 6a1ba86
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/scripts/treemacs-dirs-to-collapse.py
@@ -1,5 +1,5 @@
from os import listdir
from os.path import abspath, isdir, isfile
from os.path import isdir
from posixpath import join
import sys
import os
Expand All @@ -8,13 +8,46 @@
LIMIT = int(sys.argv[2])
SHOW_ALL = sys.argv[3] == 't'

# special workaround for windows platforms
# the default `join' implementation cannot quite deal with windows
# paths in the form of "C:/A/B" & "C:/A/B/C", joining them as
# "C:/A/B/C:/A/B/C"
# it can, however, be "tricked" into doing the right thing by adding
# a slash to the start of the paths
# go figure
if sys.platform == 'win32':
def join_dirs(d1, d2, full_path=False):
missing_slash = False
if not d1.startswith("/"):
missing_slash = True
d1 = "/" + d1
# full_path is only True when the second argument is
# another absolute path
if full_path and not d2.startswith("/"):
missing_slash = True
d2 = "/" + d2
joined = join(d1, d2)
if missing_slash:
# still need to return the joined path without the
# leading slash, the way it looked originally
return joined[1:]
else:
return joined
else:
def join_dirs(d1, d2, _):
return join(d1, d2)

if LIMIT <= 0:
exit(0)

def dir_content(path):
"""
returns the content of given path, excluding unreadable files
and dotfiles (unless SHOW_ALL is True)
"""
ret = []
for item in listdir(path):
full_path = join(path, item)
full_path = join_dirs(path, item)
if os.access(full_path, os.R_OK) and (SHOW_ALL or item[0] != '.'):
ret.append(full_path)
return ret
Expand All @@ -31,7 +64,7 @@ def main():
while True:
if len(content) == 1 and isdir(content[0]):
single_path = content[0]
collapsed = join(collapsed, single_path)
collapsed = join_dirs(collapsed, single_path, True)
content = dir_content(collapsed)
depth += 1
steps.append(single_path)
Expand Down

0 comments on commit 6a1ba86

Please sign in to comment.