This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
AddFlow.py
93 lines (61 loc) · 2.32 KB
/
AddFlow.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os, time, re
import sublime
import sublime_plugin
import glob
import os
from xml.etree import ElementTree
current_path = None
class AddFlowCommand(sublime_plugin.WindowCommand):
ROOT_DIR_PREFIX = '[root: '
ROOT_DIR_SUFFIX = ']'
INPUT_PANEL_CAPTION = 'File name:'
def run(self):
if not self.find_root():
return
self.find_templates()
self.template_selected()
# self.show_quick_panel(self.templates, self.template_selected)
def find_root(self):
folders = self.window.folders()
if len(folders) == 0:
sublime.error_message('Could not find project root')
return False
self.root = folders[0]
self.rel_path_start = len(self.root) + 1
return True
def find_templates(self):
self.templates = []
self.template_paths = []
for root, dirnames, filenames in os.walk(sublime.packages_path()):
for filename in filenames:
if filename.endswith(".flow-template"):
print(filename)
self.template_paths.append(os.path.join(root, filename))
self.templates.append(os.path.basename(root) + ": " + os.path.splitext(filename)[0])
def template_selected(self):
self.template_path = self.template_paths[0]
#print self.template_path
tree = ElementTree.parse(open(self.template_path))
self.template = tree
cur_view = self.window.active_view()
templ_content = self.get_content();
if len(templ_content ) == 0:
sublime.error_message('bad cookie')
return
else:
cur_view.run_command("insert_snippet", {'contents': templ_content })
def get_content(self):
content = ''
try:
content = self.template.find("content").text
except:
pass
try:
path = os.path.abspath(os.path.join(os.path.dirname(self.template_path), self.template.find("file").text))
content = open(path).read()
print(content)
except:
pass
return content
def show_quick_panel(self, options, done):
sublime.set_timeout(lambda: self.window.show_quick_panel(options, done), 10)