Skip to content

Commit ea50347

Browse files
committed
Prepare class WsjcppJsonRpc20ExportCliBase
1 parent 4cb64ba commit ea50347

13 files changed

+124
-50
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ list (APPEND WSJCPP_INCLUDE_DIRS "src/app")
1414
# package
1515
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20.h")
1616
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20.cpp")
17+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_base.h")
18+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_base.cpp")
1719
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_python.h")
1820
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_python.cpp")
1921
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_webjs.h")

example-of-exported/py3/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Automatically generated by wsjcpp-jsonrpc20.
44
* Version: v0.0.4
5-
* Date: Tue, 06 Oct 2020 02:19:51 GMT
5+
* Date: Wed, 07 Oct 2020 01:59:09 GMT
66

77
Example connect/disconnect:
88
```

example-of-exported/py3/libwsjcppjson20client/SomeClient.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
### This file was automatically generated by wsjcpp-jsonrpc20
44
### Version: v0.0.4
5-
### Date: Tue, 06 Oct 2020 02:19:51 GMT
5+
### Date: Wed, 07 Oct 2020 01:59:09 GMT
66

77
import asyncio
88
import websocket
@@ -44,6 +44,7 @@ def hasConnection(self):
4444

4545
def getToken(self):
4646
return self.__token
47+
4748
def setToken(self, token):
4849
if self.__token is None:
4950
self.__token = token

example-of-exported/webjs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Automatically generated by wsjcpp-jsonrpc20.
44
* Version: v0.0.4
5-
* Date: 06 Oct 2020
5+
* Date: 07 Oct 2020
66

77
Include script ```dist/libwsjcppjson20client.js```
88
Example connect:

example-of-exported/webjs/dist/libwsjcppjson20client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// This file was automatically generated by wsjcpp-jsonrpc20 (v0.0.4), date: 06 Oct 2020
1+
// This file was automatically generated by wsjcpp-jsonrpc20 (v0.0.4), date: 07 Oct 2020
22
window.SomeClient = window.SomeClient || (function() {
33
var self = {};
44
self.appName = 'wsjcpp-jsonrpc20';
55
self.appVersion = 'v0.0.4';
6-
self.appBuildDate = '06 Oct 2020';
6+
self.appBuildDate = '07 Oct 2020';
77
var _lastm = 0;
88
var _listeners = {};
99
var _connectionState = '?';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "wsjcpp_jsonrpc20_export_cli_base.h"
2+
3+
#include <iostream>
4+
#include <iomanip>
5+
#include <algorithm>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <memory>
9+
#include <iostream>
10+
#include <fstream>
11+
#include <iomanip> // put_time
12+
#include <ctime>
13+
#include <sstream>
14+
15+
// ---------------------------------------------------------------------
16+
// WsjcppJsonRpc20ExportCliBase
17+
18+
WsjcppJsonRpc20ExportCliBase::WsjcppJsonRpc20ExportCliBase(
19+
const std::string &sExportDir,
20+
const std::string &sPackageName
21+
) {
22+
TAG = "WsjcppJsonRpc20ExportCliBase";
23+
m_sExportDir = WsjcppCore::doNormalizePath(sExportDir);
24+
m_sPackageName = sPackageName;
25+
}
26+
27+
// ---------------------------------------------------------------------
28+
29+
std::string WsjcppJsonRpc20ExportCliBase::getExportDir() const {
30+
return m_sExportDir;
31+
}
32+
33+
// ---------------------------------------------------------------------
34+
35+
std::string WsjcppJsonRpc20ExportCliBase::getPackageName() const {
36+
return m_sPackageName;
37+
}
38+
39+
// ---------------------------------------------------------------------
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef WSJCPP_JSONRPC20_EXPORT_CLI_BASE_H
2+
#define WSJCPP_JSONRPC20_EXPORT_CLI_BASE_H
3+
4+
#include "wsjcpp_core.h"
5+
#include "wsjcpp_jsonrpc20.h"
6+
#include <vector>
7+
8+
class WsjcppJsonRpc20ExportCliBase {
9+
public:
10+
WsjcppJsonRpc20ExportCliBase(
11+
const std::string &sExportDir,
12+
const std::string &sPackageName
13+
);
14+
15+
std::string getExportDir() const;
16+
std::string getPackageName() const;
17+
18+
virtual bool doExportLib() = 0;
19+
20+
protected:
21+
std::string TAG;
22+
23+
private:
24+
std::string m_sExportDir;
25+
std::string m_sPackageName;
26+
};
27+
28+
#endif // WSJCPP_JSONRPC20_EXPORT_CLI_BASE_H

src/wsjcpp_jsonrpc20_export_cli_python.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ class PyCodeBuilder {
126126
WsjcppJsonRpc20ExportCliPython::WsjcppJsonRpc20ExportCliPython(
127127
const std::string &sExportDir,
128128
const std::string &sPackageName
129-
) {
129+
) : WsjcppJsonRpc20ExportCliBase(sExportDir, sPackageName) {
130130
TAG = "WsjcppJsonRpc20ExportCliPython";
131-
m_sExportDir = WsjcppCore::doNormalizePath(sExportDir);
132-
m_sPackageName = sPackageName;
133131
m_sAuthorName = "Unknown";
134132
m_sAuthorEmail = "unknown";
135133
m_sAppName = "unknown";
@@ -235,7 +233,7 @@ bool WsjcppJsonRpc20ExportCliPython::doExportLib() {
235233

236234
bool WsjcppJsonRpc20ExportCliPython::exportPrepareDirs() {
237235
std::vector<std::string> vCreateDirs;
238-
std::vector<std::string> vSplited = WsjcppCore::split(m_sExportDir, "/");
236+
std::vector<std::string> vSplited = WsjcppCore::split(this->getExportDir(), "/");
239237
std::string sExportDir = "";
240238
for (int i = 0; i < vSplited.size(); i++) {
241239
if (i > 0) {
@@ -245,7 +243,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportPrepareDirs() {
245243
sExportDir = WsjcppCore::doNormalizePath(sExportDir);
246244
vCreateDirs.push_back(sExportDir);
247245
}
248-
sExportDir = WsjcppCore::doNormalizePath(sExportDir + "/" + m_sPackageName);
246+
sExportDir = WsjcppCore::doNormalizePath(sExportDir + "/" + this->getPackageName());
249247
vCreateDirs.push_back(sExportDir);
250248

251249
for (int i = 0; i < vCreateDirs.size(); i++) {
@@ -267,20 +265,20 @@ bool WsjcppJsonRpc20ExportCliPython::exportPrepareDirs() {
267265
// ---------------------------------------------------------------------
268266

269267
bool WsjcppJsonRpc20ExportCliPython::prepareReadmeMdIfNeed() {
270-
std::string sReadmeMd = m_sExportDir + "/README.md";
268+
std::string sReadmeMd = this->getExportDir() + "/README.md";
271269
if (!WsjcppCore::fileExists(sReadmeMd)) {
272270
std::string sContentReadme =
273-
"#" + m_sPackageName + "\n\n"
271+
"#" + this->getPackageName() + "\n\n"
274272
+ m_sClassName + " Python Library for " + m_sAppName + "\n\n"
275273
+ "## Install \n\n"
276274
+ "```\n"
277-
+ "$ pip3 install " + m_sPackageName + " --upgrade\n"
275+
+ "$ pip3 install " + this->getPackageName() + " --upgrade\n"
278276
+ "```\n\n"
279277
+ "## Example code\n\n"
280278
+ "```\n"
281279
+ "#!/usr/bin/env python3\n"
282280
+ "# -*- coding: utf-8 -*-\n"
283-
+ "from " + m_sPackageName + " import " + m_sClassName + "\n\n"
281+
+ "from " + this->getPackageName() + " import " + m_sClassName + "\n\n"
284282
+ "client = " + m_sClassName + "(\"ws://host/ws-api/\")\n\n"
285283
+ "resp = client.server_api({})\n\n"
286284
+ "print(resp)\n"
@@ -296,7 +294,7 @@ bool WsjcppJsonRpc20ExportCliPython::prepareReadmeMdIfNeed() {
296294

297295
bool WsjcppJsonRpc20ExportCliPython::exportSetupPy() {
298296
std::ofstream setupPy;
299-
std::string sFilename = m_sExportDir + "/setup.py";
297+
std::string sFilename = this->getExportDir() + "/setup.py";
300298
WsjcppLog::info(TAG, "Prepare setup.py " + sFilename);
301299

302300
// https://packaging.python.org/tutorials/packaging-projects/
@@ -307,9 +305,9 @@ bool WsjcppJsonRpc20ExportCliPython::exportSetupPy() {
307305
" long_description = fh.read()\n"
308306
"\n"
309307
"setuptools.setup(\n"
310-
" name='" + m_sPackageName + "',\n"
308+
" name='" + this->getPackageName() + "',\n"
311309
" version='" + m_sAppVersion + "',\n"
312-
" packages=['" + m_sPackageName + "'],\n"
310+
" packages=['" + this->getPackageName() + "'],\n"
313311
" install_requires=['websocket-client>=0.56.0', 'requests>=2.21.0'],\n"
314312
" keywords=['" + WsjcppCore::join(m_vKeywords, "', '") + "'],\n"
315313
" author='" + m_sAuthorName + "',\n"
@@ -345,7 +343,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportSetupPy() {
345343
bool WsjcppJsonRpc20ExportCliPython::exportAPImd() {
346344

347345
std::ofstream apimd;
348-
std::string sFilename = m_sExportDir + "/API.md";
346+
std::string sFilename = this->getExportDir() + "/API.md";
349347
WsjcppLog::info(TAG, "Prepare API.md " + sFilename);
350348

351349
apimd.open(sFilename);
@@ -358,7 +356,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportAPImd() {
358356
apimd << "* Date: " << WsjcppCore::formatTimeForWeb(nSec) << "\n\n";
359357
apimd << "Example connect/disconnect:\n"
360358
<< "```\n"
361-
<< "from " + m_sPackageName + " import " + m_sClassName + " \n\n"
359+
<< "from " + getPackageName() + " import " + m_sClassName + " \n\n"
362360
<< "client = " + m_sClassName + "('ws://host:1234')\n"
363361
<< " ... \n"
364362
<< "client.close()\n"
@@ -426,7 +424,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportAPImd() {
426424
// ---------------------------------------------------------------------
427425

428426
bool WsjcppJsonRpc20ExportCliPython::exportInitPy() {
429-
std::string sFilename = m_sExportDir + "/" + m_sPackageName + "/__init__.py";
427+
std::string sFilename = this->getExportDir() + "/" + this->getPackageName() + "/__init__.py";
430428
WsjcppLog::info(TAG, "Prepare __init__.py " + sFilename);
431429
std::ofstream __init__;
432430
__init__.open (sFilename);
@@ -470,8 +468,10 @@ void exportCliPythonAddCheckDataTypeOfParam(
470468
}
471469
};
472470

471+
// ---------------------------------------------------------------------
472+
473473
bool WsjcppJsonRpc20ExportCliPython::exportClientPy() {
474-
std::string sFilename = m_sExportDir + "/" + m_sPackageName + "/" + m_sClassName + ".py";
474+
std::string sFilename = this->getExportDir() + "/" + this->getPackageName() + "/" + m_sClassName + ".py";
475475
WsjcppLog::info(TAG, "Prepare " + m_sClassName + ".py: " + sFilename);
476476

477477
std::ofstream clientpy;
@@ -534,6 +534,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() {
534534
.sub("def getToken(self):")
535535
.add("return self.__token")
536536
.end()
537+
.add("")
537538
.sub("def setToken(self, token):")
538539
.sub("if self.__token is None:")
539540
.add("self.__token = token")

src/wsjcpp_jsonrpc20_export_cli_python.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#ifndef WSJCPP_JSONRPC20_EXPORT_CLI_PYTHON_H
22
#define WSJCPP_JSONRPC20_EXPORT_CLI_PYTHON_H
33

4-
#include <string>
4+
#include "wsjcpp_jsonrpc20_export_cli_base.h"
55
#include <vector>
66
#include <map>
77

8-
class WsjcppJsonRpc20ExportCliPython {
8+
class WsjcppJsonRpc20ExportCliPython : public WsjcppJsonRpc20ExportCliBase {
99
public:
1010
WsjcppJsonRpc20ExportCliPython(
1111
const std::string &sExportDir,
@@ -22,11 +22,8 @@ class WsjcppJsonRpc20ExportCliPython {
2222
void addLoginMethod(const std::string &sMethod, const std::string &sResultVarName);
2323
void addLogoffMethod(const std::string &sMethod);
2424

25-
bool doExportLib();
25+
virtual bool doExportLib() override;
2626
private:
27-
std::string TAG;
28-
std::string m_sExportDir;
29-
std::string m_sPackageName;
3027
std::string m_sAuthorName;
3128
std::string m_sAuthorEmail;
3229
std::string m_sAppName;

src/wsjcpp_jsonrpc20_export_cli_webjs.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
WsjcppJsonRpc20ExportCliWebJs::WsjcppJsonRpc20ExportCliWebJs(
2020
const std::string &sExportDir,
2121
const std::string &sPackageName
22-
) {
22+
) : WsjcppJsonRpc20ExportCliBase(sExportDir, sPackageName) {
2323
TAG = "WsjcppJsonRpc20ExportCliWebJs";
24-
m_sExportDir = WsjcppCore::doNormalizePath(sExportDir);
25-
m_sPackageName = sPackageName;
2624
m_sAuthorName = "Unknown";
2725
m_sAuthorEmail = "unknown";
2826
m_sAppName = "unknown";
@@ -106,7 +104,7 @@ void WsjcppJsonRpc20ExportCliWebJs::setAppVersion(const std::string &sAppVersion
106104
// ---------------------------------------------------------------------
107105

108106
bool WsjcppJsonRpc20ExportCliWebJs::doExportLib() {
109-
std::string sBasicDir = "./" + m_sPackageName;
107+
std::string sBasicDir = "./" + this->getPackageName();
110108

111109
if (!prepareDirs()) {
112110
return false;
@@ -126,8 +124,8 @@ bool WsjcppJsonRpc20ExportCliWebJs::doExportLib() {
126124

127125
bool WsjcppJsonRpc20ExportCliWebJs::prepareDirs() {
128126
std::vector<std::string> vDirs;
129-
vDirs.push_back(m_sExportDir);
130-
vDirs.push_back(m_sExportDir + "/dist");
127+
vDirs.push_back(this->getExportDir());
128+
vDirs.push_back(this->getExportDir() + "/dist");
131129

132130
for (int i = 0; i < vDirs.size(); i++) {
133131
std::string sDir = vDirs[i];
@@ -150,8 +148,8 @@ bool WsjcppJsonRpc20ExportCliWebJs::prepareDirs() {
150148

151149
void WsjcppJsonRpc20ExportCliWebJs::exportPackageJson() {
152150
std::ofstream packageJson;
153-
std::cout << " * write code to " << m_sExportDir << "/package.json " << std::endl;
154-
packageJson.open (m_sExportDir + "/package.json");
151+
std::cout << " * write code to " << this->getExportDir() << "/package.json " << std::endl;
152+
packageJson.open (this->getExportDir() + "/package.json");
155153

156154
std::time_t t = std::time(nullptr);
157155
std::stringstream buffer;
@@ -160,10 +158,10 @@ void WsjcppJsonRpc20ExportCliWebJs::exportPackageJson() {
160158
// look here an example https://github.com/jquery/jquery/blob/master/package.json
161159
packageJson <<
162160
"{\n"
163-
" \"name\": \"" + m_sPackageName + "\",\n"
161+
" \"name\": \"" + this->getPackageName() + "\",\n"
164162
" \"version\": \"" + m_sAppVersion + "\",\n"
165163
" \"description\": \"" + m_sClassName + " JavaScript Web Client Library for " + m_sAppName + "\",\n"
166-
" \"main\": \"dist/" + m_sPackageName + ".js\",\n"
164+
" \"main\": \"dist/" + this->getPackageName() + ".js\",\n"
167165
" \"repository\": {\n"
168166
" \"type\": \"" + m_sRepositoryType + "\",\n"
169167
" \"url\": \"" + m_sRepositoryURL + "\"\n"
@@ -192,19 +190,19 @@ void WsjcppJsonRpc20ExportCliWebJs::exportPackageJson() {
192190
void WsjcppJsonRpc20ExportCliWebJs::exportAPImd() {
193191

194192
std::ofstream apimd;
195-
std::cout << " * write file to " + m_sExportDir + "/API.md" << std::endl;
196-
apimd.open(m_sExportDir + "/API.md");
193+
std::cout << " * write file to " + this->getExportDir() + "/API.md" << std::endl;
194+
apimd.open(this->getExportDir() + "/API.md");
197195

198196
std::time_t t = std::time(nullptr);
199197
std::stringstream buffer;
200198
buffer << std::put_time(std::gmtime(&t), "%d %b %Y");
201199

202200
apimd <<
203-
"# " + m_sPackageName + "\n\n"
201+
"# " + this->getPackageName() + "\n\n"
204202
" Automatically generated by " << m_sAppName << ". \n"
205203
" * Version: " << m_sAppVersion << "\n"
206204
" * Date: " << buffer.str() << "\n\n"
207-
" Include script ```dist/" + m_sPackageName + ".js```\n"
205+
" Include script ```dist/" + this->getPackageName() + ".js```\n"
208206
" Example connect:\n"
209207
"```\n"
210208
"var client = new " + m_sClassName + "();\n"
@@ -288,14 +286,14 @@ void WsjcppJsonRpc20ExportCliWebJs::exportAPImd() {
288286
// ---------------------------------------------------------------------
289287

290288
void WsjcppJsonRpc20ExportCliWebJs::exportSampleHtmlFile() {
291-
std::string sFilename = m_sExportDir + "/sample.html";
289+
std::string sFilename = this->getExportDir() + "/sample.html";
292290
std::ofstream sample_html;
293291
std::cout << " * write code to " << sFilename << std::endl;
294292
sample_html.open(sFilename);
295293
sample_html <<
296294
"<html>\r\n"
297295
"<head>\r\n"
298-
" <script src='dist/" + m_sPackageName + ".js'></script>\r\n"
296+
" <script src='dist/" + this->getPackageName() + ".js'></script>\r\n"
299297
" <script>\r\n"
300298
" function log(n) {\r\n"
301299
" console.log(n);\r\n"
@@ -370,7 +368,7 @@ void WsjcppJsonRpc20ExportCliWebJs::exportSampleHtmlFile() {
370368

371369
bool WsjcppJsonRpc20ExportCliWebJs::exportLibCliWebJSFile() {
372370

373-
std::string sFilename = m_sExportDir + "/dist/" + m_sPackageName + ".js";
371+
std::string sFilename = this->getExportDir() + "/dist/" + this->getPackageName() + ".js";
374372
std::ofstream libwjscppcli_web_js_file;
375373
std::cout << " * write code to " << sFilename << std::endl;
376374
libwjscppcli_web_js_file.open(sFilename);
@@ -687,7 +685,7 @@ bool WsjcppJsonRpc20ExportCliWebJs::exportLibCliWebJSFile() {
687685
// ---------------------------------------------------------------------
688686

689687
void WsjcppJsonRpc20ExportCliWebJs::exportLibCliWebServiceTSFile() {
690-
std::string sFilename = m_sExportDir + "/dist/" + m_sPackageName + ".service.ts";
688+
std::string sFilename = this->getExportDir() + "/dist/" + this->getPackageName() + ".service.ts";
691689
std::ofstream libwjscppcli_web_service_ts_file;
692690
std::cout << " * write code to " << sFilename << std::endl;
693691
libwjscppcli_web_service_ts_file.open(sFilename);

0 commit comments

Comments
 (0)