Skip to content

Commit

Permalink
ioerror, init
Browse files Browse the repository at this point in the history
  • Loading branch information
manuphatak committed Apr 14, 2015
1 parent d86a4eb commit 75b3a86
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
17 changes: 17 additions & 0 deletions json_config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#!/usr/bin/env python
# coding=utf-8
"""
===========
JSON Config
===========
A convenience utility for working with json config files.
>>> import json_config
>>> config = json_config.connect('test.json')
>>> config['root'] = '/var/www/html'
>>> config
{
"root": "/var/www/html"
}
"""
__author__ = 'Manu Phatak'
__email__ = 'bionikspoon@gmail.com'
__version__ = '0.1.0'

from .configuration import connect
22 changes: 12 additions & 10 deletions json_config/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ def connect(config_file_):

config_file = config_file_
config = Configuration()
config.update(json.load(open(config_file), object_hook=json_hook))

try:
config.update(json.load(open(config_file), object_hook=json_hook))
except IOError:
open(config_file, 'w').close()

return config


Expand All @@ -32,6 +37,7 @@ def json_hook(obj):
config_.update(**obj)
return config_


class Configuration(defaultdict, MutableMapping):
"""
Internal class. Handles nested dictionary recursion.
Expand All @@ -48,12 +54,13 @@ def save_config(*func_args):

@wraps(func)
def _wrapper(self, *args, **kwargs):

try:
return func(self, *args, **kwargs)

finally:
json.dump(config, open(config_file, 'wb'), indent=2,
sort_keys=True, separators=(',', ': '))
json.dump(config, open(config_file, 'wb'), indent=2, sort_keys=True,
separators=(',', ': '))

return _wrapper

Expand All @@ -73,14 +80,9 @@ def __getitem__(self, key):
def __delitem__(self, key):
super(Configuration, self).__delitem__(key)

def __str__(self):
return json.dumps(self, indent=2, sort_keys=True,
separators=(',', ': '))

def __repr__(self):
return json.dumps(self, indent=2, sort_keys=True, separators=(',', ': '))

@classmethod
def loads(cls, *args):
pass

@staticmethod
def factory():
Expand Down
14 changes: 12 additions & 2 deletions test/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def test_loads_json_file_returns_dict_like_obj(config, tmpdir):
from json_config import configuration

assert config['test'] == 'success'
assert configuration.config_file == tmpdir.join(
'sample_config.json').strpath
assert configuration.config_file == tmpdir.join('sample_config.json').strpath


def test_abc_magic_methods_length():
Expand Down Expand Up @@ -103,3 +102,14 @@ def test_saves_on_nested_delete(config, config_file):
del config['test']
expected = json.load(open(config_file))
assert expected.get('test') is None


def test_create_new_json_file(tmpdir):
from json_config import configuration

config = configuration.connect(tmpdir.join('unique_file.json').strpath)

config['unique'] = 'success'
actual = json.load((open(tmpdir.join('unique_file.json').strpath)))

assert actual == dict(config)

0 comments on commit 75b3a86

Please sign in to comment.