-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
869 lines (737 loc) · 34.5 KB
/
Copy pathconfig.py
File metadata and controls
869 lines (737 loc) · 34.5 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
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
"""
Modul zur Verwaltung der App-Konfiguration.
Verantwortlichkeiten:
- Laden von Umgebungsvariablen und Streamlit-Secrets.
- Laden der globalen App-Konfiguration (`mc_test_config.json`).
- Laden der Fragensets (`questions_*.json`).
"""
import os
import sys
import json
import math
import re
from dataclasses import dataclass
from pathlib import Path
from typing import List, Dict, Any
import streamlit as st
from helpers.text import sanitize_html, normalize_detailed_explanation
from i18n import DEFAULT_LOCALE, normalize_locale
from i18n.context import get_locale, t as translate_ui
def _identity_cache_decorator(func=None, **dec_kwargs):
"""Fallback, wenn Streamlit kein cache_data/cache_resource kennt.
Unterstützt Aufrufe wie `@st.cache_data` und `@st.cache_data(ttl=3600)`.
"""
def make_wrapper(f):
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
wrapper.clear = lambda: None
return wrapper
if callable(func):
return make_wrapper(func)
return make_wrapper
if not hasattr(st, "cache_data"):
st.cache_data = _identity_cache_decorator # type: ignore[attr-defined]
if not hasattr(st, "cache_resource"):
st.cache_resource = _identity_cache_decorator # type: ignore[attr-defined]
def _make_streamlit_noop(name: str):
def _noop(*args, **kwargs):
return None
_noop.__name__ = f"streamlit_noop_{name}"
return _noop
for _attr in ("error", "warning", "info", "success"):
if not hasattr(st, _attr):
setattr(st, _attr, _make_streamlit_noop(_attr))
USER_QUESTION_PREFIX = "user::"
def get_package_dir() -> str:
"""Gibt das Verzeichnis des Pakets zurück."""
# Gibt das Verzeichnis zurück, in dem diese Datei (config.py) liegt.
# Dies ist der robusteste Weg, um das Hauptverzeichnis der App zu finden.
# __file__ ist der Pfad zur aktuellen Datei. os.path.dirname() gibt das Verzeichnis davon zurück.
# Beispiel: /path/to/your/project/streamlit/config.py -> /path/to/your/project/streamlit
return os.path.abspath(os.path.dirname(__file__))
@dataclass
class QuestionSet:
"""
Repräsentiert ein geladenes Fragenset inklusive Metadaten.
Diese Klasse verhält sich in weiten Teilen wie eine Liste von Fragen, stellt
aber zusätzlich Zugriff auf Metadaten (z.B. empfohlene Testdauer) bereit.
"""
questions: List[Dict[str, Any]]
meta: Dict[str, Any]
source_filename: str | None = None
def __iter__(self):
return iter(self.questions)
def __len__(self) -> int:
return len(self.questions)
def __getitem__(self, index: int) -> Dict[str, Any]:
return self.questions[index]
def __bool__(self) -> bool:
return bool(self.questions)
def index(self, question: Dict[str, Any]) -> int:
return self.questions.index(question)
def get_test_duration_minutes(self, default_minutes: int) -> int:
"""
Liefert die für dieses Set empfohlene Testdauer in Minuten.
Priorität:
1. Explizit in den Metadaten gesetzter Wert.
2. Automatisch berechnete Empfehlung (`computed_test_duration_minutes`).
3. Übergebener Default (AppConfig).
"""
explicit = self.meta.get("test_duration_minutes")
if isinstance(explicit, (int, float)) and explicit > 0:
return _round_duration_minutes(explicit)
computed = self.meta.get("computed_test_duration_minutes")
if isinstance(computed, (int, float)) and computed > 0:
return _round_duration_minutes(computed)
return _round_duration_minutes(default_minutes)
def _infer_title_from_filename(filename: str | None) -> str:
if not filename:
return ""
name = filename.replace("questions_", "").replace(".json", "")
return name.replace("_", " ").strip()
def _safe_int(value, default: int = 1) -> int:
try:
return int(value)
except (TypeError, ValueError):
return default
def _normalize_minutes(value) -> float | None:
try:
minutes = float(value)
except (TypeError, ValueError):
return None
if minutes <= 0:
return None
return minutes
def _round_duration_minutes(minutes: float) -> int:
"""
Rundet eine Zeitangabe in Minuten auf sinnvolle Werte.
- Unter 10 Minuten wird aufgerundet (Ceiling), Mindestwert 5 Minuten.
- Ab 10 Minuten wird auf das nächste Vielfache von 5 Minuten gerundet.
"""
if minutes <= 0:
return 5
if minutes < 10:
return max(5, int(math.ceil(minutes)))
return max(10, int(5 * round(minutes / 5)))
def _compute_difficulty_profile(questions: List[Dict[str, Any]]) -> Dict[str, int]:
profile = {"leicht": 0, "mittel": 0, "schwer": 0}
for q in questions:
gewichtung = _safe_int(q.get("gewichtung"), 1)
if gewichtung >= 3:
profile["schwer"] += 1
elif gewichtung == 2:
profile["mittel"] += 1
else:
profile["leicht"] += 1
# Filtere Einträge mit 0 heraus, um die Ausgabe kompakter zu halten.
return {k: v for k, v in profile.items() if v > 0}
def _compute_recommended_duration_minutes(
meta: Dict[str, Any], questions: List[Dict[str, Any]]
) -> int:
"""
Berechnet eine empfohlene Testdauer basierend auf Anzahl und Schwierigkeit der Fragen.
Die Berechnung nutzt folgende Logik:
- Explizit gesetzte Minuten im Meta-Objekt haben Vorrang.
- Optional kann `time_per_question_minutes` oder `time_per_weight_minutes` angegeben werden.
- Falls nichts gesetzt ist, wird eine Heuristik basierend auf der Gewichtung verwendet.
"""
explicit = _normalize_minutes(meta.get("test_duration_minutes"))
if explicit:
return _round_duration_minutes(explicit)
per_question = _normalize_minutes(meta.get("time_per_question_minutes"))
per_weight_raw = meta.get("time_per_weight_minutes", {})
per_weight: Dict[int, float] = {}
if isinstance(per_weight_raw, dict):
for key, value in per_weight_raw.items():
key_int = None
if isinstance(key, int):
key_int = key
elif isinstance(key, str) and key.strip().isdigit():
key_int = int(key.strip())
if key_int is None:
continue
normalized = _normalize_minutes(value)
if normalized:
per_weight[key_int] = normalized
buffer_minutes = _normalize_minutes(meta.get("additional_buffer_minutes")) or 0.0
# Default-Heuristik pro Gewichtung (in Minuten)
default_weight_minutes = {
1: 0.5, # leichte Fragen (30 Sekunden)
2: 0.75, # mittlere Fragen (45 Sekunden)
3: 1.0, # schwere Fragen (60 Sekunden)
}
total_minutes = 0.0
for q in questions:
gewichtung = _safe_int(q.get("gewichtung"), 1)
time_for_weight = per_weight.get(gewichtung)
if time_for_weight is not None:
minutes_for_question = time_for_weight
elif per_question is not None:
minutes_for_question = per_question
else:
minutes_for_question = default_weight_minutes.get(gewichtung, 0.75)
total_minutes += minutes_for_question
total_minutes += buffer_minutes
# Mindestens 5 Minuten, um realistische Werte zu gewährleisten.
return _round_duration_minutes(max(5, total_minutes))
def _build_question_set(
data: Any, filename: str, silent: bool = False
) -> QuestionSet:
"""
Normalisiert den Rohinhalt einer Fragen-Datei und liefert ein `QuestionSet`.
"""
meta: Dict[str, Any] = {}
raw_questions: Any = []
def _sanitize_text(value: Any, context: str) -> str:
if not isinstance(value, str):
return ""
# Protect LaTeX/math regions from HTML escaping (e.g. $...$, $$...$$, \(...\), \[...\])
math_pattern = re.compile(r'(\$\$.*?\$\$|\$.*?\$|\\\\\[.*?\\\\\]|\\\\\(.*?\\\\\))', re.DOTALL)
placeholders: dict[str, str] = {}
def _math_repl(m):
idx = len(placeholders)
key = f"__MATH_PLACEHOLDER_{idx}__"
placeholders[key] = m.group(0)
return key
text_with_placeholders = math_pattern.sub(_math_repl, value)
sanitized, modified = sanitize_html(text_with_placeholders)
sanitized = sanitized.strip()
# Restore protected math placeholders (they were not passed through the HTML escaper)
for key, original_math in placeholders.items():
sanitized = sanitized.replace(key, original_math)
# Math/LaTeX uses ampersands (e.g. pmatrix). Preserve them after sanitizing.
if "&" in sanitized:
sanitized = sanitized.replace("&", "&")
# Remove any trailing contentReference markers that may have been
# appended by external importers (they are internal annotations
# and should not be shown in the UI). Example pattern:
# ":contentReference[oaicite:0]{index=0}"
try:
sanitized = re.sub(r"\s*:contentReference\[[^\]]*\]\{[^}]*\}\s*$", "", sanitized)
except Exception:
# Be defensive: if regex fails for unexpected input, leave text as-is
pass
# Remove invisible/zero-width characters which sometimes get pasted
# into questions from external editors and can break layout
# (U+200B ZERO WIDTH SPACE, U+200C ZERO WIDTH NON-JOINER, U+200D ZERO WIDTH JOINER).
for zw in ("\u200b", "\u200c", "\u200d"):
if zw in sanitized:
sanitized = sanitized.replace(zw, "")
if modified and not silent:
st.warning(
translate_ui("config.warning.html_sanitized", default="In '{filename}' wurde potenziell unsichere HTML-Auszeichnung entfernt ({context}).").format(filename=filename, context=context)
)
return sanitized
def _sanitize_nested(value: Any, context: str) -> Any:
if isinstance(value, str):
return _sanitize_text(value, context)
if isinstance(value, list):
sanitized_list = []
for idx, item in enumerate(value, start=1):
sanitized_list.append(_sanitize_nested(item, f"{context}[{idx}]"))
return sanitized_list
if isinstance(value, dict):
sanitized_dict: Dict[str, Any] = {}
for key, nested_value in value.items():
sanitized_dict[key] = _sanitize_nested(nested_value, f"{context}.{key}")
return sanitized_dict
return value
if isinstance(data, dict):
raw_questions = data.get("questions", [])
meta_candidate = data.get("meta") or {}
if isinstance(meta_candidate, dict):
meta = dict(meta_candidate)
else:
if not silent:
st.warning(
translate_ui("config.warning.metadata_ignored", default="Metadaten in '{filename}' wurden ignoriert, da sie nicht als Objekt vorliegen.").format(filename=filename)
)
elif isinstance(data, list):
raw_questions = data
meta = {}
else:
raise ValueError(translate_ui("config.error.invalid_json_format", default="Ungültiges JSON-Format: Erwartet Objekt mit 'questions' oder eine Liste."))
if not isinstance(raw_questions, list):
raise ValueError(translate_ui("config.error.json_list_required", default="Ungültiges JSON-Format: Das Feld 'questions' muss eine Liste enthalten."))
questions: List[Dict[str, Any]] = []
for i, raw_question in enumerate(raw_questions):
if not isinstance(raw_question, dict):
if not silent:
st.warning(
translate_ui("config.warning.question_skipped", default="Frage an Position {pos} in '{filename}' wurde übersprungen, da sie kein Objekt ist.").format(pos=i + 1, filename=filename)
)
continue
question = dict(raw_question)
# Load and sanitize using canonical English keys only; temporary
# compatibility for legacy German keys has been removed.
# Accept legacy 'frage' as a source when 'question' is missing to
# preserve backwards compatibility for older datasets.
frage_text = _sanitize_text(question.get("question") or question.get("frage", ""), f"Frage {i + 1}: question")
if isinstance(frage_text, str) and frage_text and frage_text[0].isdigit():
dot_pos = frage_text.find(".")
if 0 < dot_pos < len(frage_text) - 1 and frage_text[:dot_pos].isdigit():
frage_text = frage_text.split(".", 1)[-1].strip()
question["question"] = f"{i + 1}. {frage_text}".strip()
# Keep legacy German alias in sync so older callsites that read
# 'frage' continue to observe the normalized, renumbered text.
try:
question["frage"] = question["question"]
except Exception:
pass
if "topic" in question:
question["topic"] = _sanitize_text(
question.get("topic", ""), f"Frage {i + 1}: topic"
)
if "explanation" in question:
question["explanation"] = _sanitize_nested(
question.get("explanation"), f"Frage {i + 1}: explanation"
)
optionen_raw = question.get("options")
if isinstance(optionen_raw, list):
sanitized_options = []
for opt_idx, opt in enumerate(optionen_raw, start=1):
sanitized_options.append(
_sanitize_text(opt, f"Frage {i + 1}: Option {opt_idx}")
)
question["options"] = sanitized_options
# If 'answer' is provided as text, try to resolve it to an index
# using the sanitized options list.
try:
ans_val = question.get("answer")
opts = question.get("options")
if not isinstance(ans_val, int) and isinstance(ans_val, str) and isinstance(opts, list):
# exact match first
try:
idx = opts.index(ans_val)
question["answer"] = idx
except ValueError:
stripped = ans_val.strip()
for idx2, opt in enumerate(opts):
if isinstance(opt, str) and opt.strip() == stripped:
question["answer"] = idx2
break
except Exception:
pass
mini_glossary = question.get("mini_glossary")
if isinstance(mini_glossary, dict):
sanitized_glossary: Dict[str, Any] = {}
for term, definition in mini_glossary.items():
sanitized_term = _sanitize_text(str(term), f"Frage {i + 1}: Begriff")
sanitized_definition = _sanitize_text(
str(definition), f"Frage {i + 1}: Begriffserklärung"
)
sanitized_glossary[sanitized_term] = sanitized_definition
question["mini_glossary"] = sanitized_glossary
# Backwards-compatibility: populate German alias keys if missing so
# older code paths that still reference German keys continue to work.
alias_map = {
"question": "frage",
"weight": "gewichtung",
"topic": "thema",
"cognitive_level": "kognitive_stufe",
"options": "optionen",
"answer": "loesung",
"explanation": "erklaerung",
}
for eng, ger in alias_map.items():
try:
if eng in question and ger not in question:
question[ger] = question[eng]
except Exception:
# Non-fatal: continue even if assignment fails for odd objects
pass
# Normalize any extended / detailed explanation into a canonical
# object shape so all renderers (PDF, UI, exporters) can rely on
# a consistent structure. Accept legacy keys as well.
try:
raw_ext = (
question.get("extended_explanation")
or question.get("detailed_explanation")
or question.get("erklaerung_detailliert")
)
normalized_ext = normalize_detailed_explanation(raw_ext)
if normalized_ext is not None:
question["extended_explanation"] = normalized_ext
except Exception:
# Don't fail loading for normalization errors; keep original value
pass
questions.append(question)
meta.setdefault("title", _infer_title_from_filename(filename))
meta = _sanitize_nested(meta, f"Meta in '{filename}'") if meta else meta
meta["question_count"] = len(questions)
meta["difficulty_profile"] = _compute_difficulty_profile(questions)
meta["computed_test_duration_minutes"] = _compute_recommended_duration_minutes(meta, questions)
# Explizit gesetzte Testdauer auf Integer normalisieren
if "test_duration_minutes" in meta:
explicit = _normalize_minutes(meta["test_duration_minutes"])
if explicit:
meta["test_duration_minutes"] = _round_duration_minutes(explicit)
else:
# Ungültige Einträge entfernen, damit die Berechnung greifen kann.
del meta["test_duration_minutes"]
return QuestionSet(questions=questions, meta=meta, source_filename=filename)
class AppConfig:
"""Eine Klasse zur Kapselung der App-Konfiguration."""
def __init__(self):
self.admin_user: str = ""
self.admin_key: str = ""
self.scoring_mode: str = "positive_only"
self.show_top5_public: bool = True
self.test_duration_minutes: int = 60
# Hours after which temporary uploaded question sets are considered stale
# and can be cleaned up from the welcome page. Default: 24 hours.
self.user_qset_cleanup_hours: int = 24
# Days to keep temporary user question sets for reserved pseudonyms
# Default: 14 days
self.user_qset_reserved_retention_days: int = 14
# Automatically release unreserved pseudonyms that have no sessions
# when the welcome page runs cleanup. Default: True (enabled).
self.auto_release_unreserved_pseudonyms: bool = True
# Hours after which an inactive, unreserved pseudonym is released.
# Default: 24 hours to prevent race conditions with active sessions.
self.pseudonym_release_hours: int = 24
# Recovery / policy defaults
self.recovery_min_length: int = 6
self.recovery_allow_short: bool = False
self.rate_limit_attempts: int = 3
self.rate_limit_window_minutes: int = 5
# Normalization factor for the "next" cooldown extras (explanation/extended)
# Default: 1.0 (no scaling). Values < 1.0 reduce the extra penalty empirically.
self.next_cooldown_normalization_factor: float = 0.3
# Cooldown configuration for reading time (base seconds per weight, scaled by options)
self.reading_cooldown_base_per_weight: Dict[int, float] = {1: 15.0, 2: 25.0, 3: 35.0} # seconds
# Cooldown configuration for next button (extra seconds for explanations)
self.next_cooldown_extra_standard: int = 7 # seconds for explanation
self.next_cooldown_extra_extended: int = 15 # seconds for extended explanation
# Threshold for panic mode (seconds per remaining question)
self.panic_mode_threshold_seconds: int = 15
self._load_from_env_and_secrets()
self._load_from_json()
def _load_from_env_and_secrets(self):
"""Lädt Konfiguration aus Streamlit Secrets und Umgebungsvariablen."""
try:
# Streamlit Secrets haben Vorrang
self.admin_user = st.secrets.get("MC_TEST_ADMIN_USER", "").strip()
self.admin_key = st.secrets.get("MC_TEST_ADMIN_KEY", "").strip()
except Exception:
pass
if not self.admin_user:
self.admin_user = os.getenv("MC_TEST_ADMIN_USER", "").strip()
if not self.admin_key:
self.admin_key = os.getenv("MC_TEST_ADMIN_KEY", "").strip()
# Note: legacy global answer rate-limit removed. Per-question dynamic
# cooldowns are handled in the UI flow (main_view). If you need a
# global rate limit again, reintroduce via a dedicated config key.
test_duration_str = ""
try:
test_duration_str = st.secrets.get("MC_TEST_DURATION_MINUTES", "").strip()
except Exception:
pass
if not test_duration_str:
test_duration_str = os.getenv("MC_TEST_DURATION_MINUTES", "").strip()
if test_duration_str:
try:
parsed_minutes = int(test_duration_str)
if parsed_minutes > 0:
self.test_duration_minutes = parsed_minutes
except ValueError:
pass # Belasse Defaultwert bei ungültiger Eingabe
# Optional: cleanup hours for temporary user question sets (secrets/env)
# First try Streamlit secrets (may be empty), then fall back to environment.
try:
cleanup_hours = st.secrets.get("MC_USER_QSET_CLEANUP_HOURS", "")
if isinstance(cleanup_hours, str):
cleanup_hours = cleanup_hours.strip()
else:
cleanup_hours = str(cleanup_hours).strip() if cleanup_hours is not None else ""
except Exception:
cleanup_hours = ""
if not cleanup_hours:
cleanup_hours = os.getenv("MC_USER_QSET_CLEANUP_HOURS", "").strip()
if cleanup_hours:
try:
parsed = int(cleanup_hours)
if parsed > 0:
self.user_qset_cleanup_hours = parsed
except Exception:
pass
# Optional: retention days for reserved pseudonyms
try:
reserved_days = st.secrets.get("MC_USER_QSET_RESERVED_RETENTION_DAYS", "")
if isinstance(reserved_days, str):
reserved_days = reserved_days.strip()
else:
reserved_days = str(reserved_days).strip() if reserved_days is not None else ""
except Exception:
reserved_days = ""
if not reserved_days:
reserved_days = os.getenv("MC_USER_QSET_RESERVED_RETENTION_DAYS", "").strip()
if reserved_days:
try:
parsed = int(reserved_days)
if parsed > 0:
self.user_qset_reserved_retention_days = parsed
except Exception:
pass
# Optional: enable automatic release of unused/unreserved pseudonyms
try:
auto_release = st.secrets.get("MC_AUTO_RELEASE_PSEUDONYMS", "").strip()
except Exception:
auto_release = os.getenv("MC_AUTO_RELEASE_PSEUDONYMS", "").strip()
if auto_release:
if str(auto_release).lower() in ("1", "true", "yes", "on"):
self.auto_release_unreserved_pseudonyms = True
# Recovery / rate-limit from secrets/env
try:
rl_attempts = st.secrets.get("MC_RATE_LIMIT_ATTEMPTS", "").strip()
except Exception:
rl_attempts = os.getenv("MC_RATE_LIMIT_ATTEMPTS", "").strip()
if rl_attempts:
try:
self.rate_limit_attempts = int(rl_attempts)
except Exception:
pass
try:
rl_window = st.secrets.get("MC_RATE_LIMIT_WINDOW_MINUTES", "").strip()
except Exception:
rl_window = os.getenv("MC_RATE_LIMIT_WINDOW_MINUTES", "").strip()
if rl_window:
try:
self.rate_limit_window_minutes = int(rl_window)
except Exception:
pass
try:
rec_min = st.secrets.get("MC_RECOVERY_MIN_LENGTH", "").strip()
except Exception:
rec_min = os.getenv("MC_RECOVERY_MIN_LENGTH", "").strip()
if rec_min:
try:
self.recovery_min_length = int(rec_min)
except Exception:
pass
try:
rec_allow = st.secrets.get("MC_RECOVERY_ALLOW_SHORT", "").strip()
except Exception:
rec_allow = os.getenv("MC_RECOVERY_ALLOW_SHORT", "").strip()
if rec_allow:
lower = rec_allow.lower()
if lower in ("1", "true", "yes", "y"):
self.recovery_allow_short = True
else:
self.recovery_allow_short = False
# Optional: normalization factor for Next-button cooldown extras
try:
norm_val = st.secrets.get("MC_NEXT_COOLDOWN_NORMALIZATION_FACTOR", "").strip()
except Exception:
norm_val = os.getenv("MC_NEXT_COOLDOWN_NORMALIZATION_FACTOR", "").strip()
if norm_val:
try:
parsed = float(norm_val)
if parsed > 0:
self.next_cooldown_normalization_factor = parsed
except Exception:
pass
def _load_from_json(self):
"""Lädt Konfiguration aus der JSON-Datei und überschreibt ggf. Defaults."""
path = os.path.join(get_package_dir(), "mc_test_config.json")
try:
if os.path.isfile(path):
with open(path, "r", encoding="utf-8") as f:
config_data = json.load(f)
self.scoring_mode = config_data.get("scoring_mode", self.scoring_mode)
self.show_top5_public = config_data.get(
"show_top5_public", self.show_top5_public
)
raw_test_minutes = config_data.get("test_duration_minutes", self.test_duration_minutes)
try:
parsed_minutes = int(raw_test_minutes)
if parsed_minutes > 0:
self.test_duration_minutes = parsed_minutes
except (TypeError, ValueError):
pass # Behalte bestehenden Wert bei ungültigen Eingaben
# optional recovery/rate-limit overrides
try:
self.recovery_min_length = int(config_data.get("recovery_min_length", self.recovery_min_length))
except Exception:
pass
self.recovery_allow_short = bool(config_data.get("recovery_allow_short", self.recovery_allow_short))
try:
self.rate_limit_attempts = int(config_data.get("rate_limit_attempts", self.rate_limit_attempts))
except Exception:
pass
try:
self.rate_limit_window_minutes = int(config_data.get("rate_limit_window_minutes", self.rate_limit_window_minutes))
except Exception:
pass
# Optional normalization factor override from JSON config
try:
val = config_data.get("next_cooldown_normalization_factor", None)
if val is not None:
parsed = float(val)
if parsed > 0:
self.next_cooldown_normalization_factor = parsed
except Exception:
pass
# Optional cleanup hours override from JSON config
try:
val = config_data.get("user_qset_cleanup_hours", None)
if val is not None:
parsed = int(val)
if parsed > 0:
self.user_qset_cleanup_hours = parsed
except Exception:
pass
# Optional reserved retention days override from JSON config
try:
val = config_data.get("user_qset_reserved_retention_days", None)
if val is not None:
parsed = int(val)
if parsed > 0:
self.user_qset_reserved_retention_days = parsed
except Exception:
pass
except (IOError, json.JSONDecodeError):
pass # Bei Fehlern werden die Defaults beibehalten
def save(self):
"""Speichert die aktuelle Konfiguration in die JSON-Datei."""
path = os.path.join(get_package_dir(), "mc_test_config.json")
config_data = {
"scoring_mode": self.scoring_mode,
"show_top5_public": self.show_top5_public,
"test_duration_minutes": self.test_duration_minutes,
}
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(config_data, f, indent=2)
except IOError:
st.error(translate_ui("config.error.config_save_failed", default="Konfiguration konnte nicht gespeichert werden."))
@st.cache_data
def list_question_files() -> List[str]:
"""Listet alle verfügbaren `questions_*.json` Dateien auf."""
import unicodedata
data_dir = os.path.join(get_package_dir(), "data")
if not os.path.isdir(data_dir):
return []
# Normalisiere Dateinamen zu NFC (wichtig für macOS Umlaute)
files = []
for f in os.listdir(data_dir):
if f.startswith("questions_") and f.endswith(".json"):
# Backup-Dateien (pre_overwrite/backup) ignorieren
if "_pre_overwrite_" in f or "_backup_" in f:
continue
# Normalisiere zu NFC (composed form)
normalized = unicodedata.normalize("NFC", f)
files.append(normalized)
return sorted(files, key=str.casefold)
@st.cache_data
def get_question_counts() -> Dict[str, int]:
"""
Gibt ein Dictionary mit der Anzahl der Fragen für jede gültige Fragenset-Datei zurück.
Dies ist performanter, als für jede Datei `load_questions` einzeln aufzurufen.
"""
counts = {}
files = list_question_files()
for filename in files:
# Lade die Fragen (aus dem Cache, falls schon geladen) und zähle sie.
questions = load_questions(filename, silent=True)
if questions:
counts[filename] = len(questions)
return counts
def _resolve_question_paths(filename: str) -> List[Path]:
base_dir = Path(get_package_dir())
data_dir = base_dir / "data"
user_dir = base_dir / "data-user"
candidates: List[Path] = []
if filename.startswith(USER_QUESTION_PREFIX):
actual_name = filename[len(USER_QUESTION_PREFIX):]
candidates.append(user_dir / actual_name)
else:
candidate_path = Path(filename)
if candidate_path.is_absolute():
candidates.append(candidate_path)
elif any(sep in filename for sep in ("/", "\\")):
candidates.append(base_dir / filename)
# Prefer user overrides before base data files
candidates.append(user_dir / filename)
candidates.append(data_dir / filename)
if not candidates:
candidates.append(data_dir / filename)
# ensure order without duplicates
seen: set[Path] = set()
unique_candidates: List[Path] = []
for path in candidates:
if path not in seen:
unique_candidates.append(path)
seen.add(path)
return unique_candidates
@st.cache_data
def load_questions(filename: str, silent: bool = False) -> QuestionSet:
"""Lädt ein spezifisches Fragenset aus einer JSON-Datei."""
last_error: Exception | None = None
resolved_path: Path | None = None
for candidate in _resolve_question_paths(filename):
try:
with candidate.open("r", encoding="utf-8") as f:
data = json.load(f)
resolved_path = candidate
break
except (IOError, json.JSONDecodeError) as exc:
last_error = exc
continue
if resolved_path is None:
if not silent:
streamlit_module = sys.modules.get("streamlit", st)
error_handler = getattr(streamlit_module, "error", _make_streamlit_noop("error"))
if filename.startswith(USER_QUESTION_PREFIX) and isinstance(last_error, FileNotFoundError):
error_handler(
translate_ui("config.error.temp_set_removed", default="Das temporäre Fragenset wurde vom Ersteller entfernt. Bitte lade die Seite neu und wähle ein anderes Fragenset.")
)
# Hinweis: Session-Flags (z.B. Anzeige-Hinweise) sollten von
# dem Code gesetzt werden, der das Löschen initiiert (z.B. die
# Session-Abbruch-Logik oder explizite Delete-Handler). Hier
# vermeiden wir Seiteneffekte, die beim bloßen Laden einer Datei
# zu unbeabsichtigten UI-Nachrichten führen können.
else:
error_handler(translate_ui("config.error.load_failed", default="Fehler beim Laden von '{filename}': {error}").format(filename=filename, error=last_error))
return QuestionSet([], {}, filename)
source_name = filename
if filename.startswith(USER_QUESTION_PREFIX):
source_name = filename
elif resolved_path.name != filename:
source_name = resolved_path.name
try:
return _build_question_set(data, source_name, silent=silent)
except ValueError as exc:
if not silent:
streamlit_module = sys.modules.get("streamlit", st)
error_handler = getattr(streamlit_module, "error", _make_streamlit_noop("error"))
error_handler(translate_ui("config.error.invalid_json", default="Fehler in '{filename}': {error}").format(filename=filename, error=exc))
return QuestionSet([], {}, filename)
def _scientists_path_for_locale(locale: str | None) -> str | None:
base_dir = os.path.join(get_package_dir(), "data")
locale_code = normalize_locale(locale or get_locale())
candidate = os.path.join(base_dir, f"scientists.{locale_code}.json")
if os.path.isfile(candidate):
return candidate
generic = os.path.join(base_dir, "scientists.json")
if os.path.isfile(generic):
return generic
fallback = os.path.join(base_dir, f"scientists.{DEFAULT_LOCALE}.json")
if os.path.isfile(fallback):
return fallback
return None
@st.cache_data(ttl=3600)
def _read_scientists_file(path: str) -> List[Dict[str, str]]:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def load_scientists(locale: str | None = None) -> List[Dict[str, str]]:
"""Lädt die Liste der Wissenschaftler aus der JSON-Datei für die gewünschte Sprache."""
path = _scientists_path_for_locale(locale)
if path is None:
st.error(translate_ui("config.error.scientists_not_found", default="Fehler: Wissenschaftlerliste (scientists.json) nicht gefunden."))
return []
try:
return _read_scientists_file(path)
except (IOError, json.JSONDecodeError) as e:
st.error(translate_ui("config.error.scientists_load_failed", default="Fehler beim Laden von '{filename}': {error}").format(filename=os.path.basename(path), error=e))
return []