Example #1
0
 def render(self, function: str) -> List[str]:
     title: List[str] = []
     path, method = function.rsplit(":", 1)
     raw_title = get_openapi_summary(path, method)
     title.extend(raw_title.splitlines())
     title = ["# " + line for line in title]
     if check_requires_administrator(path, method):
         title.append("{!api-admin-only.md!}")
     return title
Example #2
0
    def generate_text(self, match: Match[str]) -> List[str]:
        language = match.group(1) or ""
        function = match.group(2)
        key = match.group(3)
        if self.api_url is None:
            raise AssertionError(
                "Cannot render curl API examples without API URL set.")

        if key == "fixture":
            text = self.render(function)
        elif key == "example":
            path, method = function.rsplit(":", 1)
            admin_config = language in ADMIN_CONFIG_LANGUAGES and check_requires_administrator(
                path, method)
            text = SUPPORTED_LANGUAGES[language]["render"](
                function, api_url=self.api_url, admin_config=admin_config)
        return text
Example #3
0
    def run(self, lines: List[str]) -> List[str]:
        done = False
        while not done:
            for line in lines:
                loc = lines.index(line)
                match = MACRO_REGEXP.search(line)

                if match:
                    language, options = parse_language_and_options(
                        match.group(2))
                    function = match.group(3)
                    key = match.group(4)
                    argument = match.group(6)
                    if self.api_url is None:
                        raise AssertionError(
                            "Cannot render curl API examples without API URL set."
                        )
                    options["api_url"] = self.api_url

                    if key == "fixture":
                        if argument:
                            text = self.render_fixture(function, name=argument)
                    elif key == "example":
                        path, method = function.rsplit(":", 1)
                        if language in ADMIN_CONFIG_LANGUAGES and check_requires_administrator(
                                path, method):
                            text = SUPPORTED_LANGUAGES[language]["render"](
                                function, admin_config=True)
                        else:
                            text = SUPPORTED_LANGUAGES[language]["render"](
                                function, **options)

                    # The line that contains the directive to include the macro
                    # may be preceded or followed by text or tags, in that case
                    # we need to make sure that any preceding or following text
                    # stays the same.
                    line_split = MACRO_REGEXP.split(line, maxsplit=0)
                    preceding = line_split[0]
                    following = line_split[-1]
                    text = [preceding, *text, following]
                    lines = lines[:loc] + text + lines[loc + 1:]
                    break
            else:
                done = True
        return lines
Example #4
0
    def generate_text(self, match: Match[str]) -> List[str]:
        language, options = parse_language_and_options(match.group(2))
        function = match.group(3)
        key = match.group(4)
        if self.api_url is None:
            raise AssertionError(
                "Cannot render curl API examples without API URL set.")
        options["api_url"] = self.api_url

        if key == "fixture":
            text = self.render(function)
        elif key == "example":
            path, method = function.rsplit(":", 1)
            if language in ADMIN_CONFIG_LANGUAGES and check_requires_administrator(
                    path, method):
                text = SUPPORTED_LANGUAGES[language]["render"](
                    function, admin_config=True)
            else:
                text = SUPPORTED_LANGUAGES[language]["render"](function,
                                                               **options)
        return text