Merge "netsted template validate type error"
[parser.git] / tosca2heat / tosca-parser / toscaparser / extensions / exttools.py
1 #
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
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
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
12 # under the License.
13
14 import importlib
15 import logging
16 import os
17
18 from toscaparser.common.exception import ToscaExtAttributeError
19 from toscaparser.common.exception import ToscaExtImportError
20
21 log = logging.getLogger("tosca.model")
22
23 REQUIRED_ATTRIBUTES = ['VERSION', 'DEFS_FILE']
24
25
26 class ExtTools(object):
27     def __init__(self):
28         self.EXTENSION_INFO = self._load_extensions()
29
30     def _load_extensions(self):
31         '''Dynamically load all the extensions .'''
32         extensions = {}
33
34         # Use the absolute path of the class path
35         abs_path = os.path.dirname(os.path.abspath(__file__))
36
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')]
40
41         for e in extdirs:
42             log.info(e)
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__')]
47
48             # For each module, pick out the target translation class
49             for f in ext_files:
50                 log.info(f)
51                 ext_name = 'toscaparser/extensions/' + e + '/' + f.strip('.py')
52                 ext_name = ext_name.replace('/', '.')
53                 try:
54                     extinfo = importlib.import_module(ext_name)
55                     version = getattr(extinfo, 'VERSION')
56                     defs_file = extpath + '/' + getattr(extinfo, 'DEFS_FILE')
57
58                     # Sections is an optional attribute
59                     sections = getattr(extinfo, 'SECTIONS', ())
60
61                     extensions[version] = {'sections': sections,
62                                            'defs_file': defs_file}
63                 except ImportError:
64                     raise ToscaExtImportError(ext_name=ext_name)
65                 except AttributeError:
66                     attrs = ', '.join(REQUIRED_ATTRIBUTES)
67                     raise ToscaExtAttributeError(ext_name=ext_name,
68                                                  attrs=attrs)
69
70         return extensions
71
72     def get_versions(self):
73         return self.EXTENSION_INFO.keys()
74
75     def get_sections(self):
76         sections = {}
77         for version in self.EXTENSION_INFO.keys():
78             sections[version] = self.EXTENSION_INFO[version]['sections']
79
80         return sections
81
82     def get_defs_file(self, version):
83         versiondata = self.EXTENSION_INFO.get(version)
84
85         if versiondata:
86             return versiondata.get('defs_file')
87         else:
88             return None