/** * Compiles a set of Soy files into corresponding Python source files. * * @param args Should contain command-line flags and the list of paths to the Soy files. * @throws IOException If there are problems reading the input files or writing the output file. */ public static void main(final String[] args) throws IOException { new SoyToPySrcCompiler().runMain(args); }
@Override void validateFlags() { if (runtimePath.length() == 0) { exitWithError("Must provide the Python runtime library path."); } if (outputPathFormat.isEmpty()) { exitWithError("Must provide the output path format."); } }
SyntaxVersion parsedVersion = SyntaxVersion.forName(syntaxVersion); if (parsedVersion.num < SyntaxVersion.V2_0.num) { exitWithError("Declared syntax version must be 2.0 or greater."); SoyFileSet sfs = sfsBuilder.build(); ImmutableMap<String, String> manifest = loadNamespaceManifest(namespaceManifestPaths); if (!manifest.isEmpty() && outputNamespaceManifest == null) { exitWithError("Namespace manifests provided without outputting a new manifest.");
@Override void compile(SoyFileSet.Builder sfsBuilder) throws IOException { // Disallow external call entirely in Python. sfsBuilder.setAllowExternalCalls(false); // Require strict templates in Python. sfsBuilder.setStrictAutoescapingRequired(true); SoyFileSet sfs = sfsBuilder.build(); // Load the manifest if available. ImmutableMap<String, String> manifest = loadNamespaceManifest(namespaceManifestPaths); if (!manifest.isEmpty() && outputNamespaceManifest == null) { exitWithError("Namespace manifests provided without outputting a new manifest."); } // Create SoyPySrcOptions. SoyPySrcOptions pySrcOptions = new SoyPySrcOptions( runtimePath, environmentModulePath, bidiIsRtlFn, translationClass, manifest, outputNamespaceManifest); // Compile. sfs.compileToPySrcFiles(outputPathFormat, pySrcOptions); }
/** * Compiles a set of Soy files into corresponding Python source files. * * @param args Should contain command-line flags and the list of paths to the Soy files. * @throws IOException If there are problems reading the input files or writing the output file. */ public static void main(final String[] args) throws IOException { new SoyToPySrcCompiler().runMain(args); }
@Override void validateFlags() { if (runtimePath.length() == 0) { exitWithError("Must provide the Python runtime library path."); } if (outputPathFormat.isEmpty()) { exitWithError("Must provide the output path format."); } }
/** * Load the manifest files provided at namespaceManifestPaths, deserialize (via gson), and combine * into a map containing all soy namespaces to their Python paths. */ private ImmutableMap<String, String> loadNamespaceManifest(List<String> namespaceManifestPaths) { if (namespaceManifestPaths.isEmpty()) { return ImmutableMap.of(); } ImmutableMap.Builder<String, String> manifest = new ImmutableMap.Builder<>(); for (String manifestPath : namespaceManifestPaths) { try (Reader manifestFile = Files.newReader(new File(manifestPath), StandardCharsets.UTF_8)) { Properties prop = new Properties(); prop.load(manifestFile); for (String namespace : prop.stringPropertyNames()) { manifest.put(namespace, prop.getProperty(namespace)); } } catch (IOException e) { exitWithError("Unable to read the namespaceManifest file at " + manifestPath); } } return manifest.build(); } }
/** * Load the manifest files provided at namespaceManifestPaths, deserialize (via gson), and combine * into a map containing all soy namespaces to their Python paths. */ private ImmutableMap<String, String> loadNamespaceManifest(List<String> namespaceManifestPaths) { if (namespaceManifestPaths.isEmpty()) { return ImmutableMap.of(); } ImmutableMap.Builder<String, String> manifest = new ImmutableMap.Builder<>(); for (String manifestPath : namespaceManifestPaths) { try (Reader manifestFile = Files.newReader(new File(manifestPath), StandardCharsets.UTF_8)) { Properties prop = new Properties(); prop.load(manifestFile); for (String namespace : prop.stringPropertyNames()) { manifest.put(namespace, prop.getProperty(namespace)); } } catch (IOException e) { exitWithError("Unable to read the namespaceManifest file at " + manifestPath); } } return manifest.build(); } }