Added templates and config for Python client generation.

This commit is contained in:
Russell Horton
2011-11-11 01:06:12 -08:00
parent 9b1d1a9dad
commit 087b22bcf3
9 changed files with 702 additions and 0 deletions

View File

@@ -0,0 +1,228 @@
/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wordnik.swagger.codegen.config.python;
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* User: ramesh
* Date: 5/31/11
* Time: 7:03 AM
*/
public class PythonDataTypeMappingProvider implements DataTypeMappingProvider {
public static Map<String, String> primitiveValueMap = new HashMap<String, String>();
static{
primitiveValueMap.put("string", "str");
primitiveValueMap.put("String", "str");
primitiveValueMap.put("int", "int");
primitiveValueMap.put("integer", "int");
primitiveValueMap.put("Integer", "int");
primitiveValueMap.put("boolean", "bool");
primitiveValueMap.put("Boolean", "bool");
primitiveValueMap.put("long", "int");
primitiveValueMap.put("Long", "int");
primitiveValueMap.put("float", "float");
primitiveValueMap.put("Float", "float");
primitiveValueMap.put("Date", "str");
primitiveValueMap.put("date", "str");
primitiveValueMap.put("Double", "float");
primitiveValueMap.put("double", "float");
}
public static Map<String, String> primitiveObjectMap = new HashMap<String, String>();
static{
primitiveObjectMap.put("string", "str");
primitiveObjectMap.put("String", "str");
primitiveObjectMap.put("java.lang.String", "str");
primitiveObjectMap.put("int", "int");
primitiveObjectMap.put("integer", "int");
primitiveObjectMap.put("Integer", "int");
primitiveObjectMap.put("java.lang.Integer", "int");
primitiveObjectMap.put("bool", "bool");
primitiveObjectMap.put("boolean", "bool");
primitiveObjectMap.put("Boolean", "bool");
primitiveObjectMap.put("java.lang.Boolean", "bool");
primitiveObjectMap.put("long", "int");
primitiveObjectMap.put("Long", "int");
primitiveObjectMap.put("java.lang.Long", "int");
primitiveObjectMap.put("float", "float");
primitiveObjectMap.put("Float", "float");
primitiveObjectMap.put("double", "float");
primitiveObjectMap.put("Double", "float");
primitiveObjectMap.put("java.lang.Float", "float");
primitiveObjectMap.put("Date", "str");
primitiveObjectMap.put("date", "str");
primitiveObjectMap.put("java.util.Date", "str");
}
private NamingPolicyProvider nameGenerator = new CamelCaseNamingPolicyProvider();
public boolean isPrimitiveType(String type) {
if(primitiveObjectMap.containsKey(type)){
return true;
}
return false;
}
public String getListReturnTypeSignature(String typeClass) {
if (isPrimitiveType(typeClass)) {
return "list<"+typeClass+">";
} else {
return "list<"+nameGenerator.applyClassNamingPolicy(typeClass)+">";
}
}
public String getMapReturnTypeSignature(String typeClass) {
return "dict<"+nameGenerator.applyClassNamingPolicy(typeClass)+">";
}
public String getSetReturnTypeSignature(String typeClass) {
return "set<"+nameGenerator.applyClassNamingPolicy(typeClass)+">";
}
public String generateListInitialization(String typeClass) {
return " list()";
}
public String generateMapInitialization(String typeClass) {
return " dict()";
}
public String generateSetInitialization(String typeClass) {
return " set()";
}
public List<String> getListIncludes() {
List<String> imports = new ArrayList<String>();
return imports;
}
public List<String> getMapIncludes() {
List<String> imports = new ArrayList<String>();
return imports;
}
public List<String> getSetIncludes() {
List<String> imports = new ArrayList<String>();
return imports; }
public List<String> getDateIncludes() {
List<String> imports = new ArrayList<String>();
return imports;
}
/**
* Gets the short name of the class the class.
* Input can be MAP, LIST or regular string. In case of map or list the class name will be class name
* that map or list is returning.
* @param type
* @return
*/
public String getGenericType(String type) {
String classShortName = "";
if(type.startsWith("List[")){
classShortName = type.substring(5, type.length()-1);
classShortName = getClassType(classShortName, true);
}else if (type.startsWith("Map[")) {
classShortName = type.substring(4, type.length()-1);
classShortName = getClassType(classShortName, true);
}else if (type.startsWith("Set[")) {
classShortName = type.substring(4, type.length()-1);
classShortName = getClassType(classShortName, true);
}else if (type.equalsIgnoreCase("ok")) {
classShortName = "void";
}else{
classShortName = getClassType(type, true);
}
return classShortName;
}
/**
* Returns the syntax for defintion of an object of type and name
*
* @param argumentType
* @param argumentName
* @return
*/
public String getArgumentDefinition(String argumentType, String argumentName) {
return argumentType + " " + argumentName;
}
/**
* Gets the class of the expected return value for a type string. Examples of type Strings are int, User, List[User]
* If the type string is a collection type like a map or list the string value returned would be the class
* that map or list is returning.
*
* @param type
* @return
*/
public String getClassType(String type, boolean primitiveObject) {
if(type.equalsIgnoreCase("void")|| type.equalsIgnoreCase("ok")){
return "void";
}
String classShortName = "";
if(type.startsWith("List[")){
classShortName = type.substring(5, type.length()-1);
classShortName = "list<"+ getClassName(classShortName, true)+">";
}else if (type.startsWith("Map[")) {
classShortName = type.substring(4, type.length()-1);
classShortName = "dict<"+ getClassName(classShortName, true) +">";
}else if (type.startsWith("Set[")) {
classShortName = type.substring(4, type.length()-1);
classShortName = "set<"+ getClassName(classShortName, true) +">";
}else{
classShortName = getClassName(type, true);
}
return classShortName;
}
/**
* If the data type is primitive and it is expecting object structure then return primitive objects
* else return primitive types
* @param type
* @param primitiveObject -- indicates if the object is primitive or not
* @return
*/
private String getClassName(String type, boolean primitiveObject) {
if(isPrimitiveType(type)){
if(primitiveObject){
return primitiveObjectMap.get(type);
}else{
return primitiveValueMap.get(type);
}
}else{
return nameGenerator.applyClassNamingPolicy(type);
}
}
@Override
public String generateVariableInitialization(String typeClass) {
return "";
}
}

View File

@@ -0,0 +1,103 @@
/**
* Copyright 2011 Wordnik, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wordnik.swagger.codegen.config.python;
import com.wordnik.swagger.codegen.LibraryCodeGenerator;
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
import com.wordnik.swagger.codegen.exception.CodeGenerationException;
import com.wordnik.swagger.codegen.util.FileUtil;
import java.io.File;
/**
* User: russ
* Date: 9/1/11
* Time: 11:00 PM
*/
public class PythonLibCodeGen extends LibraryCodeGenerator {
public static void main(String[] args) {
if(args.length < 1){
throw new CodeGenerationException("Invalid number of arguments passed: No command line argument was passed to the program for config json");
}
if(args.length == 1) {
String configPath = args[0];
PythonLibCodeGen codeGenerator = new PythonLibCodeGen(configPath);
codeGenerator.generateCode();
}
if(args.length == 4) {
String apiServerURL = args[0];
if(!apiServerURL.endsWith("/")){
apiServerURL = apiServerURL + "/";
}
String apiKey = args[1];
String packageName = args[2];
String libraryHome = args[3];
if(libraryHome.endsWith("/")){
libraryHome = libraryHome.substring(0, libraryHome.length()-1);
}
String modelPackageName = packageName+".model";
String apiPackageName = packageName+".api";
String classOutputDir = libraryHome + packageName.replace(".","/");
PythonLibCodeGen codeGenerator = new PythonLibCodeGen(apiServerURL, apiKey, modelPackageName,
apiPackageName, classOutputDir, libraryHome);
codeGenerator.generateCode();
}
}
public PythonLibCodeGen(String apiServerURL, String apiKey, String modelPackageName, String apiPackageName,
String classOutputDir, String libraryHome){
super(apiServerURL, apiKey, modelPackageName, apiPackageName, classOutputDir, libraryHome);
this.setDataTypeMappingProvider(new PythonDataTypeMappingProvider());
this.setNameGenerator(new CamelCaseNamingPolicyProvider());
}
public PythonLibCodeGen(String configPath){
super(configPath);
this.setDataTypeMappingProvider(new PythonDataTypeMappingProvider());
this.setNameGenerator(new CamelCaseNamingPolicyProvider());
}
@Override
protected LanguageConfiguration initializeLangConfig(LanguageConfiguration PythonConfiguration) {
PythonConfiguration.setClassFileExtension(".py");
PythonConfiguration.setTemplateLocation("conf/python/templates");
PythonConfiguration.setStructureLocation("conf/python/structure");
PythonConfiguration.setExceptionPackageName("com.wordnik.swagger.exception");
PythonConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations");
//create ouput directories
FileUtil.createOutputDirectories(PythonConfiguration.getModelClassLocation(), PythonConfiguration.getClassFileExtension());
FileUtil.createOutputDirectories(PythonConfiguration.getResourceClassLocation(), PythonConfiguration.getClassFileExtension());
FileUtil.clearFolder(PythonConfiguration.getModelClassLocation());
FileUtil.clearFolder(PythonConfiguration.getResourceClassLocation());
FileUtil.copyDirectory(new File(PythonConfiguration.getStructureLocation()), new File(PythonConfiguration.getResourceClassLocation()));
File initFile = new File(PythonConfiguration.getResourceClassLocation() + "__init__.py");
File newInitFile = new File(PythonConfiguration.getModelClassLocation() + "__init__.py");
initFile.renameTo(newInitFile);
// try {
// initFile.createNewFile();
// } catch (java.io.IOException e) {
// e.printStackTrace();
// throw new CodeGenerationException("Creating model/__init__.py failed");
// }
return PythonConfiguration;
}
}