def legacy_flattened_fields(self) -> Mapping[str, Field]:
     """Return the legacy flattening interface: top level fields only,
     required fields first"""
     required, optional = utils.partition(lambda f: f.required,
                                          self.input.fields.values())
     return collections.OrderedDict(
         (f.name, f) for f in chain(required, optional))
    def get_response(self, api_schema: api.API,
                     opts: Options) -> CodeGeneratorResponse:
        """Return a :class:`~.CodeGeneratorResponse` for this library.

        This is a complete response to be written to (usually) stdout, and
        thus read by ``protoc``.

        Args:
            api_schema (~api.API): An API schema object.
            opts (~.options.Options): An options instance.

        Returns:
            ~.CodeGeneratorResponse: A response describing appropriate
            files and contents. See ``plugin.proto``.
        """
        output_files: Dict[str, CodeGeneratorResponse.File] = OrderedDict()
        sample_templates, client_templates = utils.partition(
            lambda fname: os.path.basename(fname) == samplegen.
            DEFAULT_TEMPLATE_NAME,
            self._env.loader.list_templates(),  # type: ignore
        )

        # We generate code snippets *before* the library code so snippets
        # can be inserted into method docstrings.
        snippet_idx = snippet_index.SnippetIndex(api_schema)
        if sample_templates:
            sample_output, snippet_idx = self._generate_samples_and_manifest(
                api_schema,
                snippet_idx,
                self._env.get_template(sample_templates[0]),
                opts=opts,
            )
            output_files.update(sample_output)

        # Iterate over each template and add the appropriate output files
        # based on that template.
        # Sample templates work differently: there's (usually) only one,
        # and instead of iterating over it/them, we iterate over samples
        # and plug those into the template.
        for template_name in client_templates:
            # Quick check: Skip "private" templates.
            filename = template_name.split("/")[-1]
            if filename.startswith("_") and filename != "__init__.py.j2":
                continue

            # Append to the output files dictionary.
            output_files.update(
                self._render_template(template_name,
                                      api_schema=api_schema,
                                      opts=opts,
                                      snippet_index=snippet_idx))

        # Return the CodeGeneratorResponse output.
        res = CodeGeneratorResponse(file=[i for i in output_files.values()
                                          ])  # type: ignore
        res.supported_features |= CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL  # type: ignore
        return res
Beispiel #3
0
    def get_response(
        self,
        api_schema: api.API,
        opts: options.Options
    ) -> CodeGeneratorResponse:
        """Return a :class:`~.CodeGeneratorResponse` for this library.

        This is a complete response to be written to (usually) stdout, and
        thus read by ``protoc``.

        Args:
            api_schema (~api.API): An API schema object.
            opts (~.options.Options): An options instance.

        Returns:
            ~.CodeGeneratorResponse: A response describing appropriate
            files and contents. See ``plugin.proto``.
        """
        output_files: Dict[str, CodeGeneratorResponse.File] = OrderedDict()

        sample_templates, client_templates = utils.partition(
            lambda fname: os.path.basename(
                fname) == samplegen.DEFAULT_TEMPLATE_NAME,
            self._env.loader.list_templates())

        # Iterate over each template and add the appropriate output files
        # based on that template.
        # Sample templates work differently: there's (usually) only one,
        # and instead of iterating over it/them, we iterate over samples
        # and plug those into the template.
        for template_name in client_templates:
            # Sanity check: Skip "private" templates.
            filename = template_name.split('/')[-1]
            if filename.startswith('_') and filename != '__init__.py.j2':
                continue

            # Append to the output files dictionary.
            output_files.update(
                self._render_template(
                    template_name,
                    api_schema=api_schema,
                    opts=opts
                )
            )

        output_files.update(self._generate_samples_and_manifest(
            api_schema,
            self._env.get_template(sample_templates[0]),
        ))

        # Return the CodeGeneratorResponse output.
        return CodeGeneratorResponse(file=[i for i in output_files.values()])
    def get_response(self, api_schema: api.API) -> CodeGeneratorResponse:
        """Return a :class:`~.CodeGeneratorResponse` for this library.

        This is a complete response to be written to (usually) stdout, and
        thus read by ``protoc``.

        Args:
            api_schema (~api.API): An API schema object.

        Returns:
            ~.CodeGeneratorResponse: A response describing appropriate
            files and contents. See ``plugin.proto``.
        """
        output_files: Dict[str, CodeGeneratorResponse.File] = OrderedDict()

        # TODO: handle sample_templates specially, generate samples.
        sample_templates, client_templates = utils.partition(
            lambda fname: os.path.basename(fname) == samplegen.TEMPLATE_NAME,
            self._env.loader.list_templates())

        # Iterate over each template and add the appropriate output files
        # based on that template.
        for template_name in client_templates:
            # Sanity check: Skip "private" templates.
            filename = template_name.split('/')[-1]
            if filename.startswith('_') and filename != '__init__.py.j2':
                continue

            # Append to the output files dictionary.
            output_files.update(
                self._render_template(
                    template_name,
                    api_schema=api_schema,
                ))

        # Return the CodeGeneratorResponse output.
        return CodeGeneratorResponse(file=[i for i in output_files.values()])