# Path: snaked/core/prefs.py
# class PySettings(object):
#     def __init__(self, options=None, parent=None):
#         assert options or parent
#         if parent:
#             self.parent = parent
#             self.options = parent.options
#         else:
#             self.options = options
#             self.parent = None
# 
#         self.data = {}
# 
#     def __getitem__(self, name):
#         try:
#             return self.data[name]
#         except KeyError:
#             pass
# 
#         if self.parent:
#             v = self.parent[name]
#             if isinstance(v, list):
#                 v = v[:]
#             elif isinstance(v, dict):
#                 v = v.copy()
#         else:
#             v = self.get_default(name)
# 
#         self.data[name] = v
#         return v
# 
#     def __contains__(self, name):
#         return name in self.options
# 
#     def get_default(self, name):
#         value = self.options[name][0]
#         if callable(value):
#             value = value()
# 
#         return value
# 
#     def __setitem__(self, name, value):
#         self.data[name] = value
# 
#     def get_config(self):
#         result = ''
#         for name in sorted(set(chain(self.data, self.options))):
#             doc = cleandoc(self.options.get(name, (0, 'Unknown option'))[1])
#             if doc:
#                 for l in doc.splitlines():
#                     result += '# ' + l + '\n'
# 
#             if name not in self.options:
#                 value = self.data[name]
#                 is_default = False
#             elif name not in self.data:
#                 is_default = True
#                 if self.parent:
#                     value = self.parent[name]
#                 else:
#                     value = self.get_default(name)
#             else:
#                 value = self.data[name]
#                 if (self.parent and value == self.parent[name]) or (
#                         not self.parent and value == self.get_default(name)):
#                     is_default = True
#                 else:
#                     is_default = False
# 
#             value = '%s = %s' % (name, repr(value))
#             if is_default:
#                 value = '# ' + value
# 
#             result += value + '\n\n'
# 
#         return result
# 
#     def load(self, filename):
#         self.filename = filename
#         self.data.clear()
# 
#         if self.parent:
#             self.data['default'] = DefaultValuesProvider(self.parent)
# 
#         try:
#             execfile(self.filename, self.data)
#         except IOError:
#             pass
#         except SyntaxError, e:
#             print 'Error on loading config: %s' % self.filename, e
# 
#         try:
#             del self.data['__builtins__']
#         except KeyError:
#             pass
# 
#         if self.parent:
#             del self.data['default']
# 
#     def save(self):
#         with open(self.filename, 'w') as f:
#             f.write(self.get_config())


from snaked.core.prefs import PySettings

def make_settings(**kwargs):
    return PySettings(dict((k, (v, '')) for k,v in kwargs.items()))

def test_prefs_must_return_default_values():
    p = make_settings(key1='key1', key2='key2')
    assert p['key1'] == 'key1'
    assert p['key2'] == 'key2'

def test_prefs_must_return_parent_values():
    p = make_settings(key1='key1', key2='key2')

# Path: snaked/core/prefs.py
# class PySettings(object):
#     def __init__(self, options=None, parent=None):
#         assert options or parent
#         if parent:
#             self.parent = parent
#             self.options = parent.options
#         else:
#             self.options = options
#             self.parent = None
# 
#         self.data = {}
# 
#     def __getitem__(self, name):
#         try:
#             return self.data[name]
#         except KeyError:
#             pass
# 
#         if self.parent:
#             v = self.parent[name]
#             if isinstance(v, list):
#                 v = v[:]
#             elif isinstance(v, dict):
#                 v = v.copy()
#         else:
#             v = self.get_default(name)
# 
#         self.data[name] = v
#         return v
# 
#     def __contains__(self, name):
#         return name in self.options
# 
#     def get_default(self, name):
#         value = self.options[name][0]
#         if callable(value):
#             value = value()
# 
#         return value
# 
#     def __setitem__(self, name, value):
#         self.data[name] = value
# 
#     def get_config(self):
#         result = ''
#         for name in sorted(set(chain(self.data, self.options))):
#             doc = cleandoc(self.options.get(name, (0, 'Unknown option'))[1])
#             if doc:
#                 for l in doc.splitlines():
#                     result += '# ' + l + '\n'
# 
#             if name not in self.options:
#                 value = self.data[name]
#                 is_default = False
#             elif name not in self.data:
#                 is_default = True
#                 if self.parent:
#                     value = self.parent[name]
#                 else:
#                     value = self.get_default(name)
#             else:
#                 value = self.data[name]
#                 if (self.parent and value == self.parent[name]) or (
#                         not self.parent and value == self.get_default(name)):
#                     is_default = True
#                 else:
#                     is_default = False
# 
#             value = '%s = %s' % (name, repr(value))
#             if is_default:
#                 value = '# ' + value
# 
#             result += value + '\n\n'
# 
#         return result
# 
#     def load(self, filename):
#         self.filename = filename
#         self.data.clear()
# 
#         if self.parent:
#             self.data['default'] = DefaultValuesProvider(self.parent)
# 
#         try:
#             execfile(self.filename, self.data)
#         except IOError:
#             pass
#         except SyntaxError, e:
#             print 'Error on loading config: %s' % self.filename, e
# 
#         try:
#             del self.data['__builtins__']
#         except KeyError:
#             pass
# 
#         if self.parent:
#             del self.data['default']
# 
#     def save(self):
#         with open(self.filename, 'w') as f:
#             f.write(self.get_config())


# Path: tests/test_pysettings.py
from snaked.core.prefs import PySettings


def make_settings(**kwargs):
    return PySettings(dict((k, (v, '')) for k,v in kwargs.items()))

def test_prefs_must_return_default_values():
    p = make_settings(key1='key1', key2='key2')
    assert p['key1'] == 'key1'
    assert p['key2'] == 'key2'

def test_prefs_must_return_parent_values():
    p = make_settings(key1='key1', key2='key2')

    pp = PySettings(parent=p)