Skip to content

Commit

Permalink
update_config_from_args improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TezRomacH committed Feb 16, 2019
1 parent ed90ea4 commit da0a491
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Safitty
[![Build Status](https://travis-ci.com/TezRomacH/safitty.svg?branch=master)](https://travis-ci.com/TezRomacH/safitty)
[![Pypi version](https://img.shields.io/pypi/v/safitty.svg?colorB=blue)](https://pypi.org/project/safitty/)
[![Downloads](https://img.shields.io/pypi/dm/safitty.svg?style=flat)](https://pypi.org/project/safitty/)
[![License](https://img.shields.io/github/license/TezRomacH/safitty.svg)](LICENSE)
Expand Down
30 changes: 17 additions & 13 deletions safitty/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .types import Storage
from pydoc import locate

from safitty.core import safe_set


def argparser(**argparser_kwargs) -> argparse.ArgumentParser:
"""Creates typical argument parser with ``--config`` argument
Expand Down Expand Up @@ -134,6 +136,10 @@ def parse_content(value: str) -> Any:
1 # type is int
>>> parse_content("1:float")
1.0 # type is float
>>> parse_content("[1,2]:list")
[1, 2] # type is list
>>> parse_content("'[1,2]:list'")
'[1,2]:list' # type is str
"""
quotes_wrap = """^["'][^ ].*[^ ]["']$"""
if re.match(quotes_wrap, value) is not None:
Expand All @@ -147,9 +153,16 @@ def parse_content(value: str) -> Any:
value_content, value_type = content

result = value_content
value_type = type_from_str(value_type)
if value_type is not None:
result = value_type(value_content)

if value_type in ["set", "list", "dict", "frozenset"]:
try:
result = eval(f"{value_type}({value_content})")
except Exception:
result = value_content
else:
value_type = type_from_str(value_type)
if value_type is not None:
result = value_type(value_content)

return result

Expand All @@ -171,16 +184,7 @@ def update_config_from_args(config: Storage, args: List[str]) -> Storage:
value = parse_content(value)
names = [parse_content(name) for name in names.split("/")]

config_ = updated_config
# TODO: change to `safe_set`
# updated_config = safe_set(updated_config, *names, value=value)
for name in names[:-1]:
if name not in config_:
config_[name] = {}

config_ = config_[name]

config_[names[-1]] = value
updated_config = safe_set(updated_config, *names, value=value)

return updated_config

Expand Down

0 comments on commit da0a491

Please sign in to comment.