-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path__init__.py
More file actions
65 lines (48 loc) · 1.98 KB
/
__init__.py
File metadata and controls
65 lines (48 loc) · 1.98 KB
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
54
55
56
57
58
59
60
61
62
63
64
import os
import re
from acid.nvim.log import log_debug, log_warning
def path_to_ns(path, stop_paths=None, base_files=None):
log_debug("Supplied path is {}", str(path))
if not path:
log_debug("Possibly empty or scratch buffer. Skipping")
return
if stop_paths is None:
stop_paths = ['src', 'test']
if base_files is None:
base_files = ['project.clj', 'deps.edn', 'build.boot']
stop_paths = sorted(stop_paths, key=lambda x: x.count('/'))
raw_path_list = None
for stop_path in reversed(stop_paths):
stop_path = "/{}/".format(stop_path)
ix = path[::-1].find(stop_path[::-1])
log_debug("Attempting reverse match: [{}: {}] -> {}",
stop_path[::-1], path[::-1], ix)
if ix > 0:
startpos = len(path) - ix
raw_path_list = path[startpos:].replace("_", "-").split('/')
raw_path_list = [x for x in raw_path_list if x]
if len(raw_path_list) > 0:
raw_path_list[-1] = raw_path_list[-1].split('.')[0]
break
if raw_path_list is None:
log_debug("Previous check did not work. Attempting base files")
# Look for project.clj
path = path.replace("_", "-").split('/')[1:]
path[-1] = path[-1].split('.')[0]
for ix, _ in enumerate(path):
for bf in base_files:
if os.path.exists(os.path.join(*["/", *path[:ix], bf])):
raw_path_list = path[ix+1:]
break
if not raw_path_list:
log_warning("Have not found any viable path")
return ""
else:
log_debug("Found path list: {}", raw_path_list)
return ".".join(raw_path_list)
def ns_to_path(ns):
return ns.replace("-", "_").replace(".", "/")
def rename_file(cf_name, rename_fn):
"Takes a filename and a function and renames preserving the extension."
splitted = cf_name.split('.')
return '.'.join([rename_fn(splitted[0]), *splitted[1:]])