diff --git a/asab/config.py b/asab/config.py index c010af57f..45efef720 100644 --- a/asab/config.py +++ b/asab/config.py @@ -1,5 +1,6 @@ import os import sys +import re import glob import logging import inspect @@ -327,10 +328,9 @@ def getseconds(self, section, option, *, raw=False, vars=None, fallback=None, ** return self._get_conv(section, option, utils.convert_to_seconds, raw=raw, vars=vars, fallback=fallback, **kwargs) - def geturl(self, section, option, raw=False, vars=None, fallback=None, scheme=None, **kwargs): + def geturl(self, section, option, *, raw=False, vars=None, fallback=None, scheme=None, **kwargs): """ - Get URL from config and remove all leading and trailing - whitespaces and trailing slashes. + Get URL from config and remove all leading and trailing whitespaces and trailing slashes. Args: scheme (str | tuple): URL scheme(s) awaited. If `None`, scheme validation is bypassed. @@ -343,19 +343,50 @@ def geturl(self, section, option, raw=False, vars=None, fallback=None, scheme=No Examples: - Configuration file: ```ini [urls] teskalabs=https://www.teskalabs.com/ github=github.com ``` - Script: + ``` python asab.Config["urls"].geturl("teskalabs", scheme="https") asab.Config.geturl("urls", "github", scheme=None) ``` """ - return utils.validate_url(self.get(section, option, raw=False, vars=None, fallback=fallback), scheme) + return utils.validate_url(self.get(section, option, raw=raw, vars=vars, fallback=fallback), scheme) + + + def getmultiline(self, section, option, *, raw=False, vars=None, fallback=None, **kwargs) -> typing.List[str]: + """ + Get multiline data from config. + + Examples: + + ```ini + [places] + visited: + Praha + Brno + Pardubice Plzeň + unvisited: + ``` + + ```python + >>> asab.Config.getmultiline("places", "visited") + ["Praha", "Brno", "Pardubice", "Plzeň"] + >>> asab.Config.getmultiline("places", "unvisited") + [] + >>> asab.Config.getmultiline("places", "nonexisting", fallback=["Gottwaldov"]) + ["Gottwaldov"] + ``` + """ + values = self.get(section, option, raw=raw, vars=vars, fallback=fallback) + if isinstance(values, str): + return [item.strip() for item in re.split(r"\s+", values) if len(item) > 0] + else: + # fallback can be anything + return values class _Interpolation(configparser.ExtendedInterpolation): diff --git a/examples/config_getmultiline.py b/examples/config_getmultiline.py new file mode 100644 index 000000000..087b757c3 --- /dev/null +++ b/examples/config_getmultiline.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import asab +import logging + +# + +L = logging.getLogger(__name__) + +# + +asab.Config.read_string( + """ + [places] + visited: + Praha + Brno + Pardubice Plzeň + """ +) + + +class MyApplication(asab.Application): + + async def main(self): + visited = asab.Config.getmultiline("places", "visited") + unvisited = asab.Config.getmultiline("places", "unvisited", fallback=[]) + nonexisting = asab.Config.getmultiline("places", "nonexisting", fallback=["Gottwaldov"]) + L.log(asab.LOG_NOTICE, "Places I've already visited: {}".format(visited)) + L.log(asab.LOG_NOTICE, "Places I want to visit: {}".format(unvisited)) + L.log(asab.LOG_NOTICE, "Places that don't exist: {}".format(nonexisting)) + self.stop() + + +if __name__ == "__main__": + app = MyApplication() + app.run()