1
1
import atexit
2
2
import json
3
3
import logging
4
+ import re
4
5
from typing import Any , Dict , List
5
6
6
- defaultConfig = {"languageCode" : "en-US" , "voiceId" : "Amy" , "wikiLanguage" : "en" }
7
+ DEFAULT_CONFIG = {"languageCode" : "en-US" , "voiceId" : "Amy" , "wikiLanguage" : "en" }
7
8
8
- default_configfile_name = "discordbot_config.json"
9
+ DEFAULT_CONFIGFILE_NAME = "discordbot_config.json"
9
10
10
11
12
+ # See https://docs.aws.amazon.com/de_de/polly/latest/dg/voicelist.html
13
+ VALID_T2S_LANGUGAGE_CODES = ['arb' ,'cmn-CN' ,'cy-GB' ,'da-DK' ,'de-DE' ,'en-AU' ,'en-GB' ,'en-GB-WLS' ,'en-IN' ,'en-US' ,'es-ES' ,'es-MX' ,'es-US' ,'fr-CA' ,'fr-FR' ,'is-IS' ,'it-IT' ,'ja-JP' ,'hi-IN' ,'ko-KR' ,'nb-NO' ,'nl-NL' ,'pl-PL' ,'pt-BR' ,'pt-PT' ,'ro-RO' ,'ru-RU' ,'sv-SE' ,'tr-TR' ,'en-NZ' ,'en-ZA' ]
14
+ VALID_T2S_VOICE_IDS = ['Aditi' ,'Amy' ,'Astrid' ,'Bianca' ,'Brian' ,'Camila' ,'Carla' ,'Carmen' ,'Celine' ,'Chantal' ,'Conchita' ,'Cristiano' ,'Dora' ,'Emma' ,'Enrique' ,'Ewa' ,'Filiz' ,'Gabrielle' ,'Geraint' ,'Giorgio' ,'Gwyneth' ,'Hans' ,'Ines' ,'Ivy' ,'Jacek' ,'Jan' ,'Joanna' ,'Joey' ,'Justin' ,'Karl' ,'Kendra' ,'Kevin' ,'Kimberly' ,'Lea' ,'Liv' ,'Lotte' ,'Lucia' ,'Lupe' ,'Mads' ,'Maja' ,'Marlene' ,'Mathieu' ,'Matthew' ,'Maxim' ,'Mia' ,'Miguel' ,'Mizuki' ,'Naja' ,'Nicole' ,'Olivia' ,'Penelope' ,'Raveena' ,'Ricardo' ,'Ruben' ,'Russell' ,'Salli' ,'Seoyeon' ,'Takumi' ,'Tatyana' ,'Vicki' ,'Vitoria' ,'Zeina' ,'Zhiyu' ,'Aria' ,'Ayanda' ]
15
+
16
+ INPUT_VALIDATION_RAW_RE = r"^[a-zA-Z]{2}$"
17
+ INPUT_VALIDATION_STR = re .compile (INPUT_VALIDATION_RAW_RE )
18
+
19
+ VALID_CONFIG_PARAM_KEYS = {
20
+ "languageCode" : {
21
+ "description" : "The language code to use for the Polly API. See https://docs.aws.amazon.com/de_de/polly/latest/dg/voicelist.html" ,
22
+ "valid_inputs" : lambda val : val in VALID_T2S_LANGUGAGE_CODES
23
+ },
24
+ "voiceId" : {
25
+ "description" : "The voice ID to use for the Polly API. See https://docs.aws.amazon.com/de_de/polly/latest/dg/voicelist.html" ,
26
+ "valid_inputs" : lambda val : val in VALID_T2S_VOICE_IDS
27
+ },
28
+ "wikiLanguage" : {
29
+ "description" : f"The language code to use for the Wikipedia API. Must match '{ INPUT_VALIDATION_RAW_RE } '" ,
30
+ "valid_inputs" : lambda val : INPUT_VALIDATION_STR .match (val )
31
+ }
32
+ }
33
+
34
+ class ConfigError (Exception ):
35
+ pass
36
+
11
37
class ConfigMap :
12
38
def __init__ (self , configs : List [Dict [str , Any ]], configfile_name : str = None ):
13
39
self .log = logging .getLogger ("config" )
14
40
self ._configs = dict ()
15
- self .configfile_name = configfile_name or default_configfile_name
41
+ self .configfile_name = configfile_name or DEFAULT_CONFIGFILE_NAME
16
42
17
43
for cfg in configs :
18
44
id = cfg ["id" ]
@@ -35,10 +61,12 @@ def get_config_for(self, id: str, key: str = "", default: Any = None):
35
61
)
36
62
37
63
def add_config_for (self , id : str , config : Dict [str , Any ]):
64
+ for key in config :
65
+ self .is_valid_config_parameter (key , config [key ])
38
66
self ._configs [id ] = config
39
67
40
68
def update_config_for (self , id : str , key : str , val : str ):
41
- if self .exists (id ):
69
+ if self .exists (id ) and self . is_valid_config_parameter ( key , val ) :
42
70
self ._configs [id ][key ] = val
43
71
44
72
def remove_config_for (self , id : str ):
@@ -48,11 +76,20 @@ def remove_config_for(self, id: str):
48
76
def set_defaults_for (self , id : str ):
49
77
if not self .exists (id ):
50
78
self .log .info ("Setting default config" )
51
- self .add_config_for (id , defaultConfig )
52
- pass
79
+ self .add_config_for (id , DEFAULT_CONFIG )
80
+ self .log .info (f'Config for { id } already exists. Cannot set defaults...' )
81
+
82
+ def is_valid_config_parameter (self , key : str , val : str ) -> bool :
83
+ if not key in VALID_CONFIG_PARAM_KEYS .keys ():
84
+ raise ConfigError (f"'{ key } ' is not a valid config parameter. Try one of { VALID_CONFIG_PARAM_KEYS } " )
85
+ else :
86
+ val_info = VALID_CONFIG_PARAM_KEYS [key ]
87
+ if not val_info ["valid_inputs" ](val ):
88
+ raise ConfigError (f"'{ val } ' is not a valid value for '{ key } ': { val_info ['description' ]} " )
89
+ return True
53
90
54
91
@classmethod
55
- def from_file (cls , configfile_name : str = default_configfile_name ):
92
+ def from_file (cls , configfile_name : str = DEFAULT_CONFIGFILE_NAME ):
56
93
try :
57
94
with open (configfile_name , "r" ) as f :
58
95
data = json .load (f )
0 commit comments