Skip to content
Snippets Groups Projects
Commit 796c5cc4 authored by Joscha Schmiedt's avatar Joscha Schmiedt
Browse files

Add type hints to configuration.py

parent b05dcfc4
No related branches found
No related tags found
2 merge requests!143Release 0.15.0,!135Add and fix more type hints
...@@ -21,14 +21,14 @@ ...@@ -21,14 +21,14 @@
# #
# ** end header # ** end header
# #
from __future__ import annotations
import os import os
import warnings import warnings
import yaml import yaml
try: try:
optional_jsonschema_validate = None optional_jsonschema_validate: Optional[Callable] = None
from jsonschema import validate as optional_jsonschema_validate from jsonschema import validate as optional_jsonschema_validate
except ImportError: except ImportError:
pass pass
...@@ -37,13 +37,15 @@ from configparser import ConfigParser ...@@ -37,13 +37,15 @@ from configparser import ConfigParser
from os import environ, getcwd from os import environ, getcwd
from os.path import expanduser, isfile, join from os.path import expanduser, isfile, join
from typing import Dict, Union, Callable, Optional
def _reset_config(): def _reset_config():
global _pycaosdbconf global _pycaosdbconf
_pycaosdbconf = ConfigParser(allow_no_value=False) _pycaosdbconf = ConfigParser(allow_no_value=False)
def configure(inifile): def configure(inifile: str) -> list[str]:
"""read config from file. """read config from file.
Return a list of files which have successfully been parsed. Return a list of files which have successfully been parsed.
...@@ -61,15 +63,15 @@ def configure(inifile): ...@@ -61,15 +63,15 @@ def configure(inifile):
return read_config return read_config
def get_config(): def get_config() -> ConfigParser:
global _pycaosdbconf global _pycaosdbconf
if ("_pycaosdbconf" not in globals() or _pycaosdbconf is None): if ("_pycaosdbconf" not in globals() or _pycaosdbconf is None):
_reset_config() _reset_config()
return _pycaosdbconf return _pycaosdbconf
def config_to_yaml(config): def config_to_yaml(config: ConfigParser) -> Dict[str, Dict[str, Union[int, str, bool]]]:
valobj = {} valobj: Dict[str, Dict[str, Union[int, str, bool]]] = {}
for s in config.sections(): for s in config.sections():
valobj[s] = {} valobj[s] = {}
for key, value in config[s].items(): for key, value in config[s].items():
...@@ -84,7 +86,7 @@ def config_to_yaml(config): ...@@ -84,7 +86,7 @@ def config_to_yaml(config):
return valobj return valobj
def validate_yaml_schema(valobj): def validate_yaml_schema(valobj: Dict[str, Dict[str, Union[int, str, bool]]]):
if optional_jsonschema_validate: if optional_jsonschema_validate:
with open(os.path.join(os.path.dirname(__file__), "schema-pycaosdb-ini.yml")) as f: with open(os.path.join(os.path.dirname(__file__), "schema-pycaosdb-ini.yml")) as f:
schema = yaml.load(f, Loader=yaml.SafeLoader) schema = yaml.load(f, Loader=yaml.SafeLoader)
...@@ -95,7 +97,7 @@ def validate_yaml_schema(valobj): ...@@ -95,7 +97,7 @@ def validate_yaml_schema(valobj):
""") """)
def _read_config_files(): def _read_config_files() -> list[str]:
"""Read config files from different paths. """Read config files from different paths.
Read the config from either ``$PYCAOSDBINI`` or home directory (``~/.pylinkahead.ini``), and Read the config from either ``$PYCAOSDBINI`` or home directory (``~/.pylinkahead.ini``), and
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment