-
Notifications
You must be signed in to change notification settings - Fork 57
/
oe.py
932 lines (786 loc) · 31.5 KB
/
oe.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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2013 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2013 Lutz Fiebach (lufie@openelec.tv)
# Copyright (C) 2019-present Team LibreELEC (https://libreelec.tv)
################################# variables ##################################
import xbmc
import xbmcaddon
import xbmcgui
import os
import re
import locale
import sys
import urllib.request, urllib.error, urllib.parse
import time
import tarfile
import traceback
import subprocess
import dbus
import dbus.mainloop.glib
import defaults
import shutil
import hashlib, binascii
from xml.dom import minidom
import imp
__author__ = 'LibreELEC'
__scriptid__ = 'service.libreelec.settings'
__addon__ = xbmcaddon.Addon(id=__scriptid__)
__cwd__ = __addon__.getAddonInfo('path')
__oe__ = sys.modules[globals()['__name__']]
__media__ = '%s/resources/skins/Default/media' % __cwd__
xbmcDialog = xbmcgui.Dialog()
is_service = False
conf_lock = False
__busy__ = 0
xbmcIsPlaying = 0
input_request = False
dictModules = {}
listObject = {
'list': 1100,
'netlist': 1200,
'btlist': 1300,
'other': 1900,
'test': 900,
}
CANCEL = (
9,
10,
216,
247,
257,
275,
61467,
92,
61448,
)
try:
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
except:
pass
dbusSystemBus = dbus.SystemBus()
###############################################################################
########################## initialize module ##################################
## append resource subfolders to path
sys.path.append(xbmc.translatePath(os.path.join(__cwd__, 'resources', 'lib')))
sys.path.append(xbmc.translatePath(os.path.join(__cwd__, 'resources', 'lib', 'modules')))
## set default encoding
encoding = locale.getpreferredencoding(do_setlocale=True)
imp.reload(sys)
# sys.setdefaultencoding(encoding)
## load oeSettings modules
import oeWindows
xbmc.log('## LibreELEC Addon ## ' + str(__addon__.getAddonInfo('version')))
class ProgressDialog:
def __init__(self, label1=32181, label2=32182, label3=32183, minSampleInterval=1.0, maxUpdatesPerSecond=5):
self.label1 = _(label1)
self.label2 = _(label2)
self.label3 = _(label3)
self.minSampleInterval = minSampleInterval
self.maxUpdatesPerSecond = 1 / maxUpdatesPerSecond
self.dialog = None
self.source = None
self.total_size = 0
self.reset()
def reset(self):
self.percent = 0
self.speed = 0
self.partial_size = 0
self.prev_size = 0
self.start = 0
self.last_update = 0
self.minutes = 0
self.seconds = 0
self.cancelled = False
def setSource(self, source):
self.source = source
def setSize(self, total_size):
self.total_size = total_size
def getPercent(self):
return self.percent
def getSpeed(self):
return self.speed
def open(self, heading='LibreELEC', line1='', line2='', line3=''):
self.dialog = xbmcgui.DialogProgress()
self.dialog.create(heading, line1, line2, line3)
self.reset()
def update(self, chunk):
if self.dialog and self.needsUpdate(chunk):
line1 = '%s: %s' % (self.label1, self.source.rsplit('/', 1)[1])
line2 = '%s: %s KB/s' % (self.label2, f'{self.speed:,}')
line3 = '%s: %d m %d s' % (self.label3, self.minutes, self.seconds)
self.dialog.update(self.percent, line1, line2, line3)
self.last_update = time.time()
def close(self):
if self.dialog:
self.dialog.close()
self.dialog = None
# Calculate current speed at regular intervals, or upon completion
def sample(self, chunk):
self.partial_size += len(chunk)
now = time.time()
if self.start == 0:
self.start = now
if (now - self.start) >= self.minSampleInterval or not chunk:
self.speed = max(int((self.partial_size - self.prev_size) / (now - self.start) / 1024), 1)
remain = self.total_size - self.partial_size
self.minutes = int(remain / 1024 / self.speed / 60)
self.seconds = int(remain / 1024 / self.speed) % 60
self.prev_size = self.partial_size
self.start = now
if self.total_size != 0:
self.percent = int(self.partial_size * 100.0 / self.total_size)
# Update the progress dialog when required, or upon completion
def needsUpdate(self, chunk):
if not chunk:
return True
else:
return ((time.time() - self.last_update) >= self.maxUpdatesPerSecond)
def iscanceled(self):
if self.dialog:
self.cancelled = self.dialog.iscanceled()
return self.cancelled
def _(code):
wizardComp = read_setting('libreelec', 'wizard_completed')
if wizardComp == "True":
codeNew = __addon__.getLocalizedString(code)
else:
curLang = read_setting("system", "language")
if curLang is not None:
lang_file = os.path.join(__cwd__, 'resources', 'language', str(curLang), 'strings.po')
with open(lang_file) as fp:
contents = fp.read().decode('utf-8').split('\n\n')
for strings in contents:
if str(code) in strings:
subString = strings.split('msgstr ')[1]
subStringClean = re.sub('"', '', subString)
codeNew = subStringClean
break
else:
codeNew = __addon__.getLocalizedString(code)
else:
codeNew = __addon__.getLocalizedString(code)
return codeNew
def dbg_log(source, text, level=4):
if level == 0 and os.environ.get('DEBUG', 'no') == 'no':
return
xbmc.log('## LibreELEC Addon ## ' + source + ' ## ' + text, level)
if level == 4:
tracedata = traceback.format_exc()
if tracedata != "NoneType: None\n":
xbmc.log(tracedata, level)
def notify(title, message, icon='icon'):
try:
dbg_log('oe::notify', 'enter_function', 0)
msg = 'Notification("%s", "%s", 5000, "%s/%s.png")' % (
title,
message[0:64],
__media__,
icon,
)
xbmc.executebuiltin(msg)
dbg_log('oe::notify', 'exit_function', 0)
except Exception as e:
dbg_log('oe::notify', 'ERROR: (' + repr(e) + ')')
def execute(command_line, get_result=0):
try:
dbg_log('oe::execute', 'enter_function', 0)
dbg_log('oe::execute::command', command_line, 0)
if get_result == 0:
process = subprocess.Popen(command_line, shell=True, close_fds=True)
process.wait()
else:
result = ''
process = subprocess.Popen(command_line, shell=True, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.wait()
for line in process.stdout.readlines():
result = result + line.decode('utf-8')
return result
dbg_log('oe::execute', 'exit_function', 0)
except Exception as e:
dbg_log('oe::execute', 'ERROR: (' + repr(e) + ')')
def enable_service(service):
try:
if os.path.exists('%s/services/%s' % (CONFIG_CACHE, service)):
pass
if os.path.exists('%s/services/%s.disabled' % (CONFIG_CACHE, service)):
pass
service_file = '%s/services/%s' % (CONFIG_CACHE, service)
except Exception as e:
dbg_log('oe::enable_service', 'ERROR: (' + repr(e) + ')')
def set_service_option(service, option, value):
try:
lines = []
changed = False
conf_file_name = '%s/services/%s.conf' % (CONFIG_CACHE, service)
if os.path.isfile(conf_file_name):
with open(conf_file_name, 'r') as conf_file:
for line in conf_file:
if option in line:
line = '%s=%s' % (option, value)
changed = True
lines.append(line.strip())
if changed == False:
lines.append('%s=%s' % (option, value))
with open(conf_file_name, 'w') as conf_file:
conf_file.write('\n'.join(lines) + '\n')
except Exception as e:
dbg_log('oe::set_service_option', 'ERROR: (' + repr(e) + ')')
def get_service_option(service, option, default=None):
try:
lines = []
conf_file_name = ''
if os.path.exists('%s/services/%s.conf' % (CONFIG_CACHE, service)):
conf_file_name = '%s/services/%s.conf' % (CONFIG_CACHE, service)
if os.path.exists('%s/services/%s.disabled' % (CONFIG_CACHE, service)):
conf_file_name = '%s/services/%s.disabled' % (CONFIG_CACHE, service)
if os.path.exists(conf_file_name):
with open(conf_file_name, 'r') as conf_file:
for line in conf_file:
if option in line:
if '=' in line:
default = line.strip().split('=')[-1]
return default
except Exception as e:
dbg_log('oe::get_service_option', 'ERROR: (' + repr(e) + ')')
def get_service_state(service):
try:
if os.path.exists('%s/services/%s.conf' % (CONFIG_CACHE, service)):
return '1'
else:
return '0'
except Exception as e:
dbg_log('oe::get_service_state', 'ERROR: (' + repr(e) + ')')
def set_service(service, options, state):
try:
dbg_log('oe::set_service', 'enter_function', 0)
dbg_log('oe::set_service::service', repr(service), 0)
dbg_log('oe::set_service::options', repr(options), 0)
dbg_log('oe::set_service::state', repr(state), 0)
config = {}
changed = False
# Service Enabled
if state == 1:
# Is Service alwys enabled ?
if get_service_state(service) == '1':
cfn = '%s/services/%s.conf' % (CONFIG_CACHE, service)
cfo = cfn
else:
cfn = '%s/services/%s.conf' % (CONFIG_CACHE, service)
cfo = '%s/services/%s.disabled' % (CONFIG_CACHE, service)
if os.path.exists(cfo) and not cfo == cfn:
os.remove(cfo)
with open(cfn, 'w') as cf:
for option in options:
cf.write('%s=%s\n' % (option, options[option]))
else:
# Service Disabled
cfo = '%s/services/%s.conf' % (CONFIG_CACHE, service)
cfn = '%s/services/%s.disabled' % (CONFIG_CACHE, service)
if os.path.exists(cfo):
os.rename(cfo, cfn)
if not __oe__.is_service:
if service in defaults._services:
for svc in defaults._services[service]:
execute('systemctl restart %s' % svc)
dbg_log('oe::set_service', 'exit_function', 0)
except Exception as e:
dbg_log('oe::set_service', 'ERROR: (' + repr(e) + ')')
def load_file(filename):
try:
if os.path.isfile(filename):
objFile = open(filename, 'r')
content = objFile.read()
objFile.close()
else:
content = ''
return content.strip()
except Exception as e:
dbg_log('oe::load_file(' + filename + ')', 'ERROR: (' + repr(e) + ')')
def url_quote(var):
return urllib.parse.quote(var, safe="")
def load_url(url):
try:
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
content = response.read()
return content.decode('utf-8').strip()
except Exception as e:
dbg_log('oe::load_url(' + url + ')', 'ERROR: (' + repr(e) + ')')
def download_file(source, destination, silent=False):
try:
local_file = open(destination, 'wb')
response = urllib.request.urlopen(urllib.parse.quote(source, safe=':/'))
progress = ProgressDialog()
if not silent:
progress.open()
progress.setSource(source)
progress.setSize(int(response.getheader('Content-Length').strip()))
last_percent = 0
while not (xbmc.abortRequested or progress.iscanceled()):
part = response.read(32768)
progress.sample(part)
if not silent:
progress.update(part)
else:
if progress.getPercent() - last_percent > 5 or not part:
dbg_log('oe::download_file(%s)' % destination, '%d%% with %d KB/s' % (progress.getPercent(), progress.getSpeed()))
last_percent = progress.getPercent()
if part:
local_file.write(part)
else:
break
progress.close()
local_file.close()
response.close()
if progress.iscanceled() or xbmc.abortRequested:
os.remove(destination)
return None
return destination
except Exception as e:
dbg_log('oe::download_file(' + source + ', ' + destination + ')', 'ERROR: (' + repr(e) + ')')
def copy_file(source, destination, silent=False):
try:
dbg_log('oe::copy_file', 'SOURCE: %s, DEST: %s' % (source, destination))
source_file = open(source, 'rb')
destination_file = open(destination, 'wb')
progress = ProgressDialog()
if not silent:
progress.open()
progress.setSource(source)
progress.setSize(os.path.getsize(source))
last_percent = 0
while not (xbmc.abortRequested or progress.iscanceled()):
part = source_file.read(32768)
progress.sample(part)
if not silent:
progress.update(part)
else:
if progress.getPercent() - last_percent > 5 or not part:
dbg_log('oe::copy_file(%s)' % destination, '%d%% with %d KB/s' % (progress.getPercent(), progress.getSpeed()))
last_percent = progress.getPercent()
if part:
destination_file.write(part)
else:
break
progress.close()
source_file.close()
destination_file.close()
if progress.iscanceled() or xbmc.abortRequested:
os.remove(destination)
return None
return destination
except Exception as e:
dbg_log('oe::copy_file(' + source + ', ' + destination + ')', 'ERROR: (' + repr(e) + ')')
def is_busy():
global __busy__
return __busy__ > 0
def set_busy(state):
global __busy__, __oe__, input_request, is_service
try:
if not is_service:
if state == 1:
__busy__ = __busy__ + 1
else:
__busy__ = __busy__ - 1
dbg_log('oe::set_busy', '__busy__ = ' + str(__busy__), 0)
except Exception as e:
dbg_log('oe::set_busy', 'ERROR: (' + repr(e) + ')', 4)
def start_service():
global dictModules, __oe__
try:
__oe__.is_service = True
for strModule in sorted(dictModules, key=lambda x: list(dictModules[x].menu.keys())):
module = dictModules[strModule]
if hasattr(module, 'start_service') and module.ENABLED:
module.start_service()
__oe__.is_service = False
except Exception as e:
dbg_log('oe::start_service', 'ERROR: (' + repr(e) + ')')
def stop_service():
global dictModules
try:
for strModule in dictModules:
module = dictModules[strModule]
if hasattr(module, 'stop_service') and module.ENABLED:
module.stop_service()
xbmc.log('## LibreELEC Addon ## STOP SERVICE DONE !')
except Exception as e:
dbg_log('oe::stop_service', 'ERROR: (' + repr(e) + ')')
def openWizard():
global winOeMain, __cwd__, __oe__
try:
winOeMain = oeWindows.wizard('service-LibreELEC-Settings-wizard.xml', __cwd__, 'Default', oeMain=__oe__)
winOeMain.doModal()
winOeMain = oeWindows.mainWindow('service-LibreELEC-Settings-mainWindow.xml', __cwd__, 'Default', oeMain=__oe__) # None
except Exception as e:
dbg_log('oe::openWizard', 'ERROR: (' + repr(e) + ')')
def openConfigurationWindow():
global winOeMain, __cwd__, __oe__, dictModules
try:
PINmatch = False
PINnext = 1000
PINenable = read_setting('system', 'pinlock_enable')
if PINenable == "0" or PINenable == None:
PINmatch = True
if PINenable == "1":
PINfail = read_setting('system', 'pinlock_timeFail')
if PINfail:
nowTime = time.time()
PINnext = (nowTime - float(PINfail))
if PINnext >= 300:
PINtry = 4
while PINmatch == False:
if PINtry > 0:
PINlock = xbmcDialog.input(_(32233), type=xbmcgui.INPUT_NUMERIC)
if PINlock == '':
break
else:
storedPIN = read_setting('system', 'pinlock_pin')
PINmatch = verify_password(storedPIN, PINlock)
if PINmatch == False:
PINtry -= 1
if PINtry > 0:
xbmcDialog.ok(_(32234), str(PINtry) + _(32235))
else:
timeFail = time.time()
write_setting('system', 'pinlock_timeFail', str(timeFail))
xbmcDialog.ok(_(32234), _(32236))
break
else:
timeLeft = "{0:.2f}".format((300 - PINnext)/60)
xbmcDialog.ok(_(32237), timeLeft + _(32238))
if PINmatch == True:
winOeMain = oeWindows.mainWindow('service-LibreELEC-Settings-mainWindow.xml', __cwd__, 'Default', oeMain=__oe__)
winOeMain.doModal()
for strModule in dictModules:
dictModules[strModule].exit()
winOeMain = None
del winOeMain
else:
pass
except Exception as e:
dbg_log('oe::openConfigurationWindow', 'ERROR: (' + repr(e) + ')')
def standby_devices():
global dictModules
try:
if 'bluetooth' in dictModules:
dictModules['bluetooth'].standby_devices()
except Exception as e:
dbg_log('oe::standby_devices', 'ERROR: (' + repr(e) + ')')
def load_config():
try:
global conf_lock
while conf_lock:
time.sleep(0.2)
conf_lock = True
if os.path.exists(configFile):
config_file = open(configFile, 'r')
config_text = config_file.read()
config_file.close()
else:
config_text = ''
if config_text == '':
xml_conf = minidom.Document()
xml_main = xml_conf.createElement('libreelec')
xml_conf.appendChild(xml_main)
xml_sub = xml_conf.createElement('addon_config')
xml_main.appendChild(xml_sub)
xml_sub = xml_conf.createElement('settings')
xml_main.appendChild(xml_sub)
config_text = xml_conf.toprettyxml()
else:
xml_conf = minidom.parseString(config_text)
conf_lock = False
return xml_conf
except Exception as e:
dbg_log('oe::load_config', 'ERROR: (' + repr(e) + ')')
def save_config(xml_conf):
try:
global configFile, conf_lock
while conf_lock:
time.sleep(0.2)
conf_lock = True
config_file = open(configFile, 'w')
config_file.write(xml_conf.toprettyxml())
config_file.close()
conf_lock = False
except Exception as e:
dbg_log('oe::save_config', 'ERROR: (' + repr(e) + ')')
def read_module(module):
try:
xml_conf = load_config()
xml_settings = xml_conf.getElementsByTagName('settings')
for xml_setting in xml_settings:
for xml_modul in xml_setting.getElementsByTagName(module):
return xml_modul
except Exception as e:
dbg_log('oe::read_module', 'ERROR: (' + repr(e) + ')')
def read_node(node_name):
try:
xml_conf = load_config()
xml_node = xml_conf.getElementsByTagName(node_name)
value = {}
for xml_main_node in xml_node:
value[xml_main_node.nodeName] = {}
for xml_sub_node in xml_main_node.childNodes:
if len(xml_sub_node.childNodes) == 0:
continue
value[xml_main_node.nodeName][xml_sub_node.nodeName] = {}
for xml_value in xml_sub_node.childNodes:
if hasattr(xml_value.firstChild, 'nodeValue'):
value[xml_main_node.nodeName][xml_sub_node.nodeName][xml_value.nodeName] = xml_value.firstChild.nodeValue
else:
value[xml_main_node.nodeName][xml_sub_node.nodeName][xml_value.nodeName] = ''
return value
except Exception as e:
dbg_log('oe::read_node', 'ERROR: (' + repr(e) + ')')
def remove_node(node_name):
try:
xml_conf = load_config()
xml_node = xml_conf.getElementsByTagName(node_name)
for xml_main_node in xml_node:
xml_main_node.parentNode.removeChild(xml_main_node)
save_config(xml_conf)
except Exception as e:
dbg_log('oe::remove_node', 'ERROR: (' + repr(e) + ')')
def read_setting(module, setting, default=None):
try:
xml_conf = load_config()
xml_settings = xml_conf.getElementsByTagName('settings')
value = default
for xml_setting in xml_settings:
for xml_modul in xml_setting.getElementsByTagName(module):
for xml_modul_setting in xml_modul.getElementsByTagName(setting):
if hasattr(xml_modul_setting.firstChild, 'nodeValue'):
value = xml_modul_setting.firstChild.nodeValue
return value
except Exception as e:
dbg_log('oe::read_setting', 'ERROR: (' + repr(e) + ')')
def write_setting(module, setting, value, main_node='settings'):
try:
xml_conf = load_config()
xml_settings = xml_conf.getElementsByTagName(main_node)
if len(xml_settings) == 0:
for xml_main in xml_conf.getElementsByTagName('libreelec'):
xml_sub = xml_conf.createElement(main_node)
xml_main.appendChild(xml_sub)
xml_settings = xml_conf.getElementsByTagName(main_node)
module_found = 0
setting_found = 0
for xml_setting in xml_settings:
for xml_modul in xml_setting.getElementsByTagName(module):
module_found = 1
for xml_modul_setting in xml_modul.getElementsByTagName(setting):
setting_found = 1
if setting_found == 1:
if hasattr(xml_modul_setting.firstChild, 'nodeValue'):
xml_modul_setting.firstChild.nodeValue = value
else:
xml_value = xml_conf.createTextNode(value)
xml_modul_setting.appendChild(xml_value)
else:
if module_found == 0:
xml_modul = xml_conf.createElement(module)
xml_setting.appendChild(xml_modul)
xml_setting = xml_conf.createElement(setting)
xml_modul.appendChild(xml_setting)
xml_value = xml_conf.createTextNode(value)
xml_setting.appendChild(xml_value)
save_config(xml_conf)
except Exception as e:
dbg_log('oe::write_setting', 'ERROR: (' + repr(e) + ')')
def load_modules():
# # load libreelec configuration modules
try:
global dictModules, __oe__, __cwd__, init_done
for strModule in dictModules:
dictModules[strModule] = None
dict_names = {}
dictModules = {}
for file_name in sorted(os.listdir(__cwd__ + '/resources/lib/modules')):
if not file_name.startswith('__') and (file_name.endswith('.py') or file_name.endswith('.pyc')):
(name, ext) = file_name.split('.')
dict_names[name] = None
for module_name in dict_names:
try:
if not module_name in dictModules:
dictModules[module_name] = getattr(__import__(module_name), module_name)(__oe__)
if hasattr(defaults, module_name):
for key in getattr(defaults, module_name):
setattr(dictModules[module_name], key, getattr(defaults, module_name)[key])
except Exception as e:
dbg_log('oe::MAIN(loadingModules)(strModule)', 'ERROR: (' + repr(e) + ')')
except Exception as e:
dbg_log('oe::MAIN(loadingModules)', 'ERROR: (' + repr(e) + ')')
def timestamp():
now = time.time()
localtime = time.localtime(now)
return time.strftime('%Y%m%d%H%M%S', localtime)
def split_dialog_text(text):
ret = [''] * 3
txt = re.findall('.{1,60}(?:\W|$)', text)
for x in range(0, 2):
if len(txt) > x:
ret[x] = txt[x]
return ret
def reboot_counter(seconds=10, title=' '):
reboot_dlg = xbmcgui.DialogProgress()
reboot_dlg.create('LibreELEC %s' % title, ' ', ' ', ' ')
reboot_dlg.update(0)
wait_time = seconds
while seconds >= 0 and not reboot_dlg.iscanceled():
progress = round(1.0 * seconds / wait_time * 100)
reboot_dlg.update(int(progress), _(32329) % seconds)
time.sleep(1)
seconds = seconds - 1
if not reboot_dlg.iscanceled():
return 1
else:
return 0
def exit():
global WinOeSelect, winOeMain, __addon__, __cwd__, __oe__, _, dbusSystemBus, dictModules
dbusSystemBus.close()
dbusSystemBus = None
# del winOeMain
del dbusSystemBus
del dictModules
del __addon__
del __oe__
del _
# fix for xml printout
def fixed_writexml(self, writer, indent='', addindent='', newl=''):
writer.write(indent + '<' + self.tagName)
attrs = self._get_attributes()
a_names = list(attrs.keys())
a_names.sort()
for a_name in a_names:
writer.write(' %s="' % a_name)
minidom._write_data(writer, attrs[a_name].value)
writer.write('"')
if self.childNodes:
if len(self.childNodes) == 1 and self.childNodes[0].nodeType == minidom.Node.TEXT_NODE:
writer.write('>')
self.childNodes[0].writexml(writer, '', '', '')
writer.write('</%s>%s' % (self.tagName, newl))
return
writer.write('>%s' % newl)
for node in self.childNodes:
if node.nodeType is not minidom.Node.TEXT_NODE:
node.writexml(writer, indent + addindent, addindent, newl)
writer.write('%s</%s>%s' % (
indent,
self.tagName,
newl,
))
else:
writer.write('/>%s' % newl)
def parse_os_release():
os_release_fields = re.compile(r'(?!#)(?P<key>.+)=(?P<quote>[\'\"]?)(?P<value>.+)(?P=quote)$')
os_release_unescape = re.compile(r'\\(?P<escaped>[\'\"\\])')
try:
with open('/etc/os-release') as f:
info = {}
for line in f:
m = re.match(os_release_fields, line)
if m is not None:
key = m.group('key')
value = re.sub(os_release_unescape, r'\g<escaped>', m.group('value'))
info[key] = value
return info
except OSError:
return None
def get_os_release():
distribution = version = architecture = build = project = device = builder_name = builder_version = ''
os_release_info = parse_os_release()
if os_release_info is not None:
if 'NAME' in os_release_info:
distribution = os_release_info['NAME']
if 'VERSION_ID' in os_release_info:
version = os_release_info['VERSION_ID']
if 'VERSION' in os_release_info:
version = os_release_info['VERSION']
if 'LIBREELEC_ARCH' in os_release_info:
architecture = os_release_info['LIBREELEC_ARCH']
if 'LIBREELEC_BUILD' in os_release_info:
build = os_release_info['LIBREELEC_BUILD']
if 'LIBREELEC_PROJECT' in os_release_info:
project = os_release_info['LIBREELEC_PROJECT']
if 'LIBREELEC_DEVICE' in os_release_info:
device = os_release_info['LIBREELEC_DEVICE']
if 'BUILDER_NAME' in os_release_info:
builder_name = os_release_info['BUILDER_NAME']
if 'BUILDER_VERSION' in os_release_info:
builder_version = os_release_info['BUILDER_VERSION']
return (
distribution,
version,
architecture,
build,
project,
device,
builder_name,
builder_version
)
def hash_password(password):
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'),
salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
def verify_password(stored_password, provided_password):
salt = stored_password[:64]
stored_password = stored_password[64:]
pwdhash = hashlib.pbkdf2_hmac('sha512',
provided_password.encode('utf-8'),
salt.encode('ascii'),
100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash == stored_password
minidom.Element.writexml = fixed_writexml
############################################################################################
# Base Environment
############################################################################################
os_release_data = get_os_release()
DISTRIBUTION = os_release_data[0]
VERSION = os_release_data[1]
ARCHITECTURE = os_release_data[2]
BUILD = os_release_data[3]
PROJECT = os_release_data[4]
DEVICE = os_release_data[5]
BUILDER_NAME = os_release_data[6]
BUILDER_VERSION = os_release_data[7]
DOWNLOAD_DIR = '/storage/downloads'
XBMC_USER_HOME = os.environ.get('XBMC_USER_HOME', '/storage/.kodi')
CONFIG_CACHE = os.environ.get('CONFIG_CACHE', '/storage/.cache')
USER_CONFIG = os.environ.get('USER_CONFIG', '/storage/.config')
TEMP = '%s/temp/' % XBMC_USER_HOME
winOeMain = oeWindows.mainWindow('service-LibreELEC-Settings-mainWindow.xml', __cwd__, 'Default', oeMain=__oe__)
if os.path.exists('/etc/machine-id'):
SYSTEMID = load_file('/etc/machine-id')
else:
SYSTEMID = os.environ.get('SYSTEMID', '')
if PROJECT == 'RPi':
RPI_CPU_VER = execute('vcgencmd otp_dump 2>/dev/null | grep 30: | cut -c8', get_result=1).replace('\n','')
else:
RPI_CPU_VER = ''
BOOT_STATUS = load_file('/storage/.config/boot.status')
############################################################################################
try:
configFile = '%s/userdata/addon_data/service.libreelec.settings/oe_settings.xml' % XBMC_USER_HOME
if not os.path.exists('%s/userdata/addon_data/service.libreelec.settings' % XBMC_USER_HOME):
if os.path.exists('%s/userdata/addon_data/service.openelec.settings' % XBMC_USER_HOME):
shutil.copytree(('%s/userdata/addon_data/service.openelec.settings' % XBMC_USER_HOME),
('%s/userdata/addon_data/service.libreelec.settings' % XBMC_USER_HOME))
with open(configFile,'r+') as f:
xml = f.read()
xml = xml.replace("<openelec>","<libreelec>")
xml = xml.replace("</openelec>","</libreelec>")
f.seek(0)
f.write(xml)
f.truncate()
else:
os.makedirs('%s/userdata/addon_data/service.libreelec.settings' % XBMC_USER_HOME)
if not os.path.exists('%s/services' % CONFIG_CACHE):
os.makedirs('%s/services' % CONFIG_CACHE)
except:
pass