-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.py
486 lines (446 loc) · 16.6 KB
/
config.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
# -*- coding: utf-8 -*-
# Python modules imports
import argparse
import json
import os
import random
from xml.etree import ElementTree as ET
import polib
CWD_PATH = os.path.dirname(os.path.abspath(__file__))
def parse_settings_xml(settings_xml_filepath):
settings = {}
xml = ET.parse(settings_xml_filepath)
for child in xml.iter():
if child.tag == "setting":
if "id" in child.attrib:
value = ""
# print(child.attrib)
if "type" in child.attrib and child.attrib["type"] == "folder":
value = "/tmp"
if "default" in child.attrib:
value = child.attrib["default"]
settings[child.attrib["id"]] = value
return settings
def parse_strings_po(strings_po_filepath):
labels = {}
po = polib.pofile(strings_po_filepath)
for entry in po:
key = entry.msgctxt
key = int(key.replace("#", ""))
labels[key] = entry.msgid
return labels
class ConfigMC(type):
def __str__(cls):
s = "* Configuration:"
for k, v in cls._config.items():
s += "\n\t- {}: {}".format(k, str(v))
return s
class Config(metaclass=ConfigMC):
"""Singleton class to define user configuration"""
_config = {}
@classmethod
def get(cls, item):
return cls._config[item]
@classmethod
def init_config(cls):
parser = argparse.ArgumentParser(description="Catch-up TV & More simulator")
parser.add_argument(
"-a",
"--addon-path",
default="",
help="Path of plugin.video.catchuptvandmore",
)
parser.add_argument(
"-c",
"--config-file",
default="",
help="Optional JSON config file (if given, CLI args are not taken !!)",
)
parser.add_argument(
"-s",
"--console-size",
type=int,
default=160,
help="Your console size in order to compute the width of the fake Kodi menu [160]",
)
parser.add_argument(
"--test-modules",
action="store_true",
help="Try to load each Python files to detect errors",
)
parser.add_argument(
"--entry-point",
default="1",
help="Entry point of the simulation (separate items with dashes (e.g. '1-2-1-13')) [1]",
)
parser.add_argument(
"--exit-after-x-errors",
type=int,
default=-1,
help="Exit simulator after N errors encountered [-1]",
)
parser.add_argument(
"--disable-video-player",
action="store_true",
help="Do not open mpv on video selection",
)
parser.add_argument(
"--kodi-version",
default="LEIA",
choices=["LEIA", "KRYPTON", "JARVIS"],
help="Kodi version to simulate [LEIA]",
)
parser.add_argument(
"--print-all-explored-items",
action="store_true",
help="Print all explored items when exit the simulator",
)
parser.add_argument(
"--disable-image-check", action="store_true", help="Do not check image URL"
)
parser.add_argument(
"--autoreload-addon",
action="store_true",
help="Auto reload addon source files (usefull during dev)",
)
log_group = parser.add_argument_group("Logging")
log_group.add_argument(
"-l",
"--log-level",
default="info",
choices=["debug", "info", "warning", "error", "critical"],
help="Simulator log level",
)
log_group.add_argument(
"--kodi-log-level",
default="debug",
choices=[
"debug",
"info",
"notice",
"warning",
"error",
"severe",
"fatal",
"none",
],
help="Kodi log level",
)
log_group.add_argument(
"--disable-xbmcaddon-mock-log",
action="store_true",
help="Disable log messages of xbmcaddon module functions calls",
)
log_group.add_argument(
"--disable-xbmcplugin-mock-log",
action="store_true",
help="Disable log messages of xbmcplugin module functions calls",
)
log_group.add_argument(
"--disable-xbmc-mock-log",
action="store_true",
help="Disable log messages of xbmc module functions calls",
)
auto_exploration_group = parser.add_argument_group("Auto exploration mode")
auto_exploration_group.add_argument(
"--auto-exploration",
action="store_true",
help="Enable auto exploration of addon menus",
)
auto_exploration_group.add_argument(
"--max-items-per-menu",
type=int,
default=-1,
help="Limit the number of items to explore per menu",
)
auto_exploration_group.add_argument(
"--wait-time",
type=float,
default=1.0,
help="Time to wait between each menu during exploration [1sec]",
)
auto_exploration_group.add_argument(
"--max-items-to-explore",
type=int,
default=-1,
help="Limit the total number of item to explore",
)
auto_exploration_group.add_argument(
"--exploration-strategy",
default="RANDOM",
choices=["RANDOM", "FIRST", "LAST"],
help="How to add items of explored menus to the stack to the stack of item to explore",
)
auto_exploration_group.add_argument(
"--max-depth",
type=int,
default=-1,
help="Set the max depth to explore from the entry point",
)
auto_exploration_group.add_argument(
"--exclude-paths",
default="",
help="List of paths exclude from the exploration(e.g. '1-3, 1-2-1')",
)
auto_exploration_group.add_argument(
"--skip-playable-items",
action="store_true",
help="Do not add playable items to the exploration stack",
)
auto_exploration_group.add_argument(
"--timeout", type=int, default=-1, help="Auto exploration timeout [-1]"
)
cls._config = vars(parser.parse_args())
config_json = {}
# We have to check for a config file
if cls._config["config_file"] != "":
with open(cls._config["config_file"]) as f:
config_json = json.load(f)
# Check json dict
for k in config_json.keys():
if k not in cls._config and k != "_comment":
raise Exception(
'The key "{}" in your json file is invalid'.format(k)
)
cls._config[k] = config_json[k]
# We get the full path of the addon path
if cls._config["addon_path"] == "":
raise Exception(
"You need to specify the path of plugin.video.catchuptvandmore (see --help)"
)
cls._config["addon_path"] = os.path.abspath(cls._config["addon_path"])
cls._config["addon_id"] = "plugin.video.catchuptvandmore"
cls._config["addon_fanart_filepath"] = os.path.join(
cls._config["addon_path"], "fanart.jpg"
)
cls._config["addon_icon_filepath"] = os.path.join(
cls._config["addon_path"], "icon.png"
)
cls._config["addon_en_strings_po_filepath"] = os.path.join(
cls._config["addon_path"],
"resources",
"language",
"resource.language.en_gb",
"strings.po",
)
cls._config["codequick_path"] = os.path.join(
CWD_PATH, "libs", "script.module.codequick", "lib"
)
cls._config["codequick_addon_path"] = os.path.join(
CWD_PATH, "libs", "script.module.codequick"
)
cls._config["codequick_fanart_filepath"] = os.path.join(
cls._config["codequick_addon_path"], "fanart.jpg"
)
cls._config["codequick_icon_filepath"] = os.path.join(
cls._config["codequick_addon_path"], "icon.png"
)
cls._config["codequick_en_strings_po_filepath"] = os.path.join(
cls._config["codequick_addon_path"],
"resources",
"language",
"resource.language.en_gb",
"strings.po",
)
cls._config["inputstreamhelper_path"] = os.path.join(
CWD_PATH, "libs", "script.module.inputstreamhelper", "lib"
)
cls._config["inputstreamhelper_addon_path"] = os.path.join(
CWD_PATH, "libs", "script.module.inputstreamhelper"
)
cls._config["inputstreamhelper_fanart_filepath"] = os.path.join(
cls._config["inputstreamhelper_addon_path"], "fanart.jpg"
)
cls._config["inputstreamhelper_icon_filepath"] = os.path.join(
cls._config["inputstreamhelper_addon_path"], "icon.png"
)
cls._config["inputstreamhelper_en_strings_po_filepath"] = os.path.join(
cls._config["inputstreamhelper_addon_path"],
"resources",
"language",
"resource.language.en_gb",
"strings.po",
)
cls._config["youtubedl_path"] = os.path.join(
CWD_PATH, "libs", "script.module.youtube.dl", "lib"
)
cls._config["youtubedl_addon_path"] = os.path.join(
CWD_PATH, "libs", "script.module.youtube.dl"
)
cls._config["youtubedl_fanart_filepath"] = ""
cls._config["youtubedl_icon_filepath"] = os.path.join(
cls._config["youtubedl_addon_path"], "icon.png"
)
cls._config["youtubedl_en_strings_po_filepath"] = os.path.join(
cls._config["youtubedl_addon_path"],
"resources",
"language",
"English",
"strings.po",
)
cls._config["kodi-six_path"] = os.path.join(
CWD_PATH, "libs", "script.module.kodi-six", "libs"
)
cls._config["pyqrcode_path"] = os.path.join(
CWD_PATH, "libs", "script.module.pyqrcode", "lib"
)
cls._config["tzlocal_path"] = os.path.join(
CWD_PATH, "libs", "script.module.tzlocal", "lib"
)
cls._config["fake_xbmc_modules_path"] = os.path.join(
CWD_PATH, "libs", "fake_xbmc_modules"
)
# Fake Kodi/userdata/addon_data/plugin.video.catchuptvandmore
cls._config["userdata_path"] = os.path.join(CWD_PATH, "fake_userdata")
# We convert the entry point to a list
entry_point = []
for item_key in cls._config["entry_point"].split("-"):
entry_point.append(item_key)
cls._config["entry_point"] = entry_point
# We convert the exclude string to a list of list
exclude_paths = []
if cls._config["exclude_paths"] != "":
exclude_paths_s = "".join(cls._config["exclude_paths"].split())
for exclude in exclude_paths_s.split(","):
exclude_path = []
for item in exclude.split("-"):
exclude_path.append(int(item))
exclude_paths.append(exclude_path)
cls._config["exclude_paths"] = exclude_paths
# We move max depth according to the entry point
if cls._config["max_depth"] != -1:
cls._config["max_depth"] = (
cls._config["max_depth"] + len(cls._config["entry_point"]) - 1
)
# Parse settings.xml files
addon_settings_filepath = os.path.join(
Config.get("addon_path"), "resources", "settings.xml"
)
# Choose a random languauge
cls._config["addon_settings"] = parse_settings_xml(addon_settings_filepath)
cls._config["addon_settings"]["arte.language"] = random.choice(
["FR", "DE", "EN", "ES", "PL", "IT"]
)
cls._config["addon_settings"]["euronews.language"] = random.choice(
["FR", "EN", "AR", "DE", "IT", "ES", "PT", "RU", "TR", "FA", "GR", "HU"]
)
cls._config["addon_settings"]["france24.language"] = random.choice(
["FR", "EN", "AR", "ES"]
)
cls._config["addon_settings"]["mtv.language"] = random.choice(["FR"])
cls._config["addon_settings"]["dw.language"] = random.choice(
["EN", "DE", "ES", "AR"]
)
cls._config["addon_settings"]["beinsports.language"] = random.choice(
["AU", "AR", "EN", "FR", "US", "ES", "NZ", "HK", "PH", "TH", "ID", "MY"]
)
cls._config["addon_settings"]["qvc.language"] = random.choice(
["JP", "DE", "IT", "UK", "US"]
)
cls._config["addon_settings"]["nhkworld.language"] = random.choice(
["Outside Japan", "In Japan"]
)
cls._config["addon_settings"]["cgtn.language"] = random.choice(
["FR", "EN", "AR", "ES", "RU"]
)
cls._config["addon_settings"]["paramountchannel.language"] = random.choice(
["ES", "IT"]
)
cls._config["addon_settings"]["rt.language"] = random.choice(
["FR", "EN", "AR", "ES"]
)
cls._config["addon_settings"]["france3regions.language"] = random.choice(
[
"Alpes",
"Alsace",
"Aquitaine",
"Auvergne",
"Bourgogne",
"Bretagne",
"Centre-Val de Loire",
"Chapagne-Ardenne",
"Corse",
"Côte d'Azur",
"Franche-Compté",
"Languedoc-Roussillon",
"Limousin",
"Lorraine",
"Midi-Pyrénées",
"Nord-Pas-de-Calais",
"Basse-Normandie",
"Haute-Normandie",
"Paris Île-de-France",
"Pays de la Loire",
"Picardie",
"Poitou-Charentes",
"Provence-Alpes",
"Rhône-Alpes",
]
)
cls._config["addon_settings"]["la_1ere.language"] = random.choice(
[
"Guadeloupe",
"Guyane",
"Martinique",
"Mayotte",
"Nouvelle Calédonie",
"Polynésie",
"Réunion",
"St-Pierre et Miquelon",
"Wallis et Futuna",
"Outre-mer",
]
)
cls._config["addon_settings"]["realmadridtv.language"] = random.choice(
["ES", "EN"]
)
cls._config["addon_settings"]["tvp3.language"] = random.choice(
[
"Białystok",
"Bydgoszcz",
"Gdańsk",
"Gorzów Wielkopolski",
"Katowice",
"Kielce",
"Kraków",
"Lublin",
"Łódź",
"Olsztyn",
"Opole",
"Poznań",
"Rzeszów",
"Szczecin",
"Warszawa",
"Wrocław",
]
)
cls._config["addon_settings"]["tv_guide"] = "false"
cls._config["codequick_fake_settings"] = {}
inputstreamhelper_settings_filepath = os.path.join(
Config.get("inputstreamhelper_addon_path"), "resources", "settings.xml"
)
cls._config["inputstreamhelper_fake_settings"] = parse_settings_xml(
inputstreamhelper_settings_filepath
)
youtubedl_settings_filepath = os.path.join(
Config.get("youtubedl_addon_path"), "resources", "settings.xml"
)
cls._config["youtubedl_fake_settings"] = parse_settings_xml(
youtubedl_settings_filepath
)
# Parse english strings.po files
cls._config["addon_labels"] = parse_strings_po(
Config.get("addon_en_strings_po_filepath")
)
cls._config["codequick_labels"] = parse_strings_po(
Config.get("codequick_en_strings_po_filepath")
)
cls._config["inputstreamhelper_labels"] = parse_strings_po(
Config.get("inputstreamhelper_en_strings_po_filepath")
)
cls._config["youtubedl_labels"] = parse_strings_po(
Config.get("youtubedl_en_strings_po_filepath")
)
cls._config["xbmc_labels"] = parse_strings_po(
os.path.join(CWD_PATH, "libs", "fake_xbmc_modules", "strings.po")
)