-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
80 lines (66 loc) · 2.99 KB
/
config.py
File metadata and controls
80 lines (66 loc) · 2.99 KB
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
import configparser
import re
class LinkAction:
def __init__(self, pattern, program, label, filter_name=None, filters=None, run_in_shell=False, autorun=False):
self.pattern = re.compile(pattern)
self.program = program
self.label = label
self.filter_name = filter_name
self.filters = filters or {}
self.run_in_shell = run_in_shell
self.autorun = autorun
def filter_url(self, url):
if self.filter_name and self.filter_name in self.filters:
pattern, replace = self.filters[self.filter_name]
return pattern.sub(replace, url)
return url
def load_config(config_path='config.ini'):
config = configparser.ConfigParser()
config.read(config_path)
# Carica le label da [DICT_REGEX]
dict_regexs_labels = {}
if 'DICT_REGEX' in config:
dict_regexs_labels = dict(config['DICT_REGEX'])
# Carica le label da [DICT_PROGRAMS]
dict_programs_labels = {}
if 'DICT_PROGRAMS' in config:
dict_programs_labels = dict(config['DICT_PROGRAMS'])
# Carica i filtri da [FILTERS]
filters = {}
if 'FILTERS' in config:
for key in config['FILTERS']:
if key.endswith('_pattern'):
filter_name = key[:-8] # togli '_pattern'
pattern = config['FILTERS'][key]
replace = config['FILTERS'].get(f'{filter_name}_replace', '')
filters[filter_name] = (re.compile(pattern), replace)
actions = []
for key in config['LINKS']:
if key.startswith('regex'):
num = key.replace('regex', '')
pattern_raw = config['LINKS'][key]
pattern = substitute_label(pattern_raw, dict_regexs_labels)
# Leggi programN e sostituisci eventuali label
prog_raw = config['LINKS'].get(f'program{num}', '')
program = substitute_label(prog_raw, dict_programs_labels)
# Leggi labelN (testo pulsante), fallback a nome programma o 'Azione'
label_raw = config['LINKS'].get(f'label{num}', '')
label = label_raw if label_raw else (program.split()[0] if program else 'Azione')
filter_name = config['LINKS'].get(f'filter{num}', None)
run_in_shell = config['LINKS'].getboolean(f'run_in_shell{num}', fallback=False)
autorun = config['LINKS'].getboolean(f'autorun{num}', fallback=False)
actions.append(LinkAction(pattern, program, label, filter_name, filters, run_in_shell, autorun))
return actions
def substitute_label(value, labels):
"""
Se value inizia con una label definita in labels, sostituisci la parte label_XXX
con il valore corrispondente, mantenendo il resto della stringa.
Esempio:
value = "label_yt {url}"
labels['label_yt'] = "C:\\path\\yt-dlp.exe"
ritorna "C:\\path\\yt-dlp.exe {url}"
"""
for label_key, label_val in labels.items():
if value.startswith(label_key):
return value.replace(label_key, label_val, 1)
return value