# Path: repoxplorer/index/yamlbackend.py
# class YAMLBackend(object):
#     def __init__(self, db_path, db_default_file=None, db_cache_path=None):
#         """ Class to read YAML files from a DB path.
#         db_default_file: is the path to a trusted file usually
#             computed from an already verified data source.
#         db_path: directory where data can be read. This is
#             supposed to be user provided data to be verified
#             by the caller and could overwrite data from the
#             default_file.
#         db_cache_path: directory to store cache files
#         """
#         self.db_path = db_path or conf.get('db_path')
#         self.db_default_file = db_default_file
#         self.db_cache_path = (
#             db_cache_path or conf.get('db_cache_path') or self.db_path)
# 
#         self.default_data = None
#         self.data = []
#         # List of hashes
#         self.hashes = []
# 
#     def load_db(self):
#         def check_ext(f):
#             return f.endswith('.yaml') or f.endswith('.yml')
# 
#         def load(path):
#             data = None
#             logger.debug("Check cache for %s ..." % path)
#             basename = os.path.basename(path)
#             if not os.path.isdir(self.db_path):
#                 os.makedirs(self.db_path)
#             if self.db_cache_path and not os.path.isdir(self.db_cache_path):
#                 os.makedirs(self.db_cache_path)
#             cached_hash_path = os.path.join(
#                 self.db_cache_path, basename + '.hash')
#             cached_data_path = os.path.join(
#                 self.db_cache_path, basename + '.cached')
#             hash = SHA.new(open(path).read().encode(
#                 errors='ignore')).hexdigest()
#             if (os.path.isfile(cached_hash_path) and
#                     os.path.isfile(cached_data_path)):
#                 cached_hash = pickle.load(open(cached_hash_path, 'rb'))
#                 if cached_hash == hash:
#                     logger.debug("Reading %s from cache ..." % path)
#                     data = pickle.load(open(cached_data_path, 'rb'))
#                     self.hashes.append(hash)
#             if not data:
#                 logger.debug("Reading %s from file ..." % path)
#                 try:
#                     data = yaml.load(
#                         open(path, 'rb'), Loader=NoDatesSafeLoader)
#                 except Exception as e:
#                     raise YAMLDBException(
#                         "YAML format corrupted in file %s (%s)" % (path, e))
#                 pickle.dump(
#                     data, open(cached_data_path, 'wb'),
#                     pickle.HIGHEST_PROTOCOL)
#                 pickle.dump(
#                     hash, open(cached_hash_path, 'wb'),
#                     pickle.HIGHEST_PROTOCOL)
#                 self.hashes.append(hash)
#             return data
# 
#         if self.db_default_file:
#             self.default_data = load(self.db_default_file)
# 
#         yamlfiles = [f for f in os.listdir(self.db_path) if check_ext(f)]
#         yamlfiles.sort()
#         for f in yamlfiles:
#             path = os.path.join(self.db_path, f)
#             if path == self.db_default_file:
#                 continue
#             self.data.append(load(path))
# 
#     def get_data(self):
#         """ Return the full raw data structure.
#         """
#         return self.default_data, self.data


import os
import shutil
import tempfile
from unittest import TestCase
from repoxplorer.index.yamlbackend import YAMLBackend





class TestYAMLBackend(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        if os.path.isdir(self.db):
            shutil.rmtree(self.db)

    def create_db(self, files):
        self.db = tempfile.mkdtemp()
        for filename, content in files.items():
            open(os.path.join(self.db, filename), 'w+').write(content)

    def test_yamlbackend_load(self):
        f1 = """
---
key: value
"""
        f2 = """
---
key2: value2
"""
        files = {'f1.yaml': f1, 'f2.yaml': f2}
        self.create_db(files)

# Path: repoxplorer/index/yamlbackend.py
# class YAMLBackend(object):
#     def __init__(self, db_path, db_default_file=None, db_cache_path=None):
#         """ Class to read YAML files from a DB path.
#         db_default_file: is the path to a trusted file usually
#             computed from an already verified data source.
#         db_path: directory where data can be read. This is
#             supposed to be user provided data to be verified
#             by the caller and could overwrite data from the
#             default_file.
#         db_cache_path: directory to store cache files
#         """
#         self.db_path = db_path or conf.get('db_path')
#         self.db_default_file = db_default_file
#         self.db_cache_path = (
#             db_cache_path or conf.get('db_cache_path') or self.db_path)
# 
#         self.default_data = None
#         self.data = []
#         # List of hashes
#         self.hashes = []
# 
#     def load_db(self):
#         def check_ext(f):
#             return f.endswith('.yaml') or f.endswith('.yml')
# 
#         def load(path):
#             data = None
#             logger.debug("Check cache for %s ..." % path)
#             basename = os.path.basename(path)
#             if not os.path.isdir(self.db_path):
#                 os.makedirs(self.db_path)
#             if self.db_cache_path and not os.path.isdir(self.db_cache_path):
#                 os.makedirs(self.db_cache_path)
#             cached_hash_path = os.path.join(
#                 self.db_cache_path, basename + '.hash')
#             cached_data_path = os.path.join(
#                 self.db_cache_path, basename + '.cached')
#             hash = SHA.new(open(path).read().encode(
#                 errors='ignore')).hexdigest()
#             if (os.path.isfile(cached_hash_path) and
#                     os.path.isfile(cached_data_path)):
#                 cached_hash = pickle.load(open(cached_hash_path, 'rb'))
#                 if cached_hash == hash:
#                     logger.debug("Reading %s from cache ..." % path)
#                     data = pickle.load(open(cached_data_path, 'rb'))
#                     self.hashes.append(hash)
#             if not data:
#                 logger.debug("Reading %s from file ..." % path)
#                 try:
#                     data = yaml.load(
#                         open(path, 'rb'), Loader=NoDatesSafeLoader)
#                 except Exception as e:
#                     raise YAMLDBException(
#                         "YAML format corrupted in file %s (%s)" % (path, e))
#                 pickle.dump(
#                     data, open(cached_data_path, 'wb'),
#                     pickle.HIGHEST_PROTOCOL)
#                 pickle.dump(
#                     hash, open(cached_hash_path, 'wb'),
#                     pickle.HIGHEST_PROTOCOL)
#                 self.hashes.append(hash)
#             return data
# 
#         if self.db_default_file:
#             self.default_data = load(self.db_default_file)
# 
#         yamlfiles = [f for f in os.listdir(self.db_path) if check_ext(f)]
#         yamlfiles.sort()
#         for f in yamlfiles:
#             path = os.path.join(self.db_path, f)
#             if path == self.db_default_file:
#                 continue
#             self.data.append(load(path))
# 
#     def get_data(self):
#         """ Return the full raw data structure.
#         """
#         return self.default_data, self.data


# Path: repoxplorer/tests/test_yamlbackend.py
import os
import shutil
import tempfile
from unittest import TestCase
from repoxplorer.index.yamlbackend import YAMLBackend






class TestYAMLBackend(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        if os.path.isdir(self.db):
            shutil.rmtree(self.db)

    def create_db(self, files):
        self.db = tempfile.mkdtemp()
        for filename, content in files.items():
            open(os.path.join(self.db, filename), 'w+').write(content)

    def test_yamlbackend_load(self):
        f1 = """
---
key: value
"""
        f2 = """
---
key2: value2
"""
        files = {'f1.yaml': f1, 'f2.yaml': f2}
        self.create_db(files)

        backend = YAMLBackend(db_path=self.db)