Browse Source

added load_config and try_load_config functions

pull/3/head
Eugen Ciur 3 years ago
parent
commit
6759e35060
  1. 3
      changelog.md
  2. 46
      mglib/utils.py
  3. 2
      setup.py

3
changelog.md

@ -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

46
mglib/utils.py

@ -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

2
setup.py

@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setup(
name="mglib",
version="1.1.0",
version="1.1.1",
author="Eugen Ciur",
author_email="eugen@papermerge.com",
url="https://github.com/papermerge/mglib",

Loading…
Cancel
Save