-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspileWS.ts
341 lines (292 loc) · 12.7 KB
/
transpileWS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// ---------------------------------------------------------------------------
// Usage: npm run transpileWs
// ---------------------------------------------------------------------------
import fs from 'fs';
import log from 'ololog';
import ccxt from '../js/ccxt.js';
import ansi from 'ansicolor'
import {
replaceInFile,
copyFile,
overwriteFile,
createFolder,
createFolderRecursively,
} from './fsLocal.js';
import Exchange from '../js/src/base/Exchange.js';
import { Transpiler, parallelizeTranspiling, isMainEntry } from './transpile.js';
const exchanges = JSON.parse (fs.readFileSync("./exchanges.json", "utf8"));
const wsExchangeIds = exchanges.ws;
const { unCamelCase, precisionConstants, safeString, unique } = ccxt;
ansi.nice
// ============================================================================
class CCXTProTranspiler extends Transpiler {
getBaseClass () {
return new Exchange ()
}
createPythonClassDeclaration (className, baseClass) {
const baseClasses = (baseClass.indexOf ('Rest') >= 0) ?
[ 'ccxt.async_support.' + baseClass.replace('Rest', '') ] :
[ baseClass ]
return 'class ' + className + '(' + baseClasses.join (', ') + '):'
}
createPythonClassImports (baseClass, className, async = false) {
const baseClasses = {
'Exchange': 'base.exchange',
}
async = (async ? '.async_support' : '')
if (baseClass.indexOf ('Rest') >= 0) {
return [
// 'from ccxt.async_support' + ' import ' + baseClass,
"import ccxt.async_support"
]
} else {
return [
'from ccxt.pro.' + baseClass + ' import ' + baseClass // on the JS side we add to append `Rest` to the base class name
]
}
// return [
// (baseClass.indexOf ('ccxt.') === 0) ?
// ('import ccxt' + async + ' as ccxt') :
// ('from ccxtpro.' + safeString (baseClasses, baseClass, baseClass) + ' import ' + baseClass)
// ]
}
createPythonClassHeader (ccxtImports, bodyAsString) {
const imports = [
... ccxtImports,
]
const arrayCacheClasses = bodyAsString.match (/\bArrayCache(?:[A-Z][A-Za-z]+)?\b/g)
if (arrayCacheClasses) {
const uniqueArrayCacheClasses = unique (arrayCacheClasses).sort ()
const arrayCacheImport = 'from ccxt.async_support.base.ws.cache import ' + uniqueArrayCacheClasses.join (', ')
imports.push (arrayCacheImport)
}
const orderBookClasses = bodyAsString.match(/\s(Asks|Bids)\(.*\)/g)
if (orderBookClasses) {
const uniqueOrderBookClasses = unique (orderBookClasses.map(match => match.replace(/\(.*\)/, '').trim())).sort ()
const orderBookSideImport = 'from ccxt.async_support.base.ws.order_book_side import ' + uniqueOrderBookClasses.join (', ')
imports.push (orderBookSideImport)
}
return [
"# -*- coding: utf-8 -*-",
"",
"# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:",
"# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code",
"",
... imports,
]
}
createPHPClassDeclaration (className, baseClass) {
let lines = []
if (baseClass.indexOf ('Rest') >= 0) {
// lines = lines.concat ([
// '',
// // ' use ClientTrait;'
// ])
lines.push('class ' + className + ' extends ' + '\\ccxt\\async\\' + baseClass.replace ('Rest', '') + ' {')
} else {
lines.push('class ' + className + ' extends ' + '\\ccxt\\pro\\' + baseClass + ' {')
}
return lines.join ("\n")
}
createPHPClassHeader (className, baseClass, bodyAsString) {
return [
"<?php",
"",
"namespace ccxt\\pro;",
"",
"// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:",
"// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code",
"",
"use Exception; // a common import",
]
}
sortExchangeCapabilities (code) {
return false
}
exportTypeScriptClassNames (file, classes) {
log.bright.cyan ('Exporting WS TypeScript class names →', file.yellow)
const commonImports = [
' export const exchanges: string[]',
' class Exchange extends ExchangePro {}'
]
const replacements = [
{
file:file,
regex: /\n\n\s+export\snamespace\spro\s{\n\s+[\s\S]+}/,
replacement: "\n\n export namespace pro {\n" + commonImports.join('\n') + '\n' + Object.keys (classes).map (className => {
return ' class ' + className + ' extends Exchange {}'
}).join ("\n") + "\n }\n}"
}
]
replacements.forEach (({ file, regex, replacement }) => {
replaceInFile (file, regex, replacement)
})
}
// -----------------------------------------------------------------------
wsTestsDirectories = {
ts: './ts/src/pro/test/',
py: './python/ccxt/pro/test/',
php: './php/pro/test/',
};
transpileWsTests (){
this.transpileWsCacheTest();
this.transpileWsOrderBookTest();
this.transpileWsExchangeTests();
}
transpileWsExchangeTests () {
const wsCollectedTests = [];
for (const currentFolder of ['Exchange/']) {
const fileNames = this.readTsFileNames(this.wsTestsDirectories.ts + currentFolder);
for (const testName of fileNames) {
const testNameUncameled = this.uncamelcaseName(testName);
const test = {
base: false,
name: testName,
tsFile: this.wsTestsDirectories.ts + currentFolder + testName + '.ts',
pyFileAsync: this.wsTestsDirectories.py + currentFolder + testNameUncameled + '.py',
phpFileAsync: this.wsTestsDirectories.php + currentFolder + testNameUncameled + '.php',
};
wsCollectedTests.push(test);
}
}
this.transpileAndSaveExchangeTests (wsCollectedTests);
}
arrayEqualFunctionForPhp = (
`function equals($a, $b) {` +
`\n return json_encode($a) === json_encode($b);` +
`\n}`+
'\n'
);
arrayEqualFunctionForPy = (
`def equals(a, b):`+
`\n return a == b`+
'\n'
);
transpileWsOrderBookTest() {
const currentFolder = 'base/';
const testName = 'test.orderBook';
const testNameUncameled = this.uncamelcaseName(testName);
const test = {
base: true,
name: testName,
tsFile: this.wsTestsDirectories.ts + currentFolder + testName + '.ts',
pyFileSync: this.wsTestsDirectories.py + currentFolder + testNameUncameled + '.py',
pyHeaders: ['\n', 'from ccxt.async_support.base.ws.order_book import OrderBook, IndexedOrderBook, CountedOrderBook # noqa: F402', '\n', '\n'],
phpHeaders: [],
phpFileSync: this.wsTestsDirectories.php + currentFolder + testNameUncameled + '.php',
};
this.transpileAndSaveExchangeTests ([test]);
}
transpileWsCacheTest() {
const currentFolder = 'base/';
const testName = 'test.cache';
const testNameUncameled = this.uncamelcaseName(testName);
const test = {
base: true,
name: testName,
tsFile: this.wsTestsDirectories.ts + currentFolder + testName + '.ts',
pyFileSync: this.wsTestsDirectories.py + currentFolder + testNameUncameled + '.py',
pyHeaders: ['from ccxt.async_support.base.ws.cache import ArrayCache, ArrayCacheByTimestamp, ArrayCacheBySymbolById, ArrayCacheBySymbolBySide # noqa: F402', '\n', '\n'],
phpHeaders: [],
phpFileSync: this.wsTestsDirectories.php + currentFolder + testNameUncameled + '.php',
};
this.transpileAndSaveExchangeTests ([test]);
}
// -----------------------------------------------------------------------
async transpileEverything (force = false, child = false) {
// default pattern is '.js'
// const [ /* node */, /* script */, pattern ] = process.argv.filter (x => !x.startsWith ('--'))
const exchanges = process.argv.slice (2).filter (x => !x.startsWith ('--'))
// , python2Folder = './python/ccxtpro/', // CCXT Pro does not support Python 2
, python3Folder = './python/ccxt/pro/'
, phpAsyncFolder = './php/pro/'
, jsFolder = './js/src/pro/'
, tsFolder = './ts/src/pro/'
, options = { /* python2Folder, */ python3Folder, phpAsyncFolder, jsFolder, exchanges }
const transpilingSingleExchange = (exchanges.length === 1); // when transpiling single exchange, we can skip some steps because this is only used for testing/debugging
if (transpilingSingleExchange) {
force = true; // when transpiling single exchange, we always force
}
// createFolderRecursively (python2Folder)
if (!transpilingSingleExchange) {
if (this.buildPython) {
createFolderRecursively (python3Folder)
}
if (this.buildPHP) {
createFolderRecursively (phpAsyncFolder)
}
}
const classes = this.transpileDerivedExchangeFiles (tsFolder, options, '.ts', force, child || exchanges.length)
if (transpilingSingleExchange) {
return;
}
this.transpileWsTests ()
if (child) {
return
}
if (classes === null) {
log.bright.yellow ('0 files transpiled.')
return;
}
//*/
// this.transpileErrorHierarchy ({ tsFilename })
log.bright.green ('Transpiled successfully.')
}
afterTranspileClass (result, contents) {
// if same class import (like binanceWS extending binanceRest)
if (result.baseClass === result.className + 'Rest') {
return result;
}
// we need this because exchanges like binanceusWs extends binanceWs but we need to get the binanceus
// Rest describe() to inherit all the properties
const matchOfRestImports = contents.matchAll('\nimport (.*?)Rest from \'..(.*?)\';');
const matches = [...matchOfRestImports];
if (matches.length) {
for (const match of matches) {
if (match[1]) {
const exchangeName = match[1];
const exchangeNameRest = exchangeName + 'Rest';
result.python3 = result.python3.replace ('\nclass ', 'import ccxt.async_support.' + exchangeName + ' as ' + exchangeNameRest + '\n\n\nclass ');
// correct `new Xyz()` format
result.python3 = result.python3.replace ('new ' + exchangeNameRest, exchangeNameRest);
result.phpAsync = result.phpAsync.replace ('new '+ exchangeNameRest, 'new \\ccxt\\async\\' + exchangeName);
}
}
}
return result;
}
}
// ============================================================================
// main entry point
if (isMainEntry(import.meta.url)) { // called directly like `node module`
const transpiler = new CCXTProTranspiler ()
const test = process.argv.includes ('--test') || process.argv.includes ('--tests');
const force = process.argv.includes ('--force')
const multiprocess = process.argv.includes ('--multiprocess') || process.argv.includes ('--multi')
const child = process.argv.includes ('--child')
const pythonOnly = process.argv.includes ('--python');
const phpOnly = process.argv.includes ('--php');
if (phpOnly) {
transpiler.buildPython = false // it's easier to handle the language to build this way instead of doing something like (build python only)
}
if (pythonOnly) {
transpiler.buildPHP = false
}
if (!child && !multiprocess) {
log.bright.green ('isForceTranspile', force)
}
if (test) {
transpiler.transpileWsTests ()
}
else if (multiprocess) {
parallelizeTranspiling (exchanges.ws, undefined, force, pythonOnly, phpOnly)
} else {
(async () => {
await transpiler.transpileEverything (force, child)
})()
}
} else {
// do nothing if required as a module
}
// ============================================================================
export default CCXTProTranspiler