From e1caf0613413ee5523256ff93a3a0db7e41c1e81 Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Tue, 30 Jan 2024 15:14:04 +0100 Subject: [PATCH] Download config file if missing --- owocr/config.py | 35 +++++++++++++++++++++++------------ owocr/ocr.py | 3 +++ owocr/run.py | 4 +++- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/owocr/config.py b/owocr/config.py index 4aa74f5..91a0d07 100644 --- a/owocr/config.py +++ b/owocr/config.py @@ -1,9 +1,12 @@ import os import configparser +import urllib.request class Config: has_config = False + downloaded_config = False + config_path = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini') __general_config = {} __engine_config = {} __default_config = { @@ -45,20 +48,28 @@ class Config: return value def __init__(self): - config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini') config = configparser.ConfigParser() - res = config.read(config_file) + res = config.read(self.config_path) - if len(res) != 0: - self.has_config = True - for key in config: - if key == 'general': - for sub_key in config[key]: - self.__general_config[sub_key.lower()] = self.__parse(config[key][sub_key]) - elif key != 'DEFAULT': - self.__engine_config[key.lower()] = {} - for sub_key in config[key]: - self.__engine_config[key.lower()][sub_key.lower()] = self.__parse(config[key][sub_key]) + if len(res) == 0: + try: + config_folder = os.path.join(os.path.expanduser('~'),'.config') + if not os.path.isdir(config_folder): + os.makedirs(config_folder) + urllib.request.urlretrieve('https://github.com/AuroraWright/owocr/raw/master/owocr_config.ini', self.config_path) + self.downloaded_config = True + finally: + return + + self.has_config = True + for key in config: + if key == 'general': + for sub_key in config[key]: + self.__general_config[sub_key.lower()] = self.__parse(config[key][sub_key]) + elif key != 'DEFAULT': + self.__engine_config[key.lower()] = {} + for sub_key in config[key]: + self.__engine_config[key.lower()][sub_key.lower()] = self.__parse(config[key][sub_key]) def get_general(self, value): try: diff --git a/owocr/ocr.py b/owocr/ocr.py index 8a22869..77240d2 100644 --- a/owocr/ocr.py +++ b/owocr/ocr.py @@ -435,6 +435,9 @@ class RapidOCR: if not os.path.isfile(rapidocr_model_file): logger.info('Downloading RapidOCR model') try: + cache_folder = os.path.join(os.path.expanduser('~'),'.cache') + if not os.path.isdir(cache_folder): + os.makedirs(cache_folder) urllib.request.urlretrieve('https://github.com/AuroraWright/owocr/raw/master/rapidocr_japan_PP-OCRv4_rec_infer.onnx', rapidocr_model_file) except: logger.warning('Download failed. RapidOCR will not work!') diff --git a/owocr/run.py b/owocr/run.py index 8d73a06..ce48c4b 100644 --- a/owocr/run.py +++ b/owocr/run.py @@ -330,7 +330,9 @@ def run(read_from=None, if config.has_config: logger.info('Parsed config file') else: - logger.warning('No config file, defaults will be used') + logger.warning('No config file, defaults will be used.') + if config.downloaded_config: + logger.info(f'A default config file has been downloaded to {config.config_path}') engine_instances = [] config_engines = []