aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/extensions/ExtTools.java
blob: 6403d6e24318eb5c2ee1676b9b2557718fe96f5a (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package org.openecomp.sdc.toscaparser.api.extensions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtTools {

	private static Logger log = LoggerFactory.getLogger(ExtTools.class.getName());

	private static LinkedHashMap<String,Object> EXTENSION_INFO = new LinkedHashMap<>();
	
	public ExtTools() {
		
        EXTENSION_INFO = _loadExtensions();
	}
	
	private LinkedHashMap<String,Object> _loadExtensions() {
		
		LinkedHashMap<String,Object> extensions = new LinkedHashMap<>();
		
		String path = ExtTools.class.getProtectionDomain().getCodeSource().getLocation().getPath();
	    //String extdir = path + File.separator + "resources/extensions";
	    
	    String extdir = ExtTools.class.getClassLoader().getResource("extensions").getFile();
	    
	    // for all folders in extdir
	    File extDir = new File(extdir);
	    File extDirList[] = extDir.listFiles();
	    if (extDirList == null)  {
	    	String a = "aaaa";
	    	
	    }
	    if (extDirList != null) {
		    for(File f: extDirList) {
		    	if(f.isDirectory()) {
		  	        // for all .py files in folder
		    		File extFileList[] = f.listFiles();
		    		for(File pyf: extFileList) {
		    			String pyfName = pyf.getName();
		    			String pyfPath = pyf.getAbsolutePath();
		    			if(pyfName.endsWith(".py")) {
		    				// get VERSION,SECTIONS,DEF_FILE
		    				try {
			    				String version = null;
			    				ArrayList<String> sections = null;
			    				String defsFile = null;
		    				    String line;
		    					InputStream fis = new FileInputStream(pyfPath);
		    				    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
		    				    BufferedReader br = new BufferedReader(isr);
		    					Pattern pattern = Pattern.compile("^([^#]\\S+)\\s*=\\s*(\\S.*)$");
	    					    while((line = br.readLine()) != null) {
		    					    line = line.replace("'","\"");
	    	    					Matcher matcher = pattern.matcher(line.toString());
	    	    					if(matcher.find()) {
	    	    						if(matcher.group(1).equals("VERSION")) {
	    	    							version = matcher.group(2);
	    	    							if(version.startsWith("'") || version.startsWith("\"")) {
	    	    								version = version.substring(1,version.length()-1);
	    	    							}
	    	    						}
	    	    						else if(matcher.group(1).equals("DEFS_FILE")) {
	    	    							String fn = matcher.group(2);
	    	    							if(fn.startsWith("'") || fn.startsWith("\"")) {
	    	    								fn = fn.substring(1,fn.length()-1);
	    	    							}
	    	    							defsFile = pyf.getParent() + File.separator + fn;//matcher.group(2);
	    	    						}
	    	    						else if(matcher.group(1).equals("SECTIONS")) {
	    	    							sections = new ArrayList<>();
	    	    							Pattern secpat = Pattern.compile("\"([^\"]+)\"");
	    	    	    					Matcher secmat = secpat.matcher(matcher.group(2));
	    	    	    					while(secmat.find()) {
	    	    	    						sections.add(secmat.group(1));
	    	    	    					}
	    	    						}
	    	    					}
		    					}
	    					    br.close();
	    					    
	    					    if(version != null && defsFile != null) {
	    					    	LinkedHashMap<String,Object> ext = new LinkedHashMap<>();
	    	    					ext.put("defs_file", defsFile);
	    	    					if(sections != null) {
	        	    					ext.put("sections", sections);
	    	    					}
	    	    					extensions.put(version, ext);
		    					}
	    					    else {
	    					    	// error
	    					    }
		    				}
		    				catch(Exception e) {
		    					log.error("ExtTools - _loadExtensions - {}", e.getMessage());
		    					// ...
		    				}
		    			}
		    		}
		    	}
		    }
	    }
		return extensions;
	}
	
	public ArrayList<String> getVersions() {
		return new ArrayList<String>(EXTENSION_INFO.keySet());
	}
	
	public LinkedHashMap<String,ArrayList<String>> getSections() {
		LinkedHashMap<String,ArrayList<String>> sections = new LinkedHashMap<>();
        for(String version: EXTENSION_INFO.keySet()) {
        	LinkedHashMap<String,Object> eiv = (LinkedHashMap<String,Object>)EXTENSION_INFO.get(version);
        	sections.put(version,(ArrayList<String>)eiv.get("sections"));
        }
        return sections;
	}

	public String getDefsFile(String version) { 
    	LinkedHashMap<String,Object> eiv = (LinkedHashMap<String,Object>)EXTENSION_INFO.get(version);
    	return (String)eiv.get("defs_file");
	}
	
}

/*python

from toscaparser.common.exception import ToscaExtAttributeError
from toscaparser.common.exception import ToscaExtImportError

log = logging.getLogger("tosca.model")

REQUIRED_ATTRIBUTES = ['VERSION', 'DEFS_FILE']


class ExtTools(object):
    def __init__(self):
        self.EXTENSION_INFO = self._load_extensions()

    def _load_extensions(self):
        '''Dynamically load all the extensions .'''
        extensions = {}

        # Use the absolute path of the class path
        abs_path = os.path.dirname(os.path.abspath(__file__))

        extdirs = [e for e in os.listdir(abs_path) if
                   not e.startswith('tests') and
                   os.path.isdir(os.path.join(abs_path, e))]

        for e in extdirs:
            log.info(e)
            extpath = abs_path + '/' + e
            # Grab all the extension files in the given path
            ext_files = [f for f in os.listdir(extpath) if f.endswith('.py')
                         and not f.startswith('__init__')]

            # For each module, pick out the target translation class
            for f in ext_files:
                log.info(f)
                ext_name = 'toscaparser/extensions/' + e + '/' + f.strip('.py')
                ext_name = ext_name.replace('/', '.')
                try:
                    extinfo = importlib.import_module(ext_name)
                    version = getattr(extinfo, 'VERSION')
                    defs_file = extpath + '/' + getattr(extinfo, 'DEFS_FILE')

                    # Sections is an optional attribute
                    sections = getattr(extinfo, 'SECTIONS', ())

                    extensions[version] = {'sections': sections,
                                           'defs_file': defs_file}
                except ImportError:
                    raise ToscaExtImportError(ext_name=ext_name)
                except AttributeError:
                    attrs = ', '.join(REQUIRED_ATTRIBUTES)
                    raise ToscaExtAttributeError(ext_name=ext_name,
                                                 attrs=attrs)

        print 'Extensions ',extensions#GGG
        return extensions

    def get_versions(self):
        return self.EXTENSION_INFO.keys()

    def get_sections(self):
        sections = {}
        for version in self.EXTENSION_INFO.keys():
            sections[version] = self.EXTENSION_INFO[version]['sections']

        return sections

    def get_defs_file(self, version):
        versiondata = self.EXTENSION_INFO.get(version)

        if versiondata:
            return versiondata.get('defs_file')
        else:
            return None
*/