Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ChangeLog

# 2.0.16
- Resolve inner export dara paths with POSIX/Windows separators and reject camelCase Python module names (websocketUtils → websocket_utils).

# 2.0.15
- Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3.

Expand Down
8 changes: 6 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DSL = require('@darabonba/parser');
const {
_name, _vid, _string, _upperFirst, _type, _escape,
_subModelName, _camelCase, _snakeCase, _getImport,
_daraPathSegments, _daraModuleStem, _assertPythonModule,
_isBinaryOp, _adaptedQuotes, CORE, REQUEST, RESPONSE,
_avoidKeywords, _removeFilesInDirectory, _sortImports,
_importsToString,
Expand Down Expand Up @@ -652,10 +653,13 @@ setup(
let exceptions = 'exceptions';
if (inner && pkg.exports[inner]) {
let pyPath = pkg.exports[inner];
filename = _snakeCase(pyPath.split(path.sep).slice(-1)[0].replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, ''), this.config.keywords);
filename = _assertPythonModule(
_snakeCase(_daraModuleStem(pyPath), this.config.keywords),
`export ${inner}`
);
models = filename + '_models';
exceptions = filename + '_exceptions';
const arr = pyPath.split(path.sep).slice(1, -1);
const arr = _daraPathSegments(pyPath).slice(1, -1);
arr.map(key => {
classNamespace += '.' + _snakeCase(key, this.config.keywords);
});
Expand Down
23 changes: 23 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ function _isSnakeCase(str) {
return true;
}

function _daraPathSegments(daraPath) {
if (!daraPath) {
return [];
}
return String(daraPath).split(/[/\\]/);
}

function _daraModuleStem(daraPath) {
const segments = _daraPathSegments(daraPath).filter(Boolean);
const base = segments.length ? segments[segments.length - 1] : '';
return base.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
}

function _assertPythonModule(name, context) {
if (name && !_isSnakeCase(name)) {
throw new Error(`Python module name must be snake_case (${context}): ${name}`);
}
return name;
}

function _isBasicType(type) {
return DSL.util.isBasicType(type);
}
Expand Down Expand Up @@ -689,6 +709,9 @@ module.exports = {
_avoidKeywords,
_camelCase,
_snakeCase,
_daraPathSegments,
_daraModuleStem,
_assertPythonModule,
_deepClone,
_isSnakeCase,
_isBasicType,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@darabonba/python-generator",
"version": "2.0.15",
"version": "2.0.16",
"description": "The darabonba generator for Python",
"main": "lib/generator.js",
"directories": {
Expand Down
66 changes: 66 additions & 0 deletions tests/helper.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const assert = require('assert');
const {
_snakeCase,
_daraPathSegments,
_daraModuleStem,
_assertPythonModule,
_isSnakeCase,
} = require('../lib/helper');

describe('Python module path helpers', function () {
it('_snakeCase maps websocketUtils to websocket_utils', function () {
assert.strictEqual(_snakeCase('websocketUtils'), 'websocket_utils');
assert.strictEqual(_snakeCase('WebSocketUtils'), 'web_socket_utils');
});

it('_daraPathSegments splits POSIX and Windows separators', function () {
assert.deepStrictEqual(_daraPathSegments(), []);
assert.deepStrictEqual(_daraPathSegments(''), []);
assert.deepStrictEqual(
_daraPathSegments('./websocketUtils.dara'),
['.', 'websocketUtils.dara']
);
assert.deepStrictEqual(
_daraPathSegments('.\\websocketUtils.dara'),
['.', 'websocketUtils.dara']
);
assert.deepStrictEqual(
_daraPathSegments('foo/bar/websocketUtils.dara'),
['foo', 'bar', 'websocketUtils.dara']
);
});

it('_daraModuleStem strips dara/tea/spec suffix from any separator', function () {
assert.strictEqual(_daraModuleStem(), '');
assert.strictEqual(_daraModuleStem('/'), '');
assert.strictEqual(_daraModuleStem('./websocketUtils.dara'), 'websocketUtils');
assert.strictEqual(_daraModuleStem('.\\websocketUtils.dara'), 'websocketUtils');
assert.strictEqual(_daraModuleStem('websocketUtils.tea'), 'websocketUtils');
assert.strictEqual(_daraModuleStem('websocketUtils.spec'), 'websocketUtils');
assert.strictEqual(_daraModuleStem('dir/websocketUtils.DARA'), 'websocketUtils');
assert.strictEqual(_daraModuleStem('plain'), 'plain');
});

it('exported dara filename becomes a snake_case Python module', function () {
const filename = _snakeCase(_daraModuleStem('./websocketUtils.dara'));
assert.strictEqual(filename, 'websocket_utils');
assert.strictEqual(`${filename}_models`, 'websocket_utils_models');
assert.ok(_isSnakeCase(filename));
});

it('_assertPythonModule accepts snake_case and rejects camelCase', function () {
assert.strictEqual(_assertPythonModule('', 'empty'), '');
assert.strictEqual(
_assertPythonModule('websocket_utils', 'export WebSocketUtils'),
'websocket_utils'
);
assert.throws(() => {
_assertPythonModule('websocketUtils', 'export WebSocketUtils');
}, /snake_case/);
assert.throws(() => {
_assertPythonModule('1abc', 'export');
}, /snake_case/);
});
});
8 changes: 8 additions & 0 deletions tests/main.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,13 @@ describe('Python Generator', function () {
clientName: 'client'
}
});
const clientSrc = fs.readFileSync(
path.join(outputDir, 'websocket', 'tea_python_tests', 'client.py'),
'utf8'
);
assert.ok(clientSrc.includes('alibabacloud_tea_openapi.websocket_utils'));
assert.ok(clientSrc.includes('websocket_utils_models'));
assert.ok(!clientSrc.includes('websocketUtils'));
assert.ok(!clientSrc.includes('websocketUtils_models'));
});
});
Loading