-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact_path.py
executable file
·50 lines (35 loc) · 1.46 KB
/
compact_path.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
#! /usr/bin/env python
"""
Print a compacted version of the given path.
Usage:
compact_path.py [--trigger=LENGTH] PATH
compact_path.py (-h | --help)
compact_path.py --version
Long elements in a path are compacted to one letter. Optionally, path compaction can take place only
when the path exceeds a given trigger length.
PATH The path to compact
Options:
-t, --trigger=LENGTH The path length at which path compaction takes place [default: 0]
"""
from __future__ import print_function
import os
from docopt import docopt
def compact_path(path, trigger=0):
if not path or len(path) == 0 or len(path) <= trigger or path == os.sep:
return path
# Filter out empty path components
parts = list(filter(bool, path.split(os.sep)))
# ... but add one to the beginning if we're dealing with an absolute path
if path[0] == os.sep:
parts.insert(0, '')
# Use the first character of each nonzero-length path component
compacted_parts = [(p.strip()[0] if len(p.strip()) > 0 else '') for p in parts[:-1]]
# Add the full "basename" (last part) of the path
compacted_parts.append(parts[-1])
# Join the path back up with the proper separator
# This strips trailing slashes from the input path
compacted = os.sep.join(compacted_parts)
return compacted
if __name__ == "__main__":
args = docopt(__doc__, version="Compact Path 0.1")
print(compact_path(args['PATH'], trigger=int(args['--trigger'])))