import os

from parallels.plesk.source.plesk.hosting_description.dump.base_dump import BaseDump


class RepoDump(BaseDump):
    """
    The dump is located in Plesk repository
    """

    def __init__(self, path, backup_prefix=None, backup_id=None):
        self._indexed_entities = None
        super(RepoDump, self).__init__(path, backup_prefix, backup_id)

    def _list_files(self):
        """
        :return: [str|unicode]
        """
        list_files = []
        for _, _, files in os.walk(self._path):
            for f in files:
                list_files.append(f)
        return list_files

    def _get_content(self, name):
        """
        :type name: str|unicode
        :return: str|unicode
        """
        entry_full_path = os.path.join(self._path, name)
        with open(entry_full_path, 'r') as f:
            return f.read()

    def _path_exists(self, path):
        """
        :type path: str|unicode
        :return: bool
        """
        full_path = os.path.join(self._path, path)
        return os.path.exists(full_path)

    def _list_dir(self, path):
        """
        :type path: str|unicode
        :return: [str|unicode]
        """
        full_path = os.path.join(self._path, path)
        return os.listdir(full_path)

    def _is_dir(self, path):
        """
        :type path: str|unicode
        :return: bool
        """
        full_path = os.path.join(self._path, path)
        return os.path.isdir(full_path)

    def _join_path(self, path1, path2):
        """
        :type path1: str|unicode
        :type path2: str|unicode
        :return: str|unicode
        """
        return os.path.join(path1, path2)

    def _path_sep(self):
        """
        :return: str|unicode
        """
        return os.path.sep
