b9d72e58ffdc43a7ef387fee632298b5f85a24f5
[h5config.git] / h5config / storage / __init__.py
1 # Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of h5config.
4 #
5 # h5config is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # h5config is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with h5config.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 class Storage (object):
20     "A storage bakend for loading and saving `Config` instances"
21     def load(self, config, merge=False, **kwargs):
22         if merge:
23             self.clear()
24         self._load(config=config, **kwargs)
25         config._storage = self
26
27     def _load(self, config, **kwargs):
28         raise NotImplementedError()
29
30     def save(self, config, merge=False, **kwargs):
31         if merge:
32             self.clear()
33         self._save(config=config, **kwargs)
34         config._storage = self
35
36     def _save(self, config, **kwargs):
37         raise NotImplementedError()
38
39
40 class FileStorage (Storage):
41     "`Config` storage backend by a single file"
42     extension = None
43
44     def __init__(self, filename=None):
45         self._filename = filename