Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
@0x/abi-gen: Update python wrappers generation to work with leading…
Browse files Browse the repository at this point in the history
… underscores in identifiers.'
  • Loading branch information
merklejerk committed Jun 2, 2020
1 parent 6ef4a54 commit fc86835
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 827 deletions.
9 changes: 9 additions & 0 deletions packages/abi-gen/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "5.3.0",
"changes": [
{
"note": "Allow identifiers with leading underscores in python wrappers",
"pr": 2591
}
]
},
{
"version": "5.2.2",
"changes": [
Expand Down
11 changes: 10 additions & 1 deletion packages/abi-gen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ if (args.language === 'TypeScript') {
registerPartials();

function makeLanguageSpecificName(methodName: string): string {
return args.language === 'Python' ? changeCase.snake(methodName) : methodName;
if (args.language === 'Python') {
let snakeCased = changeCase.snake(methodName);
// Move leading underscores to the end.
const m = /^(_*).+?(_*)$/.exec(methodName);
if (m) {
snakeCased = `${snakeCased}${m[1] || m[2]}`;
}
return snakeCased;
}
return methodName;
}

if (_.isEmpty(abiFileNames)) {
Expand Down
13 changes: 9 additions & 4 deletions packages/abi-gen/src/python_handlebars_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ export function registerPythonHelpers(): void {
}
return '';
});
Handlebars.registerHelper(
'toPythonClassname',
(sourceName: string) => new Handlebars.SafeString(changeCase.pascal(sourceName)),
);
Handlebars.registerHelper('toPythonClassname', (sourceName: string) => {
let pascalCased = changeCase.pascal(sourceName);
// Retain trailing underscores.
const m = /^.+?(_*)$/.exec(sourceName);
if (m) {
pascalCased = `${pascalCased}${m[1]}`;
}
return new Handlebars.SafeString(pascalCased);
});
Handlebars.registerHelper(
'makeOutputsValue',
/**
Expand Down
11 changes: 6 additions & 5 deletions packages/abi-gen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ export const utils = {
'max',
'round',
];
if (
pythonReservedWords.includes(snakeCased) ||
pythonBuiltins.includes(snakeCased) ||
/*changeCase strips leading underscores :(*/ input[0] === '_'
) {
if (pythonReservedWords.includes(snakeCased) || pythonBuiltins.includes(snakeCased)) {
snakeCased = `_${snakeCased}`;
}
// Retain trailing underscores.
const m = /^.+?(_*)$/.exec(input);
if (m) {
snakeCased = `${snakeCased}${m[1]}`;
}
return snakeCased;
},
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

class {{toPythonClassname this.languageSpecificName}}Method(ContractMethod):
class {{toPythonClassname this.languageSpecificName}}Method(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the {{this.name}} method."""

def __init__(self, web3_or_provider: Union[Web3, BaseProvider], contract_address: str, contract_function: ContractFunction{{#if inputs}}, validator: Validator=None{{/if}}):
Expand Down
Loading

0 comments on commit fc86835

Please sign in to comment.