# Path: ice/persistence/config_file_backing_store.py
# class ConfigFileBackingStore(backing_store.BackingStore):
# 
#   def __init__(self, path):
#     super(ConfigFileBackingStore, self).__init__(path)
#     self.configParser = ConfigParser.RawConfigParser()
#     self.configParser.read(self.path)
# 
#   def identifiers(self):
#     return self.configParser.sections()
# 
#   def add_identifier(self, ident):
#     try:
#       self.configParser.add_section(ident)
#     except ConfigParser.DuplicateSectionError:
#       raise ValueError("The identifier `%s` already exists" % str(ident))
# 
#   def remove_identifier(self, ident):
#     self.configParser.remove_section(ident)
# 
#   def keys(self, ident):
#     try:
#       return self.configParser.options(ident)
#     except ConfigParser.NoSectionError:
#       raise ValueError("No identifier named `%s` exists" % str(ident))
# 
#   def get(self, ident, key, default=None):
#     try:
#       val = self.configParser.get(ident, key.lower())
#       return val
#     except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
#       return default
# 
#   def set(self, ident, key, value):
#     self.configParser.set(ident, key.lower(), value)
# 
#   def save(self):
#     try:
#       with open(self.path, "w") as configFile:
#         self.configParser.write(configFile)
#     except IOError:
#       raise IOError("Cannot save data to `%s`. Permission Denied")


import os
import shutil
import stat
import tempfile
import unittest
from ice.persistence.config_file_backing_store import ConfigFileBackingStore
    cfbs.save()
    self.assertTrue(os.path.isfile(self.tempfile))

  def test_save_modifies_contents_of_file(self):
    self.create_config_file(self.tempfile, {
        "Iron Man": {"identity": "Tony Stark"}
    })
    old_contents = self.file_contents(self.tempfile)
    cfbs = ConfigFileBackingStore(self.tempfile)
    cfbs.set("Iron Man", "alignment", "good")
    cfbs.add_identifier("Whiplash")
    cfbs.set("Whiplash", "alignment", "evil")
    cfbs.save()
    new_contents = self.file_contents(self.tempfile)
    self.assertNotIn("Whiplash", old_contents)
    self.assertIn("Whiplash", new_contents)
    self.assertNotIn("alignment", old_contents)
    self.assertIn("alignment", new_contents)
    self.assertIn("good", new_contents)
    self.assertIn("evil", new_contents)

  def test_save_raises_ioerror_when_cant_make_new_file(self):
    temppath = os.path.join(self.tempdir, "extra", "directories", "test.ini")
    cfbs = ConfigFileBackingStore(temppath)
    cfbs.add_identifier("Iron Man")
    cfbs.set("Iron Man", "identity", "Tony Stark")
    with self.assertRaises(IOError):
      cfbs.save()

  def test_config_file_backing_store_can_read_saved_file(self):

# Path: ice/persistence/config_file_backing_store.py
# class ConfigFileBackingStore(backing_store.BackingStore):
# 
#   def __init__(self, path):
#     super(ConfigFileBackingStore, self).__init__(path)
#     self.configParser = ConfigParser.RawConfigParser()
#     self.configParser.read(self.path)
# 
#   def identifiers(self):
#     return self.configParser.sections()
# 
#   def add_identifier(self, ident):
#     try:
#       self.configParser.add_section(ident)
#     except ConfigParser.DuplicateSectionError:
#       raise ValueError("The identifier `%s` already exists" % str(ident))
# 
#   def remove_identifier(self, ident):
#     self.configParser.remove_section(ident)
# 
#   def keys(self, ident):
#     try:
#       return self.configParser.options(ident)
#     except ConfigParser.NoSectionError:
#       raise ValueError("No identifier named `%s` exists" % str(ident))
# 
#   def get(self, ident, key, default=None):
#     try:
#       val = self.configParser.get(ident, key.lower())
#       return val
#     except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
#       return default
# 
#   def set(self, ident, key, value):
#     self.configParser.set(ident, key.lower(), value)
# 
#   def save(self):
#     try:
#       with open(self.path, "w") as configFile:
#         self.configParser.write(configFile)
#     except IOError:
#       raise IOError("Cannot save data to `%s`. Permission Denied")


# Path: tests/config_file_backing_store_tests.py
import os
import shutil
import stat
import tempfile
import unittest
from ice.persistence.config_file_backing_store import ConfigFileBackingStore

    cfbs.save()
    self.assertTrue(os.path.isfile(self.tempfile))

  def test_save_modifies_contents_of_file(self):
    self.create_config_file(self.tempfile, {
        "Iron Man": {"identity": "Tony Stark"}
    })
    old_contents = self.file_contents(self.tempfile)
    cfbs = ConfigFileBackingStore(self.tempfile)
    cfbs.set("Iron Man", "alignment", "good")
    cfbs.add_identifier("Whiplash")
    cfbs.set("Whiplash", "alignment", "evil")
    cfbs.save()
    new_contents = self.file_contents(self.tempfile)
    self.assertNotIn("Whiplash", old_contents)
    self.assertIn("Whiplash", new_contents)
    self.assertNotIn("alignment", old_contents)
    self.assertIn("alignment", new_contents)
    self.assertIn("good", new_contents)
    self.assertIn("evil", new_contents)

  def test_save_raises_ioerror_when_cant_make_new_file(self):
    temppath = os.path.join(self.tempdir, "extra", "directories", "test.ini")
    cfbs = ConfigFileBackingStore(temppath)
    cfbs.add_identifier("Iron Man")
    cfbs.set("Iron Man", "identity", "Tony Stark")
    with self.assertRaises(IOError):
      cfbs.save()

  def test_config_file_backing_store_can_read_saved_file(self):

    cfbs = ConfigFileBackingStore(self.tempfile)