forked from phil65/KodiDevKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kodidevkit.py
871 lines (727 loc) · 33 KB
/
kodidevkit.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# -*- coding: utf8 -*-
# Copyright (C) 2015 - Philipp Temminghoff <phil65@kodi.tv>
# This program is Free Software see LICENSE file for details
"""
KodiDevKit is a plugin to assist with Kodi skinning / scripting using Sublime Text 3
"""
import sublime_plugin
import sublime
import time
import re
import os
import webbrowser
import logging
from itertools import chain
from xml.sax.saxutils import escape
from lxml import etree as ET
from threading import Timer
import mdpopups
from .libs import utils
from .libs import sublimelogger
from .libs import infoprovider
from .libs.kodi import kodi
INFOS = infoprovider.InfoProvider()
# sublime.log_commands(True)
SETTINGS_FILE = 'kodidevkit.sublime-settings'
sublimelogger.config()
CONST_ATTRIBUTES = {"x", "y", "width", "height", "center", "max", "min", "w", "h", "time",
"acceleration", "delay", "start", "end", "center", "border", "repeat"}
CONST_NODES = {"posx", "posy", "left", "centerleft", "right", "centerright", "top", "centertop",
"bottom", "centerbottom", "width", "height", "offsetx", "offsety", "textoffsetx",
"textoffsety", "textwidth", "spinposx", "spinposy", "spinwidth", "spinheight",
"radioposx", "radioposy", "radiowidth", "radioheight", "markwidth", "markheight",
"sliderwidth", "sliderheight", "itemgap", "bordersize", "timeperimage", "fadetime",
"pauseatend", "depth"}
LABEL_TAGS = {"label", "label2", "altlabel", "property", "hinttext"}
VISIBLE_TAGS = {"visible", "enable", "usealttexture", "expression", "autoscroll"}
def plugin_loaded():
"""
ST API method: gets called once API is ready
"""
settings = sublime.load_settings(SETTINGS_FILE)
kodi.load_settings(settings)
INFOS.load_settings(settings)
INFOS.load_data()
KodiDevKit.settings = settings
class KodiDevKit(sublime_plugin.EventListener):
settings = {}
"""
Handles interaction of ST with InfoProviders (kodi/skin)
"""
def __init__(self, **kwargs):
self.actual_project = None
self.prev_selection = None
self.is_modified = False
self.root = None
self.tree = None
self.timer = None
def on_query_completions(self, view, prefix, locations):
"""
ST API method: gets called when autocompletion triggers
"""
completions = []
scope_name = view.scope_name(view.sel()[0].b)
filename = view.file_name()
if not filename or not INFOS.addon:
return []
folder = filename.split(os.sep)[-2]
if folder not in INFOS.addon.includes:
return []
if "text.xml" in scope_name:
for dir_, _, files in os.walk(INFOS.addon.media_path):
for filename in files:
rel_dir = os.path.relpath(dir_, INFOS.addon.media_path)
rel_file = os.path.join(rel_dir, filename).lstrip("./").lstrip("\\")
rel_file = rel_file.replace("\\", "/")
completions.append([rel_file, rel_file])
colors = []
for node in INFOS.get_colors():
if node["name"] not in colors:
colors.append(node["name"])
completions.append(["%s (%s)" % (node["name"], node["content"]), node["name"]])
for node in chain(INFOS.addon.includes[folder], INFOS.addon.fonts[folder]):
completions.append([node["name"], node["name"]])
for node in chain(INFOS.builtins, INFOS.conditions):
completions.append([node[0], node[0]])
for item in INFOS.WINDOW_NAMES:
completions.append([item, item])
for item in completions:
for i, match in enumerate(re.findall(r"\([a-z,\]\[]+\)", item[1])):
item[1] = item[1].replace(match, "($%i)" % (i + 1))
return completions
# return (completions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
def on_selection_modified_async(self, view):
"""
ST API method: gets called when selection changed
"""
if len(view.sel()) > 1 or not INFOS.addon:
return None
try:
region = view.sel()[0]
except Exception:
return None
if region == self.prev_selection:
return None
self.prev_selection = region
delay = self.settings.get("tooltip_delay", 200)
if self.timer:
self.timer.cancel()
self.timer = Timer(delay / 1000, self.show_tooltip, (view,))
self.timer.start()
def get_tooltip(self, view):
"""
get correct tooltip based on context (veeery hacky atm)
"""
region = view.sel()[0]
scope_name = view.scope_name(region.b)
line = view.line(region)
if "source.python" in scope_name:
line_contents = view.substr(line).lower().strip()
if "lang" in line_contents or "label" in line_contents or "string" in line_contents:
word = view.substr(view.word(region))
text = INFOS.return_label(word)
if text:
return text
elif "text.xml" in scope_name:
flags = sublime.CLASS_WORD_START | sublime.CLASS_WORD_END
scope_content = view.substr(view.extract_scope(region.b))
label_region = view.expand_by_class(region, flags, '$],')
bracket_region = view.expand_by_class(region, flags, '<>')
row, _ = view.rowcol(view.sel()[0].begin())
selected_content = view.substr(view.expand_by_class(region, flags, '<>"[]'))
info_type = ""
info_id = ""
if label_region.begin() > bracket_region.begin() and label_region.end() < bracket_region.end():
info_list = view.substr(label_region).split("[", 1)
info_type = info_list[0]
if len(info_list) > 1:
info_id = info_list[1]
if "constant.other.allcaps" in scope_name:
window_name = scope_content.lower()[1:-1]
if window_name in INFOS.WINDOW_NAMES:
window_index = INFOS.WINDOW_NAMES.index(window_name)
return INFOS.WINDOW_FILENAMES[window_index]
if info_type in ["VAR", "ESCVAR", "EXP"]:
content = self.get_formatted_include(selected_content, view)
if content:
return content
if info_type in ["INFO", "ESCINFO"]:
result = kodi.request(method="XBMC.GetInfoLabels",
params={"labels": [info_id]})
if result:
_, value = result["result"].popitem()
if value:
return str(value)
if info_type == "LOCALIZE":
return INFOS.return_label(info_id)
element = None
if self.tree:
for i in self.tree.iter():
if i.sourceline >= row + 1:
element = i
break
if "string.quoted.double.xml" in scope_name:
content = scope_content[1:-1]
if content.isdigit() and element is not None and any(k in element.attrib for k in {"fallback", "label"}):
label_id = INFOS.return_label(content)
if label_id:
return label_id
if content.endswith((".png", ".jpg", ".gif")):
image_info = INFOS.get_image_info(content)
if image_info:
return image_info
content = self.get_formatted_include(selected_content, view)
if content:
return content
if element is not None and element.tag in VISIBLE_TAGS:
if re.search('[a-zA-Z]', selected_content):
result = kodi.request(method="XBMC.GetInfoBooleans",
params={"booleans": [selected_content]})
if result:
key, value = result["result"].popitem()
if value is not None:
return "%s: <b>%s</b>" % (key, value)
if element is not None and element.tag in LABEL_TAGS:
label = INFOS.return_label(selected_content)
if label:
return label
if selected_content.endswith((".png", ".gif", ".jpg")):
image_info = INFOS.get_image_info(selected_content)
if image_info:
return image_info
if element is not None and element.tag == "control":
# TODO: add positioning based on parent nodes
return INFOS.get_ancestor_info(element)
color = INFOS.get_color_info_html(selected_content)
if color:
return color
@staticmethod
def get_formatted_include(content, view):
folder = view.file_name().split(os.sep)[-2]
node = INFOS.addon.return_node(content, folder=folder)
if node:
node_content = str(node["content"])
if not node_content:
pass
elif len(node_content) < 10000:
return mdpopups.syntax_highlight(view=view,
src=node_content,
language="xml")
else:
return "include too big for preview"
def show_tooltip(self, view):
"""
show tooltip using mdpopups
"""
if not view.file_name():
return None
tooltip = self.get_tooltip(view)
if not tooltip:
return None
mdpopups.show_popup(view=view,
content=tooltip,
flags=sublime.COOPERATE_WITH_AUTO_COMPLETE,
max_width=self.settings.get("tooltip_width", 1000),
max_height=self.settings.get("height", 400),
on_navigate=lambda label_id, view=view: utils.jump_to_label_declaration(view, tooltip))
def on_modified_async(self, view):
if INFOS.addon and INFOS.addon.path:
if view.file_name() and view.file_name().endswith(".xml"):
self.is_modified = True
def on_load_async(self, view):
self.check_status()
def on_activated_async(self, view):
self.check_status()
def on_deactivated_async(self, view):
view.hide_popup()
def on_post_save_async(self, view):
if not INFOS.addon or not view.file_name():
return False
if view.file_name().endswith(".xml"):
if not self.is_modified:
return False
INFOS.addon.update_xml_files()
filename = os.path.basename(view.file_name())
folder = view.file_name().split(os.sep)[-2]
INFOS.addon.reload(view.file_name())
self.root = utils.get_root_from_file(view.file_name())
self.tree = ET.ElementTree(self.root)
if (folder in INFOS.addon.window_files and filename in INFOS.addon.window_files[folder]) or folder == "colors":
if self.settings.get("auto_reload_skin", True):
self.is_modified = False
view.window().run_command("execute_builtin",
{"builtin": "ReloadSkin()"})
if self.settings.get("auto_skin_check", True):
view.window().run_command("check_variables",
{"check_type": "file"})
if view.file_name().endswith(".po"):
INFOS.addon.update_labels()
def check_status(self):
"""
check currently visible view, assign syntax file and update InfoProvider if needed
"""
view = sublime.active_window().active_view()
self.filename = view.file_name()
self.root = None
self.tree = None
if not self.filename:
return None
if INFOS.addon and self.filename and self.filename.endswith(".xml"):
self.root = utils.get_root_from_file(self.filename)
self.tree = ET.ElementTree(self.root)
view.assign_syntax('Packages/KodiDevKit/KodiSkinXML.sublime-syntax')
if self.filename and self.filename.endswith(".po"):
view.assign_syntax('Packages/KodiDevKit/Gettext.tmLanguage')
if self.filename and self.filename.endswith(".log"):
view.assign_syntax('Packages/KodiDevKit/KodiLog.sublime-syntax')
if view:
wnd = view.window()
if wnd is None:
return None
variables = wnd.extract_variables()
if "folder" in variables:
project_folder = variables["folder"]
if project_folder and project_folder != self.actual_project:
self.actual_project = project_folder
logging.info("project change detected: " + project_folder)
INFOS.init_addon(project_folder)
else:
logging.info("Could not find folder path in project file")
class ReloadKodiLanguageFilesCommand(sublime_plugin.WindowCommand):
"""
Command to manually reload language files
"""
def run(self):
INFOS.load_settings(sublime.load_settings(SETTINGS_FILE))
kodi.update_labels()
INFOS.addon.update_labels()
class QuickPanelCommand(sublime_plugin.WindowCommand):
"""
Parent class including method callbacks to show location and select text
"""
def is_visible(self):
return bool(INFOS.addon)
def on_done(self, index):
if index == -1:
return None
node = self.nodes[index]
view = self.window.open_file("%s:%i" % (node["file"], node["line"]),
sublime.ENCODED_POSITION)
self.select_text(view, node)
def show_preview(self, index):
node = self.nodes[index]
self.window.open_file("%s:%i" % (node["file"], node["line"]),
sublime.ENCODED_POSITION | sublime.TRANSIENT)
@staticmethod
def select_text(view, node):
while view.is_loading():
time.sleep(0.01)
view.sel().clear()
if "identifier" in node:
text_point = view.text_point(node["line"] - 1, 0)
line_contents = view.substr(view.line(text_point))
label = escape(node["identifier"])
if line_contents.count(label) != 1:
return False
line_start = line_contents.find(label)
line_end = line_start + len(label)
view.sel().add(sublime.Region(text_point + line_start, text_point + line_end))
class OpenSkinImageCommand(sublime_plugin.WindowCommand):
"""
Open image with default OS image tool
"""
def is_visible(self):
return bool(INFOS.addon) and os.path.exists(INFOS.addon.media_path)
@utils.run_async
def run(self, pack_textures=True):
path = utils.get_node_content(view=self.window.active_view(),
flags=sublime.CLASS_WORD_START | sublime.CLASS_WORD_END)
imagepath = INFOS.addon.translate_path(path)
if not os.path.exists(imagepath):
return None
webbrowser.open(imagepath)
class BuildAddonCommand(sublime_plugin.WindowCommand):
"""
Create an installable zip archive of the currently open addon
"""
def is_visible(self):
return bool(INFOS.addon) and INFOS.addon.type == "skin"
@utils.run_async
def run(self, pack_textures=True):
path = INFOS.addon.media_path
if pack_textures:
utils.texturepacker(media_path=path,
settings=sublime.load_settings(SETTINGS_FILE))
utils.make_archive(folderpath=path,
archive=os.path.join(path, os.path.basename(path) + ".zip"))
if sublime.ok_cancel_dialog("Zip file created!\nDo you want to show it with a file browser?"):
webbrowser.open(path)
class BuildThemeCommand(sublime_plugin.WindowCommand):
"""
Select and build a theme of the currently open skin
"""
def is_visible(self):
return bool(INFOS.addon) and INFOS.addon.type == "skin" and os.path.exists(INFOS.addon.theme_path)
def run(self):
self.themes = INFOS.addon.get_themes()
self.window.show_quick_panel(items=self.themes,
on_select=self.on_done,
selected_index=0)
@utils.run_async
def on_done(self, index):
if index == -1:
return None
media_path = os.path.join(INFOS.addon.theme_path, self.themes[index])
utils.texturepacker(media_path=media_path,
settings=sublime.load_settings(SETTINGS_FILE),
xbt_filename=self.themes[index] + ".xbt")
if sublime.ok_cancel_dialog("Theme file created!\nDo you want to show it with a file browser?"):
webbrowser.open(media_path)
class ShowFontRefsCommand(QuickPanelCommand):
def run(self):
listitems = []
self.nodes = []
view = self.window.active_view()
font_refs = INFOS.addon.get_font_refs()
self.folder = view.file_name().split(os.sep)[-2]
self.nodes = [ref for ref in font_refs[self.folder] if ref["name"] == "Font_Reg28"]
listitems = [i["name"] for i in self.nodes]
if listitems:
self.window.show_quick_panel(items=listitems,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
else:
logging.critical("No references found")
class SearchFileForLabelsCommand(QuickPanelCommand):
def run(self):
listitems = []
self.nodes = []
labels = []
label_ids = []
regexs = [r"\$LOCALIZE\[([0-9].*?)\]",
r"\$ADDON\[.*?([0-9].*?)\]",
r"(?:label|property|altlabel|label2)>([0-9].*?)<"]
path = self.window.active_view().file_name()
for po_file in INFOS.get_po_files():
labels += [s.msgid for s in po_file]
label_ids += [s.msgctxt for s in po_file]
with open(path, encoding="utf8") as text_file:
for i, line in enumerate(text_file.readlines()):
for regex in regexs:
for match in re.finditer(regex, line):
label_id = "#" + match.group(1)
if label_id in label_ids:
index = label_ids.index(label_id)
listitems.append("%s (%s)" % (labels[index], label_id))
node = {"file": path,
"line": i + 1}
self.nodes.append(node)
if listitems:
self.window.show_quick_panel(items=listitems,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
else:
logging.critical("No references found")
class CheckVariablesCommand(QuickPanelCommand):
"""
start skin check with *check_type and show results in QuickPanel
"""
def run(self, check_type):
if check_type == "file":
filename = self.window.active_view().file_name()
self.nodes = INFOS.check_file(filename)
else:
self.nodes = INFOS.get_check_listitems(check_type)
listitems = [[i["message"], "%s: %s" % (os.path.basename(i["file"]), i["line"])] for i in self.nodes]
if listitems:
self.window.show_quick_panel(items=listitems,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
elif not check_type == "file":
logging.critical("No errors detected")
class OpenActiveWindowXmlFromRemoteCommand(sublime_plugin.WindowCommand):
"""
checks currently active window via JSON and opens corresponding XML file
"""
@utils.run_async
def run(self):
folder = self.window.active_view().file_name().split(os.sep)[-2]
result = kodi.request(method="XBMC.GetInfoLabels",
params={"labels": ["Window.Property(xmlfile)"]})
if not result:
return None
_, value = result["result"].popitem()
if os.path.exists(value):
self.window.open_file(value)
for xml_file in INFOS.addon.window_files[folder]:
if xml_file == value:
path = os.path.join(INFOS.addon.path, folder, xml_file)
self.window.open_file(path)
return None
class SearchForLabelCommand(sublime_plugin.WindowCommand):
"""
Search through all core / addon labels and insert selected entry
"""
def is_visible(self):
return bool(INFOS.addon) and bool(INFOS.get_po_files())
def run(self):
listitems = []
self.ids = []
for po_file in INFOS.get_po_files():
for entry in po_file:
if entry.msgctxt not in self.ids:
self.ids.append(entry.msgctxt)
listitems.append(["%s (%s)" % (entry.msgid, entry.msgctxt), entry.comment])
self.window.show_quick_panel(items=listitems,
on_select=self.label_search_ondone_action,
selected_index=0)
def label_search_ondone_action(self, index):
if index == -1:
return None
view = self.window.active_view()
label_id = int(self.ids[index][1:])
view.run_command("insert",
{"characters": INFOS.build_translate_label(label_id, view)})
class SearchForBuiltinCommand(sublime_plugin.WindowCommand):
def run(self):
listitems = [["%s" % (item[0]), item[1]] for item in INFOS.builtins]
self.window.show_quick_panel(items=listitems,
on_select=self.builtin_search_on_done,
selected_index=0)
def builtin_search_on_done(self, index):
if index == -1:
return None
view = self.window.active_view()
view.run_command("insert", {"characters": INFOS.builtins[index][0]})
class BumpVersionCommand(sublime_plugin.WindowCommand):
"""
Bump Addon version by incrementing addon.xml version and adding changelog entry
"""
def run(self):
self.window.show_quick_panel(items=["Major", "Minor", "Bugfix"],
on_select=self.on_done,
selected_index=0)
def on_done(self, index):
if index == -1:
return None
INFOS.addon.bump_version("9.9.9")
self.window.open_file(INFOS.addon.changelog_path)
class SearchForVisibleConditionCommand(sublime_plugin.WindowCommand):
def run(self):
listitems = [["%s" % (item[0]), item[1]] for item in INFOS.conditions]
self.window.show_quick_panel(items=listitems,
on_select=self.builtin_search_on_done,
selected_index=0)
def builtin_search_on_done(self, index):
if index == -1:
return None
view = self.window.active_view()
view.run_command("insert", {"characters": INFOS.conditions[index][0]})
class SearchForJsonCommand(sublime_plugin.WindowCommand):
"""
search through JSONRPC Introspect results
"""
@utils.run_async
def run(self):
result = kodi.request(method="JSONRPC.Introspect")
self.listitems = [[k, str(v)] for k, v in result["result"]["types"].items()]
self.listitems += [[k, str(v)] for k, v in result["result"]["methods"].items()]
self.listitems += [[k, str(v)] for k, v in result["result"]["notifications"].items()]
self.window.show_quick_panel(items=self.listitems,
on_select=self.builtin_search_on_done,
selected_index=0)
def builtin_search_on_done(self, index):
if index == -1:
return None
view = self.window.active_view()
view.run_command("insert", {"characters": str(self.listitems[index][0])})
class PreviewImageCommand(sublime_plugin.TextCommand):
"""
show image preview of selected text inside SublimeText
"""
def is_visible(self):
if not INFOS.addon or not INFOS.addon.media_path:
return False
flags = sublime.CLASS_WORD_START | sublime.CLASS_WORD_END
content = utils.get_node_content(self.view, flags)
return "/" in content or "\\" in content
def run(self, edit):
flags = sublime.CLASS_WORD_START | sublime.CLASS_WORD_END
path = utils.get_node_content(self.view, flags)
imagepath = INFOS.addon.translate_path(path)
if not os.path.exists(imagepath):
return None
if os.path.isdir(imagepath):
self.files = []
for (_dirpath, _dirnames, filenames) in os.walk(imagepath):
self.files.extend(filenames)
break
self.files = [imagepath + s for s in self.files]
else:
self.files = [imagepath]
sublime.active_window().show_quick_panel(items=self.files,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
def on_done(self, index):
sublime.active_window().focus_view(self.view)
def show_preview(self, index):
if index >= 0:
file_path = self.files[index]
sublime.active_window().open_file(file_path, sublime.TRANSIENT)
class GoToTagCommand(sublime_plugin.WindowCommand):
"""
Jump to include/font/etc
"""
def run(self):
flags = sublime.CLASS_WORD_START | sublime.CLASS_WORD_END
view = self.window.active_view()
position = INFOS.go_to_tag(keyword=utils.get_node_content(view, flags),
folder=view.file_name().split(os.sep)[-2])
if position:
self.window.open_file(position, sublime.ENCODED_POSITION)
class ShowDependenciesCommand(sublime_plugin.WindowCommand):
"""
Show all possible dependencies for open addon
"""
def is_visible(self):
return bool(INFOS.addon)
def run(self):
addons = utils.get_addons(INFOS.addon.api_version)
items = [[k, v] for k, v in addons.items()]
sublime.active_window().show_quick_panel(items=items,
on_select=None,
selected_index=0)
class SearchForImageCommand(sublime_plugin.TextCommand):
"""
Search through all files in media folder via QuickPanel
"""
def is_visible(self):
return bool(INFOS.addon and INFOS.addon.media_path)
def run(self, edit):
self.files = [i for i in INFOS.addon.get_media_files()]
sublime.active_window().show_quick_panel(items=self.files,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
def on_done(self, index):
items = ["Insert path", "Open Image"]
if index >= 0:
sublime.active_window().show_quick_panel(items=items,
on_select=lambda s: self.insert_char(s, index),
selected_index=0)
else:
sublime.active_window().focus_view(self.view)
def insert_char(self, index, fileindex):
if index == 0:
self.view.run_command("insert", {"characters": self.files[fileindex]})
elif index == 1:
os.system("start " + os.path.join(INFOS.addon.media_path, self.files[fileindex]))
sublime.active_window().focus_view(self.view)
def show_preview(self, index):
if index >= 0:
file_path = os.path.join(INFOS.addon.media_path, self.files[index])
sublime.active_window().open_file(file_path, sublime.TRANSIENT)
class SearchForFontCommand(sublime_plugin.TextCommand):
"""
search through all fonts from Fonts.xml via QuickPanel
"""
def is_visible(self):
return bool(INFOS.addon and INFOS.addon.fonts)
def run(self, edit):
self.fonts = []
folder = self.view.file_name().split(os.sep)[-2]
self.fonts = [[i["name"], "%s - %s" % (i["size"], i["filename"])] for i in INFOS.addon.fonts[folder]]
sublime.active_window().show_quick_panel(items=self.fonts,
on_select=self.on_done,
selected_index=0)
def on_done(self, index):
if index >= 0:
self.view.run_command("insert", {"characters": self.fonts[index][0]})
sublime.active_window().focus_view(self.view)
class ConvertXmlToPoCommand(sublime_plugin.WindowCommand):
"""
Show all possible dependencies for open addon
"""
def is_visible(self):
return bool(INFOS.addon)
def run(self):
for item in os.listdir(INFOS.addon.primary_lang_folder):
if item.endswith(".xml"):
path = os.path.join(INFOS.addon.primary_lang_folder, item)
utils.convert_xml_to_po(path)
class MoveToLanguageFileCommand(sublime_plugin.TextCommand):
"""
move selected file to default .po file (or create .po file if not existing)
"""
def is_visible(self):
scope_name = self.view.scope_name(self.view.sel()[0].b)
if INFOS.addon and INFOS.addon.po_files:
if "text.xml" in scope_name or "source.python" in scope_name:
return self.view.sel()[0].b != self.view.sel()[0].a
return False
def run(self, edit):
self.label_ids = []
self.labels = []
region = self.view.sel()[0]
if region.begin() == region.end():
logging.critical("Please select the complete label")
return False
word = self.view.substr(region)
for po_file in INFOS.get_po_files():
for entry in po_file:
if entry.msgid.lower() == word.lower() and entry.msgctxt not in self.label_ids:
self.label_ids.append(entry.msgctxt)
self.labels.append(["%s (%s)" % (entry.msgid, entry.msgctxt), entry.comment])
self.labels.append("Create new label")
sublime.active_window().show_quick_panel(items=self.labels,
on_select=lambda s: self.on_done(s, region),
selected_index=0)
def on_done(self, index, region):
if index == -1:
return None
region = self.view.sel()[0]
rel_path = self.view.file_name().replace(INFOS.addon.path, "").replace("\\", "/")
if self.labels[index] == "Create new label":
label_id = INFOS.addon.create_new_label(word=self.view.substr(region),
filepath=rel_path)
else:
label_id = self.label_ids[index][1:]
INFOS.addon.attach_occurrence_to_label(label_id, rel_path)
self.view.run_command("replace_text", {"label_id": label_id})
class ReplaceTextCommand(sublime_plugin.TextCommand):
"""
replace selected text with label from *label_id
"""
def run(self, edit, label_id):
for region in self.view.sel():
new = INFOS.build_translate_label(int(label_id), self.view)
self.view.replace(edit, region, new)
class SwitchXmlFolderCommand(QuickPanelCommand):
"""
switch to same file in different XML folder if available
"""
def is_visible(self):
return bool(INFOS.addon) and len(INFOS.addon.xml_folders) > 1
def run(self):
view = self.window.active_view()
self.nodes = []
line, _ = view.rowcol(view.sel()[0].b)
filename = os.path.basename(view.file_name())
for folder in INFOS.addon.xml_folders:
node = {"file": os.path.join(INFOS.addon.path, folder, filename),
"line": line + 1}
self.nodes.append(node)
self.window.show_quick_panel(items=INFOS.addon.xml_folders,
on_select=self.on_done,
selected_index=0,
on_highlight=self.show_preview)
def on_done(self, index):
if index == -1:
return None
node = self.nodes[index]
self.window.open_file("%s:%i" % (node["file"], node["line"]),
sublime.ENCODED_POSITION)