# 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
# 
# Path: kibitzr/stash.py
# class Stash(object):
# 
#     FILENAME = 'stash.db'
# 
#     @contextlib.contextmanager
#     def open(self):
#         import shelve
#         with contextlib.closing(shelve.open(self.FILENAME)) as db:
#             yield db
# 
#     def read(self):
#         with self.open() as db:
#             return dict(db)
# 
#     def write(self, data):
#         with self.open() as db:
#             for key, value in data.items():
#                 db[key] = value
# 
#     @classmethod
#     def print_content(cls):
#         for key, value in cls().read().items():
#             print("{0}: {1}".format(key, value))
# 
# Path: tests/compat.py



import contextlib
import tempfile
from kibitzr.conf import ReloadableSettings
from kibitzr.stash import Stash
from .compat import mock




class SettingsMock(ReloadableSettings):

    def __init__(self):
        self.checks = []
        self.notifiers = {}
        self.creds = {'pass': 'password'}

    @classmethod
    def instance(cls):
        ReloadableSettings._instance = cls()
        return ReloadableSettings._instance

    @staticmethod
    def dispose():
        ReloadableSettings._instance = None


@contextlib.contextmanager
def stash_mock():
    with tempfile.NamedTemporaryFile() as fp:
        fp.close()

# 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
# 
# Path: kibitzr/stash.py
# class Stash(object):
# 
#     FILENAME = 'stash.db'
# 
#     @contextlib.contextmanager
#     def open(self):
#         import shelve
#         with contextlib.closing(shelve.open(self.FILENAME)) as db:
#             yield db
# 
#     def read(self):
#         with self.open() as db:
#             return dict(db)
# 
#     def write(self, data):
#         with self.open() as db:
#             for key, value in data.items():
#                 db[key] = value
# 
#     @classmethod
#     def print_content(cls):
#         for key, value in cls().read().items():
#             print("{0}: {1}".format(key, value))
# 
# Path: tests/compat.py



# Path: tests/helpers.py
import contextlib
import tempfile
from kibitzr.conf import ReloadableSettings
from kibitzr.stash import Stash
from .compat import mock





class SettingsMock(ReloadableSettings):

    def __init__(self):
        self.checks = []
        self.notifiers = {}
        self.creds = {'pass': 'password'}

    @classmethod
    def instance(cls):
        ReloadableSettings._instance = cls()
        return ReloadableSettings._instance

    @staticmethod
    def dispose():
        ReloadableSettings._instance = None


@contextlib.contextmanager
def stash_mock():
    with tempfile.NamedTemporaryFile() as fp:
        fp.close()

        with mock.patch.object(Stash, 'FILENAME', fp.name):