• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef FLATBUFFERS_INCLUDE_CODEGEN_PYTHON_H_
2 #define FLATBUFFERS_INCLUDE_CODEGEN_PYTHON_H_
3 
4 #include <cstdint>
5 #include <set>
6 #include <string>
7 #include <vector>
8 
9 #include "codegen/namer.h"
10 
11 namespace flatbuffers {
12 namespace python {
13 static const Namer::Config kConfig = {
14     /*types=*/Case::kKeep,
15     /*constants=*/Case::kScreamingSnake,
16     /*methods=*/Case::kUpperCamel,
17     /*functions=*/Case::kUpperCamel,
18     /*fields=*/Case::kLowerCamel,
19     /*variable=*/Case::kLowerCamel,
20     /*variants=*/Case::kKeep,
21     /*enum_variant_seperator=*/".",
22     /*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
23     /*namespaces=*/Case::kKeep,  // Packages in python.
24     /*namespace_seperator=*/".",
25     /*object_prefix=*/"",
26     /*object_suffix=*/"T",
27     /*keyword_prefix=*/"",
28     /*keyword_suffix=*/"_",
29     /*filenames=*/Case::kKeep,
30     /*directories=*/Case::kKeep,
31     /*output_path=*/"",
32     /*filename_suffix=*/"",
33     /*filename_extension=*/".py",
34 };
35 
36 static const Namer::Config kStubConfig = {
37     /*types=*/Case::kKeep,
38     /*constants=*/Case::kScreamingSnake,
39     /*methods=*/Case::kUpperCamel,
40     /*functions=*/Case::kUpperCamel,
41     /*fields=*/Case::kLowerCamel,
42     /*variables=*/Case::kLowerCamel,
43     /*variants=*/Case::kKeep,
44     /*enum_variant_seperator=*/".",
45     /*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
46     /*namespaces=*/Case::kKeep,  // Packages in python.
47     /*namespace_seperator=*/".",
48     /*object_prefix=*/"",
49     /*object_suffix=*/"T",
50     /*keyword_prefix=*/"",
51     /*keyword_suffix=*/"_",
52     /*filenames=*/Case::kKeep,
53     /*directories=*/Case::kKeep,
54     /*output_path=*/"",
55     /*filename_suffix=*/"",
56     /*filename_extension=*/".pyi",
57 };
58 
59 // `Version` represent a Python version.
60 //
61 // The zero value (i.e. `Version{}`) represents both Python2 and Python3.
62 //
63 // https://docs.python.org/3/faq/general.html#how-does-the-python-version-numbering-scheme-work
64 struct Version {
65   explicit Version(const std::string &version);
66 
67   bool IsValid() const;
68 
69   int16_t major = 0;
70   int16_t minor = 0;
71   int16_t micro = 0;
72 };
73 
74 std::set<std::string> Keywords(const Version &version);
75 
76 struct Import {
IsLocalImport77   bool IsLocal() const { return module == "."; }
78 
79   std::string module;
80   std::string name;
81 };
82 
83 struct Imports {
84   const python::Import &Import(const std::string &module);
85   const python::Import &Import(const std::string &module,
86                                const std::string &name);
87 
88   std::vector<python::Import> imports;
89 };
90 }  // namespace python
91 }  // namespace flatbuffers
92 
93 #endif  // FLATBUFFERS_INCLUDE_CODEGEN_PYTHON_H_
94