Skip to content

Commit 702b461

Browse files
committed
python support
1 parent ae2c9da commit 702b461

File tree

4 files changed

+1435
-0
lines changed

4 files changed

+1435
-0
lines changed

main/java/uk/co/real_logic/sbe/generation/TargetCodeGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import uk.co.real_logic.sbe.generation.java.JavaGenerator;
2323
import uk.co.real_logic.sbe.generation.java.JavaMockPojoGenerator;
2424
import uk.co.real_logic.sbe.generation.java.PackageOutputManager;
25+
import uk.co.real_logic.sbe.generation.python.PythonGenerator;
26+
import uk.co.real_logic.sbe.generation.python.ModuleOutputManager;
2527
import uk.co.real_logic.sbe.ir.Ir;
2628

2729
import java.io.IOException;
@@ -49,6 +51,15 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
4951
}
5052
},
5153

54+
PYTHON()
55+
{
56+
public CodeGenerator newInstance(final Ir ir, final String outputDir)
57+
throws IOException
58+
{
59+
return new PythonGenerator(ir, new ModuleOutputManager(outputDir, ir.applicableNamespace()));
60+
}
61+
},
62+
5263
CPP98()
5364
{
5465
public CodeGenerator newInstance(final Ir ir, final String outputDir)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package uk.co.real_logic.sbe.generation.python;
17+
18+
import uk.co.real_logic.sbe.generation.OutputManager;
19+
import uk.co.real_logic.sbe.util.Verify;
20+
21+
import java.io.*;
22+
23+
/**
24+
* {@link OutputManager} for managing the creation of C++98 source files as the target of code generation.
25+
* The character encoding for the {@link java.io.Writer} is UTF-8.
26+
*/
27+
public class ModuleOutputManager implements OutputManager
28+
{
29+
private final File outputDir;
30+
31+
/**
32+
* Create a new {@link uk.co.real_logic.sbe.generation.OutputManager} for generating C++98 source files into a given package.
33+
*
34+
* @param baseDirectoryName for the generated source code.
35+
* @param namespaceName for the generated source code relative to the baseDirectoryName.
36+
* @throws IOException if an error occurs during output
37+
*/
38+
public ModuleOutputManager(final String baseDirectoryName, final String namespaceName)
39+
throws IOException
40+
{
41+
Verify.notNull(baseDirectoryName, "baseDirectoryName");
42+
Verify.notNull(namespaceName, "applicableNamespace");
43+
44+
final String dirName =
45+
(baseDirectoryName.endsWith("" + File.separatorChar) ? baseDirectoryName : baseDirectoryName + File.separatorChar) +
46+
namespaceName.replace('.', '_');
47+
48+
outputDir = new File(dirName);
49+
if (!outputDir.exists())
50+
{
51+
if (!outputDir.mkdirs())
52+
{
53+
throw new IllegalStateException("Unable to create directory: " + dirName);
54+
}
55+
}
56+
}
57+
58+
/**
59+
* Create a new output which will be a C++98 source file in the given package.
60+
*
61+
* The {@link java.io.Writer} should be closed once the caller has finished with it. The Writer is
62+
* buffer for efficient IO operations.
63+
*
64+
* @param name the name of the C++ class.
65+
* @return a {@link java.io.Writer} to which the source code should be written.
66+
*/
67+
public Writer createOutput(final String name) throws IOException
68+
{
69+
final File targetFile = new File(outputDir, name + ".py");
70+
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
71+
}
72+
}

0 commit comments

Comments
 (0)