Skip to content

Commit

Permalink
generate imports
Browse files Browse the repository at this point in the history
  • Loading branch information
niklauslee committed Apr 28, 2016
1 parent 43502c9 commit 10d472c
Show file tree
Hide file tree
Showing 3 changed files with 1,077 additions and 13 deletions.
43 changes: 33 additions & 10 deletions PythonCodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ define(function (require, exports, module) {
if (options.docString && text.trim().length > 0) {
lines = text.trim().split("\n");
if (lines.length > 1) {
codeWriter.writeLine('"""');
for (i = 0, len = lines.length; i < len; i++) {
if (i === 0) {
codeWriter.writeLine('"""' + lines[i]);
} else {
codeWriter.writeLine(lines[i]);
}
codeWriter.writeLine(lines[i]);
}
codeWriter.writeLine('"""');
} else {
Expand Down Expand Up @@ -249,13 +246,22 @@ define(function (require, exports, module) {
*/
PythonCodeGenerator.prototype.writeClass = function (codeWriter, elem, options) {
var self = this,
line = "";
line = "",
_inherits = this.getInherits(elem);

// Import
if (_inherits.length > 0) {
_inherits.forEach(function (e) {
var _path = _.map(e.getPath(self.baseModel), function (item) { return item.name; }).join(".");
codeWriter.writeLine("from " + _path + " import " + e.name);
});
codeWriter.writeLine();
}

// Class
line = "class " + elem.name;

// Inherits
var _inherits = this.getInherits(elem);
if (_inherits.length > 0) {
line += "(" + _.map(_inherits, function (e) { return e.name; }).join(", ") + ")";
}
Expand Down Expand Up @@ -372,9 +378,26 @@ define(function (require, exports, module) {
* @param {Object} options
*/
function generate(baseModel, basePath, options) {
var result = new $.Deferred();
var pythonCodeGenerator = new PythonCodeGenerator(baseModel, basePath);
return pythonCodeGenerator.generate(baseModel, basePath, options);
var result = new $.Deferred(),
directory,
fullPath;
var pythonCodeGenerator = new PythonCodeGenerator(baseModel, basePath);
fullPath = basePath + "/" + baseModel.name;
directory = FileSystem.getDirectoryForPath(fullPath);
directory.create(function (err, stat) {
if (!err) {
Async.doSequentially(
baseModel.ownedElements,
function (child) {
return pythonCodeGenerator.generate(child, fullPath, options);
},
false
).then(result.resolve, result.reject);
} else {
result.reject(err);
}
});
return result.promise();
}

exports.generate = generate;
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ Belows are the rules to convert from UML model elements to Python source codes.
* converted to an instance method if `isStatic` property is true, or a class method (`@classmethod`) if `isStatic` property is false
* `name` property to identifier
* `documentation` property to docstring
* _UMLParameter_ to method parameter

### UMLGeneralization, UMLInterfaceRealization

* converted to inheritance

Loading

0 comments on commit 10d472c

Please sign in to comment.