Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Net-Mist committed Jun 30, 2019
1 parent aa57b11 commit d76fa04
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__pycache__
.vscode
distribute_config.egg-info
conf.yml
conf2.yml
.eggs
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![Maintainability](https://api.codeclimate.com/v1/badges/c95ee137fde197b24dc1/maintainability)](https://codeclimate.com/github/Net-Mist/distribute_config/maintainability)
[![Coverage Status](https://coveralls.io/repos/github/Net-Mist/distribute_config/badge.svg?branch=master)](https://coveralls.io/github/Net-Mist/distribute_config?branch=master)
[![Build Status](https://travis-ci.org/Net-Mist/distribute_config.svg?branch=master)](https://travis-ci.org/Net-Mist/distribute_config)

A package to handle multi-source distributed configuration

7 changes: 0 additions & 7 deletions app.py

This file was deleted.

21 changes: 17 additions & 4 deletions distribute_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def __init__(self):
if not Config.__instance:
Config.__instance = Config.__Config()

@classmethod
def clear(cls):
cls.__instance = Config.__Config()

@classmethod
def _set_namespace(cls, name):
cls.__instance.namespace = name
Expand Down Expand Up @@ -97,19 +101,19 @@ def get_var(cls, name):
variables = variables[sub_path]

if type(variables) == Variable:
return variables.value
return variables.get_value()
else:
return variables

@classmethod
def set_val(cls, name, val):
def set_var(cls, name, val):
path = name.split(".")
variables = cls.__instance.variables
for sub_path in path:
variables = variables[sub_path]

assert type(variables) == Variable
variables.value = val
variables.set_value(val)

@classmethod
def get_dict(cls):
Expand Down Expand Up @@ -145,11 +149,20 @@ def load_conf(cls):
for var in os.environ:
path = ".".join(var.lower().split("__"))
try:
cls.set_val(path, os.environ[var])
cls.set_var(path, os.environ[var])
print("Load env variable", var)
except KeyError:
pass

#3
print(vars(args))
for key in vars(args):
if key == "c":
continue
cls.set_var(key, vars(args)[key])



@staticmethod
def load_dict(loading_dict, variables):
for key in loading_dict:
Expand Down
32 changes: 28 additions & 4 deletions distribute_config/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from unittest import TestCase
import argparse
import os
from unittest import TestCase, mock

from distribute_config import Config


class TestConfig(TestCase):
def test_define(self):
def test_define_int(self):
Config.clear()
# int and flat float can be assign as int
Config.define_int("test_int", 5, "A test for int")
Config.define_int("test_int_float_valid", 5., "A test for int with a float var")
Expand All @@ -17,10 +20,10 @@ def test_define(self):
with self.assertRaises(KeyError):
Config.define_int("test_int", 5, "A test for int")

# print(Config.get_dict())
self.assertDictEqual(Config.get_dict(), {'test_int': 5, 'test_int_float_valid': 5})

def test_namespace(self):
Config.clear()
Config.define_int("namespace1.test_int", 5, "A test for int")
with Config.namespace("namespace2"):
Config.define_int("test_int", 5, "A test for int")
Expand All @@ -30,8 +33,29 @@ def test_namespace(self):
Config.define_int("plop", 4, "test of subnamespace")

# print(Config.get_dict())
self.assertDictEqual(Config.get_dict(), {'test_int': 5, 'test_int_float_valid': 5, 'namespace1': {'test_int': 5},
self.assertDictEqual(Config.get_dict(), {'namespace1': {'test_int': 5},
'namespace2': {'test_int': 5, 'subnamespace': {'test_int': 5}, 'subnamespace2': {'plop': 4}}})

def test_int(self):
print(Config.get_dict())

@mock.patch('argparse.ArgumentParser.parse_args',
return_value=argparse.Namespace(v1=2, v2=3, c="conf.yml"))
def test_load_conf(self, mock_args):
Config.clear()
Config.define_int("v1", 1, "var")
Config.define_int("v2", 2, "var")
Config.load_conf()
self.assertEqual(Config.get_var("v1"), 2)
self.assertEqual(Config.get_var("v2"), 3)

@mock.patch.dict(os.environ, {"V1": "1", "NM__V2": "2"})
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=argparse.Namespace(c="conf2.yml"))
def test_load_conf_2(self, mock_args):
Config.clear()
Config.define_int("v1", 0, "var")
Config.define_int("nm.v2", 0, "var")
Config.load_conf()
self.assertEqual(Config.get_var("v1"), 1)
self.assertEqual(Config.get_var("nm.v2"), 2)
5 changes: 5 additions & 0 deletions distribute_config/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def set_value(self, value):
TypeError: if the type of value doesn't matche the type of the variable
"""

if self.type != str and type(value) == str:
# Try to convert
if self.type in [int, float]:
value = float(value)

if self.type == int:
# In the special case value is a int encoded in float, should convert it before loading
if int(value) == value:
Expand Down

0 comments on commit d76fa04

Please sign in to comment.