2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
18 from toscaparser.common.exception import ToscaExtAttributeError
19 from toscaparser.common.exception import ToscaExtImportError
21 log = logging.getLogger("tosca.model")
23 REQUIRED_ATTRIBUTES = ['VERSION', 'DEFS_FILE']
26 class ExtTools(object):
28 self.EXTENSION_INFO = self._load_extensions()
30 def _load_extensions(self):
31 '''Dynamically load all the extensions .'''
34 # Use the absolute path of the class path
35 abs_path = os.path.dirname(os.path.abspath(__file__))
37 extdirs = [e for e in os.listdir(abs_path) if
38 not e.startswith('tests') and
39 not e.endswith('.pyc') and not e.endswith('.py')]
43 extpath = abs_path + '/' + e
44 # Grab all the extension files in the given path
45 ext_files = [f for f in os.listdir(extpath) if f.endswith('.py')
46 and not f.startswith('__init__')]
48 # For each module, pick out the target translation class
51 ext_name = 'toscaparser/extensions/' + e + '/' + f.strip('.py')
52 ext_name = ext_name.replace('/', '.')
54 extinfo = importlib.import_module(ext_name)
55 version = getattr(extinfo, 'VERSION')
56 defs_file = extpath + '/' + getattr(extinfo, 'DEFS_FILE')
58 # Sections is an optional attribute
59 sections = getattr(extinfo, 'SECTIONS', ())
61 extensions[version] = {'sections': sections,
62 'defs_file': defs_file}
64 raise ToscaExtImportError(ext_name=ext_name)
65 except AttributeError:
66 attrs = ', '.join(REQUIRED_ATTRIBUTES)
67 raise ToscaExtAttributeError(ext_name=ext_name,
72 def get_versions(self):
73 return self.EXTENSION_INFO.keys()
75 def get_sections(self):
77 for version in self.EXTENSION_INFO.keys():
78 sections[version] = self.EXTENSION_INFO[version]['sections']
82 def get_defs_file(self, version):
83 versiondata = self.EXTENSION_INFO.get(version)
86 return versiondata.get('defs_file')