summaryrefslogtreecommitdiff
path: root/baserockimport/morphsetondisk.py
blob: cd69748370542ad28df52c0dbc370a09c89bcf33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (C) 2014  Codethink Limited
#
# This program 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; version 2 of the License.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import morphlib

import copy
import logging
import os


class MorphologySetOnDisk(morphlib.morphset.MorphologySet):
    '''Extensions to the morphlib.MorphologySet class.

    The base class deals only with reading morphologies into memory. This class
    extends it to support reading and writing them from disk.

    FIXME: this should perhaps be merged into the base class in morphlib.

    '''

    def __init__(self, path):
        super(MorphologySetOnDisk, self).__init__()

        self.path = path
        self.loader = morphlib.morphloader.MorphologyLoader()

        if os.path.exists(path):
            self.load_all_morphologies()
        else:
            os.makedirs(path)
            morphlib.gitdir.init(path)

    def load_all_morphologies(self):
        logging.info('Loading all .morph files under %s', self.path)

        gitdir = morphlib.gitdir.GitDirectory(self.path)
        finder = morphlib.morphologyfinder.MorphologyFinder(gitdir)
        for filename in (f for f in finder.list_morphologies()
                         if not gitdir.is_symlink(f)):
            text = finder.read_file(filename)
            morph = self.loader.load_from_string(text, filename=filename)
            morph.repo_url = None  # self.root_repository_url
            morph.ref = None  # self.system_branch_name
            self.add_morphology(morph)

    def get_morphology(self, repo_url, ref, filename):
        return self._get_morphology(repo_url, ref, filename)

    def save_morphology(self, filename, morphology):
        self.add_morphology(morphology)
        morphology_to_save = copy.copy(morphology)
        filename = os.path.join(self.path, filename)
        self.loader.save_to_file(filename, morphology_to_save)