IPC removed, replaced by WS
This commit is contained in:
1 parent
ba4217fb24
commit
656c193511
503 files changed
+28128
-34498
No files matched your search
+155
-38
@@ -5,16 +5,23 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require ('fs')
|
||||
, log = require ('ololog')
|
||||
, log = require ('ololog').unlimited
|
||||
, _ = require ('ansicolor').nice
|
||||
, errors = require ('../js/base/errors.js')
|
||||
, { unCamelCase, precisionConstants, safeString } = require ('../js/base/functions.js')
|
||||
, functions = require ('../js/base/functions.js')
|
||||
, {
|
||||
unCamelCase,
|
||||
precisionConstants,
|
||||
safeString,
|
||||
unique,
|
||||
} = functions
|
||||
, { basename } = require ('path')
|
||||
, {
|
||||
createFolderRecursively,
|
||||
replaceInFile,
|
||||
overwriteFile,
|
||||
} = require ('./fs.js')
|
||||
, Exchange = require ('../js/base/Exchange.js')
|
||||
|
||||
class Transpiler {
|
||||
|
||||
@@ -60,6 +67,7 @@ class Transpiler {
|
||||
[ /\.parseTrades\s/g, '.parse_trades'],
|
||||
[ /\.parseTrade\s/g, '.parse_trade'],
|
||||
[ /\.parseTradingViewOHLCV\s/g, '.parse_trading_view_ohlcv'],
|
||||
[ /\.convertTradingViewToOHLCV\s/g, '.convert_trading_view_to_ohlcv'],
|
||||
[ /\.parseTransaction\s/g, '.parse_transaction'],
|
||||
[ /\.parseTransactions\s/g, '.parse_transactions'],
|
||||
[ /\.parseOrderBook\s/g, '.parse_order_book'],
|
||||
@@ -299,7 +307,9 @@ class Transpiler {
|
||||
[ /this\.stringToBase64\s/g, 'base64_encode' ],
|
||||
[ /this\.binaryToBase16\s/g, 'bin2hex' ],
|
||||
[ /this\.base64ToBinary\s/g, 'base64_decode' ],
|
||||
[ /this\.deepExtend\s/g, 'array_replace_recursive'],
|
||||
// deepExtend is commented for PHP because it does not overwrite linear arrays
|
||||
// a proper \ccxt\Exchange::deep_extend() base method is implemented instead
|
||||
// [ /this\.deepExtend\s/g, 'array_replace_recursive'],
|
||||
[ /(\w+)\.shift\s*\(\)/g, 'array_shift($1)' ],
|
||||
[ /(\w+)\.pop\s*\(\)/g, 'array_pop($1)' ],
|
||||
[ /Number\.MAX_SAFE_INTEGER/g, 'PHP_INT_MAX' ],
|
||||
@@ -382,12 +392,27 @@ class Transpiler {
|
||||
])
|
||||
}
|
||||
|
||||
getBaseClass () {
|
||||
return new Exchange ()
|
||||
}
|
||||
|
||||
getBaseMethods () {
|
||||
const baseExchange = this.getBaseClass ()
|
||||
let object = baseExchange
|
||||
let properties = []
|
||||
while (object !== Object.prototype) {
|
||||
properties = properties.concat (Object.getOwnPropertyNames (object))
|
||||
object = Object.getPrototypeOf (object)
|
||||
}
|
||||
return properties.filter (x => typeof baseExchange[x] === 'function')
|
||||
}
|
||||
|
||||
getPythonBaseMethods () {
|
||||
return []
|
||||
return this.getBaseMethods ()
|
||||
}
|
||||
|
||||
getPHPBaseMethods () {
|
||||
return []
|
||||
return this.getBaseMethods ()
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@@ -589,10 +614,10 @@ class Transpiler {
|
||||
methods = methods.concat (this.getPHPBaseMethods ())
|
||||
|
||||
for (let method of methods) {
|
||||
const regex = new RegExp ('\\$this->(' + method + ')(\\s\\(|[^a-zA-Z0-9_])', 'g')
|
||||
const regex = new RegExp ('\\$this->(' + method + ')\\s?(\\(|[^a-zA-Z0-9_])', 'g')
|
||||
bodyAsString = bodyAsString.replace (regex,
|
||||
(match, p1, p2) => {
|
||||
return ((p2 === ' (') ?
|
||||
return ((p2 === '(') ?
|
||||
('$this->' + unCamelCase (p1) + p2) : // support direct php calls
|
||||
("array($this, '" + unCamelCase (p1) + "')" + p2)) // as well as passing instance methods as callables
|
||||
})
|
||||
@@ -748,7 +773,7 @@ class Transpiler {
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
transpileDerivedExchangeClass (contents) {
|
||||
transpileDerivedExchangeClass (contents, methodNames = undefined) {
|
||||
|
||||
let exchangeClassDeclarationMatches = contents.match (/^module\.exports\s*=\s*class\s+([\S]+)\s+extends\s+([\S]+)\s+{([\s\S]+?)^};*/m)
|
||||
|
||||
@@ -763,7 +788,7 @@ class Transpiler {
|
||||
let python3 = []
|
||||
let php = []
|
||||
|
||||
let methodNames = []
|
||||
methodNames = [] // methodNames || []
|
||||
|
||||
// run through all methods
|
||||
for (let i = 0; i < methods.length; i++) {
|
||||
@@ -839,7 +864,7 @@ class Transpiler {
|
||||
|
||||
// compile signature + body for PHP
|
||||
php.push ('');
|
||||
php.push (' public function ' + method + ' (' + phpArgs + ') {');
|
||||
php.push (' public function ' + method + '(' + phpArgs + ') {');
|
||||
php.push (phpBody);
|
||||
php.push (' }')
|
||||
|
||||
@@ -866,8 +891,25 @@ class Transpiler {
|
||||
try {
|
||||
|
||||
const { python2Folder, python3Folder, phpFolder } = options
|
||||
const contents = fs.readFileSync (jsFolder + filename, 'utf8')
|
||||
const { python2, python3, php, className, baseClass } = this.transpileDerivedExchangeClass (contents)
|
||||
const path = jsFolder + filename
|
||||
const contents = fs.readFileSync (path, 'utf8')
|
||||
|
||||
// function getMethodNames (object) {
|
||||
// let functions = []
|
||||
// let o = object
|
||||
// do {
|
||||
// functions = functions.concat (Object.getOwnPropertyNames (o))
|
||||
// } while (o = Object.getPrototypeOf (o))
|
||||
// return unique (functions.filter (f => (typeof object[f] === 'function')))
|
||||
// }
|
||||
// const exchangeClass = require ('.' + path)
|
||||
// const exchange = new exchangeClass ()
|
||||
// const methodNames = getMethodNames (exchange)
|
||||
// const { python2, python3, php, className, baseClass } =
|
||||
// this.transpileDerivedExchangeClass (contents, methodNames)
|
||||
|
||||
const { python2, python3, php, className, baseClass } =
|
||||
this.transpileDerivedExchangeClass (contents)
|
||||
|
||||
log.cyan ('Transpiling from', filename.yellow)
|
||||
|
||||
@@ -943,11 +985,12 @@ class Transpiler {
|
||||
|
||||
// ========================================================================
|
||||
|
||||
exportTypeScriptDeclarations (classes) {
|
||||
exportTypeScriptDeclarations (file, classes) {
|
||||
|
||||
const file = './ccxt.d.ts'
|
||||
const regex = /(?: export class [^\s]+ extends [^\s]+ \{\}[\r]?[\n])+/
|
||||
const replacement = Object.keys (classes).map (className => {
|
||||
log.bright.cyan ('Exporting TypeScript declarations →', file.yellow)
|
||||
|
||||
const regex = /\/[\n]{2}(?: export class [^\s]+ extends [^\s]+ \{\}[\r]?[\n])+/
|
||||
const replacement = "/\n\n" + Object.keys (classes).map (className => {
|
||||
const baseClass = classes[className]
|
||||
return ' export class ' + className + ' extends ' + baseClass + " {}"
|
||||
}).join ("\n") + "\n"
|
||||
@@ -968,34 +1011,91 @@ class Transpiler {
|
||||
[ /module\.exports = [^\;]+\;\n/s, '' ],
|
||||
]).trim ()
|
||||
|
||||
const message = 'Transpiling error hierachy →'
|
||||
const root = errorHierarchy['BaseError']
|
||||
|
||||
const { python3Body, phpBody } = this.transpileJavaScriptToPythonAndPHP ({ js })
|
||||
|
||||
const python3BodyIntellisense = python3Body + '\n\n' + Array.from (function* intellisense (map) {
|
||||
for (const key in map) {
|
||||
yield key + ' = Exception'
|
||||
yield* intellisense (map[key])
|
||||
// a helper to generate a list of exception class declarations
|
||||
// properly derived from corresponding parent classes according
|
||||
// to the error hierarchy
|
||||
|
||||
function intellisense (map, parent, generate, classes) {
|
||||
function* generator(map, parent, generate, classes) {
|
||||
for (const key in map) {
|
||||
yield generate (key, parent, classes)
|
||||
yield* generator (map[key], key, generate, classes)
|
||||
}
|
||||
}
|
||||
} (errorHierarchy['BaseError'])).join ('\n')
|
||||
|
||||
const message = 'Transpiling error hierachy →'
|
||||
|
||||
const python = {
|
||||
filename: './python/ccxt/base/errors.py',
|
||||
regex: /error_hierarchy = [\S\s]+= Exception/s,
|
||||
replacement: python3BodyIntellisense,
|
||||
return Array.from (generator (map, parent, generate, classes))
|
||||
}
|
||||
|
||||
log.bright.cyan (message, python.filename.yellow)
|
||||
replaceInFile (... Object.values (python))
|
||||
// Python -------------------------------------------------------------
|
||||
|
||||
const php = {
|
||||
filename:'./php/base/errors.php',
|
||||
regex: /\$error_hierarchy = .+?\n\)\;/s,
|
||||
replacement: phpBody,
|
||||
function pythonDeclareErrorClass (name, parent, classes) {
|
||||
classes.push (name)
|
||||
return [
|
||||
'class ' + name + '(' + parent + '):',
|
||||
' pass',
|
||||
'',
|
||||
'',
|
||||
].join ('\n');
|
||||
}
|
||||
|
||||
log.bright.cyan (message, php.filename.yellow)
|
||||
replaceInFile (... Object.values (php))
|
||||
const pythonBaseError = [
|
||||
'class BaseError(Exception):',
|
||||
' pass',
|
||||
'',
|
||||
'',
|
||||
].join ('\n');
|
||||
|
||||
const quote = (s) => "'" + s + "'" // helper to add quotes around class names
|
||||
|
||||
const pythonExports = [ 'error_hierarchy' ]
|
||||
const pythonErrors = intellisense (root, 'BaseError', pythonDeclareErrorClass, pythonExports)
|
||||
const pythonAll = '__all__ = [\n ' + pythonExports.map (quote).join (',\n ') + '\n]'
|
||||
|
||||
const python3BodyIntellisense = python3Body + '\n\n\n' + pythonBaseError + '\n' + pythonErrors.join ('\n') + '\n' + pythonAll + '\n'
|
||||
|
||||
const pythonFilename = './python/ccxt/base/errors.py'
|
||||
log.bright.cyan (message, pythonFilename.yellow)
|
||||
fs.writeFileSync (pythonFilename, python3BodyIntellisense)
|
||||
|
||||
// PHP ----------------------------------------------------------------
|
||||
|
||||
function phpDeclareErrorClass (name, parent) {
|
||||
return 'class ' + name + ' extends ' + parent + ' {};'
|
||||
}
|
||||
|
||||
const phpHeader = '<?php\n\nnamespace ccxt;\n\nuse Exception;\n\n'
|
||||
const phpBaseError = 'class BaseError extends Exception {};'
|
||||
const phpErrors = intellisense (root, 'BaseError', phpDeclareErrorClass)
|
||||
const phpBodyIntellisense = phpHeader + phpBody + '\n\n' + phpBaseError + '\n' + phpErrors.join ('\n')
|
||||
|
||||
const phpFilename = './php/base/errors.php'
|
||||
log.bright.cyan (message, phpFilename.yellow)
|
||||
fs.writeFileSync (phpFilename, phpBodyIntellisense)
|
||||
|
||||
// TypeScript ---------------------------------------------------------
|
||||
|
||||
function tsDeclareErrorClass (name, parent) {
|
||||
return 'export class ' + name + ' extends ' + parent + ' {}'
|
||||
}
|
||||
|
||||
const tsBaseError = [
|
||||
'export class BaseError extends Error {',
|
||||
' constructor(message: string);',
|
||||
'}',
|
||||
].join ('\n ')
|
||||
|
||||
const tsErrors = intellisense (root, 'BaseError', tsDeclareErrorClass)
|
||||
|
||||
const tsBodyIntellisense = tsBaseError + '\n\n ' + tsErrors.join ('\n ') + '\n\n'
|
||||
|
||||
const tsFilename = './ccxt.d.ts'
|
||||
log.bright.cyan (message, tsFilename.yellow)
|
||||
const regex = /export class BaseError[^}]+\}[\n][\n](?:\s+export class [a-zA-Z]+ extends [a-zA-Z]+ \{\}[\n])+[\n]/m
|
||||
replaceInFile (tsFilename, regex, tsBodyIntellisense)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -1070,8 +1170,15 @@ class Transpiler {
|
||||
"from ccxt.base.decimal_to_precision import PAD_WITH_ZERO # noqa F401",
|
||||
"from ccxt.base.decimal_to_precision import NO_PADDING # noqa F401",
|
||||
"from ccxt.base.decimal_to_precision import number_to_string # noqa F401",
|
||||
"from ccxt.base.exchange import Exchange # noqa F401",
|
||||
"",
|
||||
"# ----------------------------------------------------------------------------",
|
||||
"",
|
||||
"def toWei(amount, decimals):",
|
||||
" return Exchange.to_wei(amount, decimals)",
|
||||
"",
|
||||
"",
|
||||
"def fromWei(amount, decimals):",
|
||||
" return Exchange.from_wei(amount, decimals)",
|
||||
"",
|
||||
"",
|
||||
].join ("\n")
|
||||
@@ -1095,6 +1202,12 @@ class Transpiler {
|
||||
"function number_to_string ($x) {",
|
||||
" return Exchange::number_to_string ($x);",
|
||||
"}",
|
||||
"function toWei ($amount, $decimals) {",
|
||||
" return Exchange::to_wei ($amount, $decimals);",
|
||||
"}",
|
||||
"function fromWei ($amount, $decimals) {",
|
||||
" return Exchange::from_wei ($amount, $decimals);",
|
||||
"}",
|
||||
"",
|
||||
].join ("\n")
|
||||
|
||||
@@ -1190,6 +1303,8 @@ class Transpiler {
|
||||
createFolderRecursively (python3Folder)
|
||||
createFolderRecursively (phpFolder)
|
||||
|
||||
//*
|
||||
|
||||
const classes = this.transpileDerivedExchangeFiles ('./js/', options, pattern)
|
||||
|
||||
if (classes === null) {
|
||||
@@ -1199,7 +1314,9 @@ class Transpiler {
|
||||
|
||||
// HINT: if we're going to support specific class definitions
|
||||
// this process won't work anymore as it will override the definitions
|
||||
this.exportTypeScriptDeclarations (classes)
|
||||
this.exportTypeScriptDeclarations ('./ccxt.d.ts', classes)
|
||||
|
||||
//*/
|
||||
|
||||
this.transpileErrorHierarchy ()
|
||||
|
||||
|
||||
Reference in new issue
Block a user