summaryrefslogtreecommitdiff
path: root/ick2/state.py
blob: 8fd00c6d2f84a707ced7d49aa07004d028937d41 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2017  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import glob
import os


import apifw
import yaml


import ick2


class ControllerState:

    def __init__(self):
        self._statedir = None

    def get_state_directory(self):
        return self._statedir

    def set_state_directory(self, dirname):
        self._statedir = dirname
        if not os.path.exists(self._statedir):
            os.makedirs(self._statedir)

    def get_project_directory(self):
        return os.path.join(self.get_state_directory(), 'projects')

    def get_project_filename(self, project_name):
        return os.path.join(
            self.get_project_directory(), project_name + '.yaml')

    def load_projects(self):
        assert self._statedir is not None
        projects = []
        dirname = self.get_project_directory()
        for filename in glob.glob(dirname + '/*.yaml'):
            obj = self.load_project(filename)
            projects.append(obj)
        return projects

    def load_project(self, filename):
        with open(filename, 'r') as f:
            return yaml.safe_load(f)

    def get_projects(self):
        return self.load_projects()

    def get_project(self, project):
        filename = self.get_project_filename(project)
        if os.path.exists(filename):
            return self.load_project(filename)
        raise NotFound()

    def add_project(self, project):
        filename = self.get_project_filename(project['project'])
        dirname = os.path.dirname(filename)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(filename, 'w') as f:
            yaml.safe_dump(project, stream=f)
        return project

    def update_project(self, project, body):
        filename = self.get_project_filename(project)
        dirname = os.path.dirname(filename)
        with open(filename, 'w') as f:
            yaml.safe_dump(body, stream=f)
        return body

    def remove_project(self, project):
        filename = self.get_project_filename(project)
        if not os.path.exists(filename):
            raise NotFound()
        os.remove(filename)


class NotFound(Exception):

    def __init__(self):
        super().__init__('Resource not found')