Download config file if missing

This commit is contained in:
AuroraWright
2024-01-30 15:14:04 +01:00
parent 1b88f2a6c5
commit e1caf06134
3 changed files with 29 additions and 13 deletions

View File

@@ -1,9 +1,12 @@
import os import os
import configparser import configparser
import urllib.request
class Config: class Config:
has_config = False has_config = False
downloaded_config = False
config_path = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
__general_config = {} __general_config = {}
__engine_config = {} __engine_config = {}
__default_config = { __default_config = {
@@ -45,11 +48,19 @@ class Config:
return value return value
def __init__(self): def __init__(self):
config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
config = configparser.ConfigParser() config = configparser.ConfigParser()
res = config.read(config_file) res = config.read(self.config_path)
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
if len(res) != 0:
self.has_config = True self.has_config = True
for key in config: for key in config:
if key == 'general': if key == 'general':

View File

@@ -435,6 +435,9 @@ class RapidOCR:
if not os.path.isfile(rapidocr_model_file): if not os.path.isfile(rapidocr_model_file):
logger.info('Downloading RapidOCR model') logger.info('Downloading RapidOCR model')
try: 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) urllib.request.urlretrieve('https://github.com/AuroraWright/owocr/raw/master/rapidocr_japan_PP-OCRv4_rec_infer.onnx', rapidocr_model_file)
except: except:
logger.warning('Download failed. RapidOCR will not work!') logger.warning('Download failed. RapidOCR will not work!')

View File

@@ -330,7 +330,9 @@ def run(read_from=None,
if config.has_config: if config.has_config:
logger.info('Parsed config file') logger.info('Parsed config file')
else: 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 = [] engine_instances = []
config_engines = [] config_engines = []