-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIExample.py
126 lines (100 loc) · 3.9 KB
/
APIExample.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sublime
import sublime_plugin
import os
import re
log_header = '[APIExample] '
example_path = os.path.join(os.path.dirname(__file__), 'examples')
fdir = os.path.dirname(__file__)
def get_files(path):
if os.path.isfile(path):
return [path]
rlist = []
for fname in os.listdir(path):
unknownPath = os.path.join(path, fname)
rlist.extend(get_files(unknownPath))
return rlist
def get_file_path():
filePaths = get_files(example_path)
return filePaths
def split_path(filePaths):
rlist = []
for p in filePaths:
path, fname = os.path.split(p)
fname = fname.split('.')[0]
rlist.append(fname)
return rlist
def split_with_category(filePaths):
rlist = []
for file_path in filePaths:
path, fname = os.path.split(file_path)
fname = fname.split('.')[0]
if '\\' in path:
index = path.rfind('\\')
path = path[index + 1:]
rlist.append(path + " -> " + fname)
return rlist
class ExampleShowInNewFileCommand(sublime_plugin.WindowCommand):
# window.run_command('example_show_in_new_file', {'output':"helloworld"})
def run_(self, edit_token, args):
if args:
output = args.get('output', 'content not set')
fname = 'TestExample.py'
filename = fdir + '/' + fname
with open(filename, 'w+', encoding='utf-8') as fp:
fp.write(output)
sublime.set_timeout_async(
lambda: self.window.open_file(filename), 0)
class ExampleShowListCommand(sublime_plugin.WindowCommand):
def run(self):
print(log_header + 'show example list')
self.filePaths = get_file_path()
showList = split_with_category(self.filePaths)
self.window.show_quick_panel(showList, self.on_done)
def on_done(self, index):
if index == -1:
return
print(log_header + 'open file : ' + self.filePaths[index])
with open(self.filePaths[index], 'r', encoding='utf-8') as fp:
content = fp.read()
sublime.set_timeout_async(lambda:self.window.run_command('example_show_in_new_file', {'output': content}), 0)
class ExampleRunTestCommand(sublime_plugin.TextCommand):
def run(self, edit):
# get command and type then run command
# type = window, view, application
if self.view.file_name().endswith('TestExample.py'):
region = sublime.Region(0, self.view.size())
content = self.view.substr(region)
restr = re.compile(
r'class (.*?)Command\(.*?sublime_plugin\.(.*?)\):', re.S)
items = re.findall(restr, content)
for item in items:
command = item[0]
cmd_type = item[1]
print(log_header + "command : " + command + ", type : " +
cmd_type)
command = self.format_cmd(command)
# show console panel before run command
self.view.window().run_command('show_panel', {
"panel": "console",
"toggle": False
})
print(log_header + 'run command : ' + command)
if cmd_type == "TextCommand":
self.view.run_command(command)
elif cmd_type == "WindowCommand":
self.view.active_window().run_command(command)
elif cmd_type == "ApplicationCommand":
sublime.run_command(command)
else:
pass
# eg: ApiRunTest -> api_run_test
def format_cmd(self, cmd):
rlist = []
last_index = 0
for i in range(len(cmd)):
if cmd[i].isupper() and i != 0:
rlist.append(cmd[last_index:i])
last_index = i
rlist.append(cmd[last_index:])
return '_'.join(map(str.lower, rlist))
from .examples import *