Skip to content

Commit

Permalink
Merge pull request #468 from TeskaLabs/feature/multiline-config-method
Browse files Browse the repository at this point in the history
Add `asab.Config.getmultiline()` method
  • Loading branch information
mejroslav authored Aug 10, 2023
2 parents 97cb882 + 347076f commit 0592d7f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
39 changes: 37 additions & 2 deletions asab/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import re
import glob
import logging
import inspect
Expand All @@ -8,6 +9,8 @@
import urllib.parse
import collections.abc

import typing

from . import utils


Expand Down Expand Up @@ -270,15 +273,47 @@ 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):
"""Gets URL from config and removes all leading and trailing
whitespaces and trailing slashes.
:param scheme: URL scheme(s) awaited. If None, scheme validation is bypassed.
:type scheme: str, tuple
:return: validated URL, raises ValueError when scheme requirements are not met if set.
"""
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):
Expand Down
36 changes: 36 additions & 0 deletions examples/config_getmultiline.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0592d7f

Please sign in to comment.