Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

add and remove methods added #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions kaptan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,48 @@ def get(self, key=None, default=SENTINEL):
return default
raise

def add(self, key, value=None, replace=False):
current_data = self.configuration_data
keys = key.split('.')
new_key = keys[0]
is_key_exist = False
if len(keys) > 1:
new_key = keys.pop()
for chunk in keys:
try:
if not replace and new_key in current_data[chunk]:
is_key_exist = True
current_data = current_data[chunk]
except KeyError:
current_data[chunk] = {}
Copy link

@bahattincinic bahattincinic Jun 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_data[chunk] = {}
current_data = current_data[chunk]

and

current_data = {}

the same.

The second method is simple and easy to read.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have to change current_data[chunk] as {} here.

current_data = current_data[chunk]
try:
if replace:
current_data[new_key] = value
elif isinstance(current_data[new_key], list):
current_data[new_key].append(value)
elif is_key_exist:
raise RuntimeError("Key %s already exist" % new_key)
except KeyError as e:
current_data[new_key] = value

return self

def remove(self, key, index=None):
current_data = self.configuration_data
keys = key.split('.')
exact_key = keys[0]
if len(keys) > 1:
exact_key = keys.pop()
for chunk in keys:
current_data = current_data[chunk]
if isinstance(index, int) and isinstance(current_data[exact_key], list):
current_data[exact_key].pop(index)
else:
del current_data[exact_key]

return self

def export(self, handler=None, **kwargs):
if not handler:
handler_class = self.handler
Expand Down
65 changes: 65 additions & 0 deletions tests/test_kaptan.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,71 @@ def test_lists_on_configuration():
assert config.get('servers.0') == 'redis1'


def test_add_to_configuration():
config = kaptan.Kaptan()
config.import_config({
'server': 'redis'
})
config.add('uptime', 99.9)

assert config.get('uptime') == 99.9


def test_add_to_list_on_configuration():
config = kaptan.Kaptan()
config.import_config({
'servers': ['redis1', 'redis2', 'redis3'],
})
config.add('servers', 'redis4')

assert config.get('servers.3') == 'redis4'


def test_replace_key_on_configuration():
config = kaptan.Kaptan()
config.import_config({
'price': 22
})
config.add('price', 12.5, True)

assert config.get('price') == 12.5


def test_add_key_with_parent_on_configuration():
config = kaptan.Kaptan()
config.import_config({
'product': 'Test'
})
config.add('price.currency.name', 'TL')

assert config.get('price.currency.name') == 'TL'


def test_remove_key_from_configuration():
config = kaptan.Kaptan()
config.import_config({
'server': {
'name': 'redis',
'status': True
}
})
config.remove('server.status')

assert config.get('server') == {'name': 'redis'}


def test_remove_item_from_array_on_configuration():
config = kaptan.Kaptan()
config.import_config({
'price': {
'currencies': ['TL', 'EUR', 'USD']
}
})
config.remove('price.currencies', 1)

assert config.get('price.currencies') == ['TL', 'USD']


def test_upsert(testconfig):
config = kaptan.Kaptan()
config.import_config(testconfig)
Expand Down