loader.py 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os

24
from ansible.errors import AnsibleParserError
25
from ansible.parsing import DataLoader
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

class DictDataLoader(DataLoader):

    def __init__(self, file_mapping=dict()):
        assert type(file_mapping) == dict

        self._file_mapping = file_mapping
        self._build_known_directories()

        super(DictDataLoader, self).__init__()

    def load_from_file(self, path):
        if path in self._file_mapping:
            return self.load(self._file_mapping[path], path)
        return None

42 43
    def _get_file_contents(self, path):
        if path in self._file_mapping:
44
            return (self._file_mapping[path], False)
45 46 47
        else:
            raise AnsibleParserError("file not found: %s" % path)

48 49 50 51 52 53 54 55 56
    def path_exists(self, path):
        return path in self._file_mapping or path in self._known_directories

    def is_file(self, path):
        return path in self._file_mapping

    def is_directory(self, path):
        return path in self._known_directories

James Cammarata committed
57 58 59
    def list_directory(self, path):
        return [x for x in self._known_directories]

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    def _add_known_directory(self, directory):
        if directory not in self._known_directories:
            self._known_directories.append(directory)

    def _build_known_directories(self):
        self._known_directories  = []
        for path in self._file_mapping:
            dirname = os.path.dirname(path)
            while dirname not in ('/', ''):
                self._add_known_directory(dirname)
                dirname = os.path.dirname(dirname)

    def push(self, path, content):
        rebuild_dirs = False
        if path not in self._file_mapping:
            rebuild_dirs = True
    
        self._file_mapping[path] = content

        if rebuild_dirs:
            self._build_known_directories()

    def pop(self, path):
        if path in self._file_mapping:
            del self._file_mapping[path]
            self._build_known_directories()

    def clear(self):
        self._file_mapping = dict()
        self._known_directories = []