From 4068f47af9ba9b46b5c15eda4477bb4eac6b422c Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 17 Sep 2024 20:04:04 +0200 Subject: [PATCH] Mehrere Daten-Dateien, besserer Import --- main.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index bfe6d88..607ea62 100644 --- a/main.py +++ b/main.py @@ -4,15 +4,18 @@ import pyamf import shutil import json import os +import glob import argparse import unicodedata import random import re import sys import readline +import datetime DATA_DIR = os.environ.get('XDG_DATA_HOME', os.environ['HOME'] + '/.local/share') + '/klette' -VOKABELN_FILE = DATA_DIR + '/vokabeln.json' +DEPRECATED_VOKABELN_FILE = DATA_DIR + '/vokabeln.json' +VOKABELN_DIR = DATA_DIR + '/vokabeln/' STATUS_FILE = DATA_DIR + '/status.json' AUDIO_BASE = DATA_DIR + '/audio/' @@ -49,9 +52,10 @@ def import_vokabeln(file_name, audio_base): 'unidad': unidad, 'paso': paso }) - shutil.copy2(audio_base + audio + '.bin', - AUDIO_BASE + audio + '.aac') - with open(VOKABELN_FILE, 'x') as writer: + if audio: + shutil.copy2(audio_base + audio + '.bin', + AUDIO_BASE + audio + '.aac') + with open(VOKABELN_DIR + '/import-' + datetime.datetime.now().isoformat() + '.json', 'x') as writer: writer.write(json.dumps(palabras)) def import_status(file_name): @@ -238,8 +242,16 @@ class Sesion: def abfrage(parser, quiero_unidad, otra_vez): random.seed() try: - with open(VOKABELN_FILE, 'r') as f: - palabras = json.load(f) + palabras = [] + os.makedirs(VOKABELN_DIR, exist_ok=True) + try: + os.rename(DEPRECATED_VOKABELN_FILE, os.path.join(VOKABELN_DIR, 'vokabeln.json')) + except FileNotFoundError: + pass + + for filename in glob.glob(os.path.join(VOKABELN_DIR, '*.json')): + with open(filename, 'r') as f: + palabras += json.load(f) with open(STATUS_FILE, 'r') as f: status = json.load(f) except FileNotFoundError: @@ -255,7 +267,6 @@ def abfrage(parser, quiero_unidad, otra_vez): print(f'{bcolors.OKGREEN}+{sesion.bien}{bcolors.ENDC} / {bcolors.FAIL}-{sesion.mal}{bcolors.ENDC}') parser = argparse.ArgumentParser() -default_data_file = os.environ['PWD'] + '/assets/amf/vokabelTrainer147.amf' parser.add_argument('--import-data', type=str, help="Path to assets", metavar="DIR") default_status_file = os.environ['HOME'] + '/klett/1266/vokabeltrainerData147' parser.add_argument('--import-status', type=str, help="Path to AMF File, defaults to " + default_status_file, metavar="FILE", nargs='?', const=default_status_file) @@ -264,7 +275,7 @@ parser.add_argument('--otra-vez', action='store_true') args = parser.parse_args() if args.import_data: - import_vokabeln(args.import_data + '/amf/vokabelTrainer147.amf', + import_vokabeln(glob.glob(os.path.join(args.import_data, 'amf/vokabelTrainer*.amf'))[0], args.import_data + '/amf/medium/') elif args.import_status: import_status(args.import_status)