# Path: kibitzr/conf.py
# class ReloadableSettings(object):
#     _instance = None
#     CONFIG_DIRS = (
#         '',
#         '~/.config/kibitzr/',
#         '~/',
#     )
#     CONFIG_FILENAME = 'kibitzr.yml'
#     RE_PUNCTUATION = re.compile(r'\W+')
#     UNNAMED_PATTERN = 'Unnamed check {0}'
# 
#     def __init__(self, config_dir):
#         self.filename = os.path.join(config_dir, self.CONFIG_FILENAME)
#         self.checks = None
#         self.creds = CompositeCreds(config_dir)
#         self.parser = SettingsParser()
#         self.reread()
# 
#     @classmethod
#     def detect_config_dir(cls):
#         candidates = [
#             (directory, os.path.join(directory, cls.CONFIG_FILENAME))
#             for directory in map(os.path.expanduser, cls.CONFIG_DIRS)
#         ]
#         for directory, file_path in candidates:
#             if os.path.exists(file_path):
#                 return directory
#         raise ConfigurationError(
#             "kibitzr.yml not found in following locations: %s"
#             % ", ".join([x[1] for x in candidates])
#         )
# 
#     @classmethod
#     def instance(cls):
#         if cls._instance is None:
#             config_dir = cls.detect_config_dir()
#             cls._instance = cls(config_dir)
#         return cls._instance
# 
#     def reread(self):
#         """
#         Read configuration file and substitute references into checks conf
#         """
#         logger.debug("Loading settings from %s",
#                      os.path.abspath(self.filename))
#         conf = self.read_conf()
#         changed = self.creds.reread()
#         checks = self.parser.parse_checks(conf)
#         if self.checks != checks:
#             self.checks = checks
#             return True
#         else:
#             return changed
# 
#     def read_conf(self):
#         """
#         Read and parse configuration file
#         """
#         with self.open_conf() as fp:
#             return yaml.safe_load(fp)
# 
#     @contextlib.contextmanager
#     def open_conf(self):
#         with open(self.filename) as fp:
#             yield fp
# 
# class PlainYamlCreds(object):
# 
#     CREDENTIALS_FILENAME = 'kibitzr-creds.yml'
# 
#     def __init__(self, config_dir):
#         super(PlainYamlCreds, self).__init__()
#         self.creds = {}
#         self.creds_filename = os.path.join(config_dir,
#                                            self.CREDENTIALS_FILENAME)
#         self.reread()
# 
#     def __contains__(self, key):
#         return key in self.creds
# 
#     def __getitem__(self, key):
#         return self.creds[key]
# 
#     def reread(self):
#         """
#         Read and parse credentials file.
#         If something goes wrong, log exception and continue.
#         """
#         logger.debug("Loading credentials from %s",
#                      os.path.abspath(self.creds_filename))
#         creds = {}
#         try:
#             with self.open_creds() as fp:
#                 creds = yaml.safe_load(fp)
#         except IOError:
#             logger.info("No credentials file found at %s",
#                         os.path.abspath(self.creds_filename))
#         except:
#             logger.exception("Error loading credentials file")
#         if creds != self.creds:
#             self.creds = creds
#             return True
#         return False
# 
#     @contextlib.contextmanager
#     def open_creds(self):
#         with open(self.creds_filename) as fp:
#             yield fp
# 
# Path: tests/compat.py



import contextlib
from kibitzr.conf import (
    ReloadableSettings,
    PlainYamlCreds,
)
from ..compat import mock



@contextlib.contextmanager
def patch_creds(data):
    with patch_source(PlainYamlCreds, 'open_creds', data) as fake_method:
        yield fake_method


@contextlib.contextmanager
def patch_conf(data):

# Path: kibitzr/conf.py
# class ReloadableSettings(object):
#     _instance = None
#     CONFIG_DIRS = (
#         '',
#         '~/.config/kibitzr/',
#         '~/',
#     )
#     CONFIG_FILENAME = 'kibitzr.yml'
#     RE_PUNCTUATION = re.compile(r'\W+')
#     UNNAMED_PATTERN = 'Unnamed check {0}'
# 
#     def __init__(self, config_dir):
#         self.filename = os.path.join(config_dir, self.CONFIG_FILENAME)
#         self.checks = None
#         self.creds = CompositeCreds(config_dir)
#         self.parser = SettingsParser()
#         self.reread()
# 
#     @classmethod
#     def detect_config_dir(cls):
#         candidates = [
#             (directory, os.path.join(directory, cls.CONFIG_FILENAME))
#             for directory in map(os.path.expanduser, cls.CONFIG_DIRS)
#         ]
#         for directory, file_path in candidates:
#             if os.path.exists(file_path):
#                 return directory
#         raise ConfigurationError(
#             "kibitzr.yml not found in following locations: %s"
#             % ", ".join([x[1] for x in candidates])
#         )
# 
#     @classmethod
#     def instance(cls):
#         if cls._instance is None:
#             config_dir = cls.detect_config_dir()
#             cls._instance = cls(config_dir)
#         return cls._instance
# 
#     def reread(self):
#         """
#         Read configuration file and substitute references into checks conf
#         """
#         logger.debug("Loading settings from %s",
#                      os.path.abspath(self.filename))
#         conf = self.read_conf()
#         changed = self.creds.reread()
#         checks = self.parser.parse_checks(conf)
#         if self.checks != checks:
#             self.checks = checks
#             return True
#         else:
#             return changed
# 
#     def read_conf(self):
#         """
#         Read and parse configuration file
#         """
#         with self.open_conf() as fp:
#             return yaml.safe_load(fp)
# 
#     @contextlib.contextmanager
#     def open_conf(self):
#         with open(self.filename) as fp:
#             yield fp
# 
# class PlainYamlCreds(object):
# 
#     CREDENTIALS_FILENAME = 'kibitzr-creds.yml'
# 
#     def __init__(self, config_dir):
#         super(PlainYamlCreds, self).__init__()
#         self.creds = {}
#         self.creds_filename = os.path.join(config_dir,
#                                            self.CREDENTIALS_FILENAME)
#         self.reread()
# 
#     def __contains__(self, key):
#         return key in self.creds
# 
#     def __getitem__(self, key):
#         return self.creds[key]
# 
#     def reread(self):
#         """
#         Read and parse credentials file.
#         If something goes wrong, log exception and continue.
#         """
#         logger.debug("Loading credentials from %s",
#                      os.path.abspath(self.creds_filename))
#         creds = {}
#         try:
#             with self.open_creds() as fp:
#                 creds = yaml.safe_load(fp)
#         except IOError:
#             logger.info("No credentials file found at %s",
#                         os.path.abspath(self.creds_filename))
#         except:
#             logger.exception("Error loading credentials file")
#         if creds != self.creds:
#             self.creds = creds
#             return True
#         return False
# 
#     @contextlib.contextmanager
#     def open_creds(self):
#         with open(self.creds_filename) as fp:
#             yield fp
# 
# Path: tests/compat.py



# Path: tests/unit/helpers.py
import contextlib
from kibitzr.conf import (
    ReloadableSettings,
    PlainYamlCreds,
)
from ..compat import mock




@contextlib.contextmanager
def patch_creds(data):
    with patch_source(PlainYamlCreds, 'open_creds', data) as fake_method:
        yield fake_method


@contextlib.contextmanager
def patch_conf(data):

    with patch_source(ReloadableSettings, 'open_conf', data) as fake_method: