Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 56 lines (38 sloc) 1.47 kB
c526c6e @vojtajina Initial commit
authored
1 import re
2
3
cd87803 @vojtajina Allow windows paths (backslash)
authored
4 class Converter(object):
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
5 def __init__(self, patterns):
6 self.from_re = []
c526c6e @vojtajina Initial commit
authored
7 self.to_exp = []
8
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
9 for pattern in patterns:
10 self.from_re.append(self._pattern_to_re(pattern))
11 self.to_exp.append(self._pattern_to_formatter(pattern))
c526c6e @vojtajina Initial commit
authored
12
13 def convert(self, path):
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
14 results = []
15 lenght = len(self.from_re)
c526c6e @vojtajina Initial commit
authored
16
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
17 for i in range(lenght):
18 match = self.from_re[i].match(path)
19 if not match: continue
20
21 j = (i + 1) % lenght
22 while (j is not i):
23 results.append(self.to_exp[j].format(*match.groups()))
24 j = (j + 1) % lenght
25
26 break
27
28 return results
c526c6e @vojtajina Initial commit
authored
29
cd87803 @vojtajina Allow windows paths (backslash)
authored
30 def _pattern_to_re(self, pattern):
31 return re.compile(re.escape(pattern).replace("\\*", "(.*)"))
32
c526c6e @vojtajina Initial commit
authored
33 def _pattern_to_formatter(self, pattern):
34 counter = [-1]
35 def increment(m):
36 counter[0] += 1
37 return "{" + str(counter[0]) + "}"
38
39 return re.sub(r"\*", increment, pattern)
40
41 def _is_formatter(self, exp):
42 return exp.find("*") != -1
cd87803 @vojtajina Allow windows paths (backslash)
authored
43
44
45 class WindowsConverter(Converter):
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
46 def __init__(self, patterns):
7fd1658 Adds compatability for Sublime Text 3
Daniel Shannon authored
47 super(WindowsConverter, self).__init__(list(map(self._normalize, patterns)))
cd87803 @vojtajina Allow windows paths (backslash)
authored
48
49 def _normalize(self, path):
50 return path.replace("/", "\\")
51
52
c3b438a @vojtajina Allow multiple opening multiple files at once
authored
53 def create(patterns, platform):
54 if platform is "windows": return WindowsConverter(patterns)
55 else: return Converter(patterns)
Something went wrong with that request. Please try again.