mirror of https://github.com/papermerge/mglib
added load_config and try_load_config functions
parent
0d2d5c7dfb
commit
6759e35060
|
@ -3,7 +3,8 @@
|
|||
## [1.1.0] - work in progress
|
||||
|
||||
### Added
|
||||
|
||||
- utils.try_load_config
|
||||
- utils.load_config
|
||||
- Endpoint module move in (from pmworker)
|
||||
|
||||
## [1.0.0] - 25 Apr 2020
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import os
|
||||
import logging
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -84,3 +87,46 @@ def get_assigns_after_delete(total_pages, deleted_pages):
|
|||
page_numbers = range(1, len(pages) + 1)
|
||||
|
||||
return list(zip(page_numbers, pages))
|
||||
|
||||
|
||||
def load_config(config_file):
|
||||
|
||||
loader_ = importlib.machinery.SourceFileLoader(
|
||||
"config",
|
||||
config_file
|
||||
)
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"config", config_file, loader=loader_
|
||||
)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
# and stop looking for ther configs.
|
||||
cfg_papermerge = vars(mod)
|
||||
cfg_file_found = True
|
||||
|
||||
return cfg_papermerge, cfg_file_found
|
||||
|
||||
|
||||
def try_load_config(config_locations, config_env_var_name):
|
||||
|
||||
cfg_file_found = False
|
||||
cfg_papermerge = {}
|
||||
|
||||
for config_file in config_locations:
|
||||
if os.path.exists(config_file):
|
||||
cfg_papermerge, cfg_file_found = load_config(config_file)
|
||||
if cfg_file_found:
|
||||
break
|
||||
|
||||
if not cfg_file_found:
|
||||
config_file = os.environ.get(config_env_var_name, False)
|
||||
if config_file:
|
||||
try:
|
||||
cfg_papermerge, cfg_file_found = load_config(config_file)
|
||||
except FileNotFoundError:
|
||||
err_msg = f"Failed attempted to read" +\
|
||||
f" configuration file '{config_file}'" +\
|
||||
f" pointed by environment variable '{config_env_var_name}'"
|
||||
raise FileNotFoundError(err_msg)
|
||||
|
||||
return cfg_papermerge
|
||||
|
|
Loading…
Reference in New Issue