Skip to content

Commit

Permalink
feat: support for python
Browse files Browse the repository at this point in the history
  • Loading branch information
hbofolts1 committed Sep 22, 2020
1 parent fd00b4c commit cd199ee
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The currently supported languages are
* ECMAScript6 (javascript2.0)
* Java (java)
* PHP (php)
* Python (python)
* Ruby (ruby)
* TypeScript (typescript)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plantcode",
"version": "0.1.3",
"version": "0.1.4",
"description": "PlantUML to class file generator.",
"author": "Brian Folts <brian.folts@gmail.com>",
"main": "plantcode",
Expand Down
2 changes: 1 addition & 1 deletion plantcode
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var options = {
output: null
};

var supported_languages = ["coffeescript", "csharp", "ecmascript5", "ecmascript6", "java", "php", "ruby", "typescript","swift"];
var supported_languages = ["coffeescript", "csharp", "ecmascript5", "ecmascript6", "java", "php", "python", "ruby", "typescript","swift"];

var args = process.argv.slice(2); // Trim "node" and the script path.

Expand Down
50 changes: 22 additions & 28 deletions src/plantcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@ var hbs = require("handlebars");
var parser = require("./plantuml");
var os = require("os");

var supported_languages = ["coffeescript", "csharp", "ecmascript5", "ecmascript6", "java", "php", "ruby", "typescript"];
var supported_languages = ["coffeescript", "csharp", "ecmascript5", "ecmascript6", "java", "php", "python", "ruby", "typescript", "swift"];

hbs.registerHelper('if_ne', function(a, b, opts) {
if (a() != b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});

// sReturnType Check
hbs.registerHelper('if_ne2', function(a, b, opts) {
if (a["sReturnType"] != b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});

hbs.registerHelper('call', function (context, member, options) {
return member.call(context);
});

function convertFile(config) {

Expand Down Expand Up @@ -60,32 +81,5 @@ function processTemplateFile(config, templateData, dictionary) {

}

// Workaround for an apparent bug in Handlebars: functions are not called with the parent scope
// as context.
//
// Here the getFullName is found in the parent scope (Class), but it is called with the current
// scope (Field) as context:
//
// {{#each getFields}}
// {{../getFullName}}
// {{/each}}
//
// The following helper works around it:
//
// {{#each getFields}}
// {{#call ../this ../getFullName}}
// {{/each}}
hbs.registerHelper('if_ne', function(a, b, opts) {
if (a() != b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});

hbs.registerHelper('call', function (context, member, options) {
return member.call(context);
});

exports.getSupportedLanguages = getSupportedLanguages;
exports.convertFile = convertFile;
11 changes: 11 additions & 0 deletions templates/python.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {{getFullName}}{{#if getExtends}}({{#with getExtends}}{{getFullName}}{{/with}}){{/if}}:
def __init__(self):
{{#each getFields}}
self.{{this.getName}} = None;
{{/each}}
pass;
{{#each getMethods}}
def {{this.getName}}{{#if this.getParameters}}({{#each this.getParameters}}{{#if @first}}{{else}},{{/if}}{{#if this.getName}}{{this.getName}}{{else}}param{{@index}}{{/if}}{{/each}}){{else}}(){{/if}}:
pass;
{{/each}}

71 changes: 71 additions & 0 deletions tests/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Car:
def __init__(self):
self.model = None;
self.make = None;
self.year = None;
pass;
def setModel(model):
pass;
def setMake(make):
pass;
def setYear(param0):
pass;
def getModel():
pass;
def getMake():
pass;
def getYear():
pass;



class Driver:
def __init__(self):
self.name = None;
pass;



class NamesInThings:
def __init__(self):
self.field = None;
self.field1 = None;
self._some_private = None;
self.field_2 = None;
self.member_d = None;
pass;
def member():
pass;
def member2():
pass;
def member3():
pass;
def member_s():
pass;



class Toyota(Car):
def __init__(self):
pass;



class Honda(Car):
def __init__(self):
pass;



class Ford(Car):
def __init__(self):
pass;



class Hyundai(Car):
def __init__(self):
pass;



2 changes: 1 addition & 1 deletion tests/car.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class NamesInThings {
var field1: String1? = nil;
var _some_private: String? = nil;
var field_2: String_2? = nil;
var member_d: String? = nil;
func member(){

}
Expand Down Expand Up @@ -71,4 +72,3 @@ class Hyundai : Car {




8 changes: 8 additions & 0 deletions tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ var inputs = [{
language: "ecmascript6",
input: "tests/car.pegjs",
output: "tests/car.js6"
}, {
language: "swift",
input: "tests/car.pegjs",
output: "tests/car.swift"
}, {
language: "python",
input: "tests/car.pegjs",
output: "tests/car.py"
}];

for(var i = 0; i < inputs.length; i++) {
Expand Down

0 comments on commit cd199ee

Please sign in to comment.