-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathZnunyCustomizers.py
260 lines (168 loc) · 9.31 KB
/
ZnunyCustomizers.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# http://docs.sublimetext.info/en/latest/extensibility/plugins.html
# window.run_command('znuny_customizers')
# view.run_command('znuny_customizers')
# sublime.run_command('znuny_customizers')
import sublime
import sublime_plugin
import os
import json
import re
import base64
import codecs
# taken from: https://github.com/twolfson/sublime-request/blob/master/request.py#L5
# Attempt to load urllib.request/error and fallback to urllib2 (Python 2/3 compat)
try:
from urllib.request import urlopen, Request
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen, URLError
# https://github.com/titoBouzout/Open-Include/issues/28#issuecomment-31145976
def plugin_loaded():
global settings
settings = sublime.load_settings('Znuny.sublime-settings')
class ZnunyCustomizers(sublime_plugin.WindowCommand):
repository_names = ['Znuny', 'ITSMIncidentProblemManagement', 'ITSMConfigurationManagement', 'ITSMChangeManagement', 'ITSMCore', 'GeneralCatalog', 'ITSMServiceLevelManagement', 'ImportExport', 'OTRSMasterSlave', 'TimeAccounting', 'Survey', 'FAQ'] # noqa: E501
selected_repository = ''
branch_names = []
branch_files = []
selected_branch = ''
file = {}
def run(self):
sublime.active_window().show_quick_panel(self.repository_names, self.repository_selected)
def repository_selected(self, index):
if index == -1:
return
self.selected_repository = self.repository_names[index]
branches = self.branches()
self.branch_names = []
for branch in branches:
self.branch_names.append(branch['name'])
# reverse branches
self.branch_names = self.branch_names[::-1]
sublime.status_message('Showing branch selection.')
sublime.active_window().show_quick_panel(self.branch_names, self.branch_selected)
def branches(self):
url = 'https://api.github.com/repos/znuny/%s/branches' % self.selected_repository
sublime.status_message('fetching branches from "%s".' % url)
return self.url_json(url)
def branch_selected(self, index):
if index == -1:
return
self.selected_branch = self.branch_names[index]
sublime.status_message('Branch "%s" selected.' % self.selected_branch)
self.fetch_branch_files()
sublime.status_message('Showing file selection for branch "%s".' % self.selected_branch)
sublime.active_window().show_quick_panel(self.branch_files, self.file_selected)
def fetch_branch_files(self):
url = "https://api.github.com/repos/znuny/%s/git/trees/%s?recursive=1" % (self.selected_repository, self.selected_branch)
sublime.status_message('Fetching files for branch "%s" from "%s".' % (self.selected_branch, url))
self.branch_files = []
tree = self.url_json(url)
sublime.status_message('Files fetched for branch "%s". Building file list.' % self.selected_branch)
for file in tree['tree']:
# skip folder names
if file['type'] == 'tree':
continue
self.branch_files.append(file['path'])
def file_selected(self, index):
if index == -1:
return
file_path = self.branch_files[index]
sublime.status_message('Selected file "%s" from branch "%s".' % (file_path, self.selected_branch))
url = 'https://api.github.com/repos/znuny/%s/contents/%s?ref=%s' % (self.selected_repository, file_path, self.selected_branch)
sublime.status_message('Fetching file information for file "%s" from branch "%s" from "%s".' % (file_path, self.selected_branch, url))
file_information = self.url_json(url)
url = "https://api.github.com/repos/znuny/%s/commits?path=%s;sha=%s" % (self.selected_repository, file_path, self.selected_branch)
sublime.status_message('Fetching commits for file "%s" from branch "%s" from "%s".' % (file_path, self.selected_branch, url))
commits = self.url_json(url)
file_content = base64.decodestring(file_information['content'].encode('utf-8')).decode('utf-8')
sublime.status_message('Decoded file "%s" from branch "%s". Adding custom header.' % (file_path, self.selected_branch))
file_content = self.custom_header(file_content, file_path, commits[0]['sha'])
if file_path.endswith('.pm') or file_path.endswith('.dtl') or file_path.endswith('.tt'):
sublime.status_message('Adding file "%s" to Custom/ folder.')
file_path = "Custom/%s" % file_path
# fix windows line endings
file_content = file_content.replace('\r\n', '\n')
file_content = file_content.replace('\r', '\n')
self.file['path'] = file_path
self.file['content'] = file_content
sublime.status_message('Determing possible target folders for file "%s" from branch "%s".' % (self.file['path'], self.selected_branch))
folders = self.window.folders()
if len(folders) > 1:
sublime.status_message('Showing folder selection for file "%s" from branch "%s".' % (self.file['path'], self.selected_branch))
sublime.active_window().show_quick_panel(folders, self.folder_selected)
else:
self.file['folder'] = folders[0]
self.write_and_open_file()
def custom_header(self, content, path, sha):
copyright = " Copyright (C) 2012 Znuny GmbH, https://znuny.com/"
comment_prefix = '#'
comment_prefix_regex = '\%s' % comment_prefix # noqa: W605
if path.endswith('.js'):
comment_prefix = '//'
comment_prefix_regex = comment_prefix
# prepare origin block
origin_block = "%s --\n" % comment_prefix
origin_block += "%s $origin: %s - %s - %s" % (comment_prefix, self.selected_repository, sha, path)
# prepare customization header with copyright and origin
customization_block = "\n%s%s\n%s" % (comment_prefix, copyright, origin_block)
search_regex = '(^%s\s+Copyright\s[^\n]+\sOTRS\sAG[^\n]+\n)' % comment_prefix_regex # noqa: W605
insert_regex = '\\1%s' % customization_block
search = re.compile(search_regex, re.VERBOSE | re.DOTALL | re.MULTILINE | re.IGNORECASE)
return search.sub(insert_regex, content)
def folder_selected(self, index):
folders = self.window.folders()
self.file['folder'] = folders[index]
self.write_and_open_file()
def write_and_open_file(self):
sublime.status_message('Adding file "%s" from branch "%s" to folder "%s".' % (self.file['path'], self.selected_branch, self.file['folder']))
self.file['absolut_path'] = '%s/%s' % (self.file['folder'], self.file['path'])
self.write_to_file()
sublime.status_message('Opening file "%s" from branch "%s".' % (self.file['path'], self.selected_branch))
sublime.active_window().open_file(self.file['absolut_path'])
self.refresh_folders()
sublime.status_message('Activating view for file "%s" from branch "%s".' % (self.file['path'], self.selected_branch))
sublime.active_window().focus_view(sublime.active_window().active_view())
self.file = {}
def refresh_folders(self):
sublime.status_message('Refreshing folders.')
data = sublime.active_window().project_data()
sublime.active_window().set_project_data({})
sublime.active_window().set_project_data(data)
def write_to_file(self):
directory = os.path.dirname(self.file['absolut_path'])
if not os.path.exists(directory):
sublime.status_message('Creating folder structure "%s" for file "%s" from branch "%s".' % (directory, self.file['path'], self.selected_branch)) # noqa: E501
os.makedirs(directory)
sublime.status_message('Writing content to file "%s" from branch "%s".' % (self.file['path'], self.selected_branch))
file_handle = codecs.open(self.file['absolut_path'], 'w', 'utf-8')
file_handle.write(self.file['content'])
file_handle.close()
return
def url_json(self, url):
json_result = self.url_content(url)
sublime.status_message('Successfully read from "%s"' % url)
sublime.status_message('json_result "%s"' % json_result)
return json.loads(json_result)
def url_content(self, url):
req = self.url_request(url)
return req.read().decode(req.headers.get_content_charset())
def url_request(self, url):
request = Request(url)
github_username = settings.get("znuny_github_username")
github_token = settings.get("znuny_github_token")
if github_username and github_token and len(github_username) > 0 and len(github_token) > 0:
credentials = '%s:%s' % (github_username, github_token)
credentials_base64 = base64.b64encode(credentials.encode('utf-8'))
request.add_header("Authorization", "Basic %s" % credentials_base64.decode('utf-8'))
# Attempt to open the url
try:
# Make our open request
req = urlopen(request)
except TypeError as err:
# If the arguments are malformed, display the error
return sublime.status_message(str(err))
except URLError:
# Otherwise, if there was a connection error, let it be known
return sublime.status_message('Error connecting to "%s"' % url)
return req