1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from . import ROOT_DIR
20 from .resources import DIR as RESOURCES_DIR
23 def get_example_uri(*args):
24 return os.path.join(ROOT_DIR, 'examples', *args)
27 def get_resource_uri(*args):
28 return os.path.join(RESOURCES_DIR, *args)
31 def get_service_template_uri(*args):
32 return os.path.join(RESOURCES_DIR, 'service-templates', *args)
35 class FilesystemDataHolder(object):
37 def __init__(self, path, reset=False):
39 if reset or not os.path.exists(self._path) or open(self._path).read() == '':
43 with open(self._path) as f:
46 def _dump(self, value):
47 with open(self._path, 'w') as f:
48 return json.dump(value, f)
50 def __contains__(self, item):
51 return item in self._load()
53 def __setitem__(self, key, value):
58 def __getitem__(self, item):
59 return self._load()[item]
62 return iter(self._load())
64 def get(self, item, default=None):
65 return self._load().get(item, default)
67 def setdefault(self, key, value):
69 return_value = dict_.setdefault(key, value)
73 def update(self, dict_=None, **kwargs):
74 current_dict = self._load()
76 current_dict.update(dict_)
77 current_dict.update(**kwargs)
78 self._dump(current_dict)