tars2python.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _TARS2PYTHON_H
  17. #define _TARS2PYTHON_H
  18. #include "parse.h"
  19. #include <cassert>
  20. #include <set>
  21. #include <string>
  22. using namespace tars;
  23. #if 0
  24. #include "util/tc_md5.h"
  25. #include "util/tc_file.h"
  26. #include "util/tc_common.h"
  27. #endif
  28. class Tars2Python
  29. {
  30. public:
  31. void setBaseDir(const string &dir)
  32. {
  33. _baseDir = dir;
  34. }
  35. void setBasePackage(const string &prefix)
  36. {
  37. _packagePrefix = prefix;
  38. if (_packagePrefix.length() != 0 && _packagePrefix.substr(_packagePrefix.length()-1, 1) != ".")
  39. {
  40. _packagePrefix += ".";
  41. }
  42. }
  43. void createFile(const string &file);
  44. private:
  45. string generatePython(const ConstPtr &pPtr, const std::string &sNamespace);
  46. string generatePython(const EnumPtr &pPtr, const std::string &sNamespace);
  47. string generatePython(const StructPtr & pPtr, const NamespacePtr &nPtr);
  48. string generatePython(const NamespacePtr &pPtr);
  49. string generatePython(const InterfacePtr &pPtr);
  50. string toFunctionName(const TypeIdPtr & pPtr, const string & sName);
  51. string getDataType(const TypePtr & pPtr);
  52. string getDefault(const TypeIdPtr & pPtr, const std::string &sDefault, const std::string & sCurStruct);
  53. string makeDataType(const TypePtr & pPtr);
  54. string getFilePath() const;
  55. void getNamespace(const string & sFile, set<string> & vec);
  56. void makePackages();
  57. void generatePython(const ContextPtr &pPtr) ;
  58. string makeParams(const OperationPtr &pPtr);
  59. string makeOperations(const OperationPtr &pPtr);
  60. string tostrEnum(const EnumPtr &pPtr);
  61. private:
  62. string _packagePrefix;
  63. string _baseDir;
  64. };
  65. #endif