-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.py
101 lines (90 loc) · 3.17 KB
/
main.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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Thomas Amland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import shutil
import traceback
import xbmc
import utils
from xbmcgui import Dialog
from editor import Editor
from utils import tr
default = xbmc.translatePath('special://xbmc/system/keymaps/keyboard.xml')
userdata = xbmc.translatePath('special://userdata/keymaps')
gen_file = os.path.join(userdata, 'gen.xml')
def setup_keymap_folder():
if not os.path.exists(userdata):
os.makedirs(userdata)
else:
#make sure there are no user defined keymaps
for name in os.listdir(userdata):
if name.endswith('.xml') and name != os.path.basename(gen_file):
src = os.path.join(userdata, name)
for i in xrange(100):
dst = os.path.join(userdata, "%s.bak.%d" % (name, i))
if os.path.exists(dst):
continue
shutil.move(src, dst)
#successfully renamed
break
def main():
## load mappings ##
try:
setup_keymap_folder()
except Exception:
traceback.print_exc()
utils.rpc('GUI.ShowNotification', title="Keymap Editor",
message="Failed to remove old keymap file", image='error')
return
defaultkeymap = utils.read_keymap(default)
userkeymap = []
if os.path.exists(gen_file):
try:
userkeymap = utils.read_keymap(gen_file)
except Exception:
traceback.print_exc()
utils.rpc('GUI.ShowNotification', title="Keymap Editor",
message="Failed to load keymap file", image='error')
return
## main loop ##
confirm_discard = False
while True:
idx = Dialog().select(tr(30000), [tr(30003), tr(30004), tr(30005)])
if idx == 0:
# edit
editor = Editor(defaultkeymap, userkeymap)
editor.start()
confirm_discard = editor.dirty
elif idx == 1:
# reset
confirm_discard = bool(userkeymap)
userkeymap = []
elif idx == 2:
# save
if os.path.exists(gen_file):
shutil.copyfile(gen_file, gen_file + ".old")
utils.write_keymap(userkeymap, gen_file)
xbmc.executebuiltin("action(reloadkeymaps)")
break
elif idx == -1 and confirm_discard:
if Dialog().yesno(tr(30000), tr(30006)) == 1:
break
else:
break
sys.modules.clear()
if __name__ == "__main__":
main()