コード例 #1
0
    def _code_snippet(self, repo_name, code_name):
        if self.environment.dependent_sections.get(repo_name):
            repo = self.environment.dependent_sections[repo_name]
            root = os.path.abspath(repo)

            if repo_name not in self.environment.code_snippets:
                snippets = {}
                names = []
                try:
                    # this uses `rg` as searching across files is not something we need to reprogram
                    output = subprocess.check_output([
                        'rg', '-m', '1', '-l', 'code_snippet [\w-]+ start',
                        root
                    ])
                    names = output.splitlines()
                except subprocess.CalledProcessError as e:
                    names = []
                finder = regex.compile(
                    r"""code_snippet ([\w-]+) start (\w+)\n(.*)\n.*?code_snippet \1 end""",
                    regex.MULTILINE | regex.DOTALL)
                for name in names:
                    path = os.path.join(root, name.decode())
                    f = open(path, 'r')
                    matches = finder.findall(f.read(), overlapped=True)
                    for match in matches:
                        snippets[match[0]] = """\n\n```%s\n%s\n```\n\n""" % (
                            match[1], match[2])
                self.environment.code_snippets[repo_name] = snippets

            if code_name in self.environment.code_snippets[repo_name]:
                return self.environment.code_snippets[repo_name][code_name]

            raise TemplateRuntimeError(
                'could not find code snippet "%s" under repo "%s" -- please check for "rg" or entry in ".gitignore"'
                % (code_name, repo_name))
        else:
            raise TemplateRuntimeError(
                'dependent section "%s" not defined in mkdocs.yml' %
                (repo_name))
コード例 #2
0
    def _get_parameter(self, name):
        if name not in self.environment.aws_parameter_store_values:

            # Create the ssm client as needed
            if self.environment.aws_parameter_store_client is None:
                session = botocore.session.get_session()
                self.environment.aws_parameter_store_client = session.create_client(
                    'ssm')

            try:
                result = self.environment.aws_parameter_store_client.get_parameter(
                    Name=name, WithDecryption=True)
                self.environment.aws_parameter_store_values[name] = result[
                    'Parameter']['Value']
            except botocore.exceptions.ClientError as ex:
                code = ex.response.get('Error', {}).get('Code')
                raise TemplateRuntimeError(
                    'Failed to retrieve value "{0}" from parameter store with error: {1}'
                    .format(name, code)) from None

        return self.environment.aws_parameter_store_values[name]
コード例 #3
0
 def raise_helper(msg):
     raise TemplateRuntimeError(msg)
コード例 #4
0
 def _raise(self, message, caller):
     """Execute the {% error %} statement, raising an exception."""
     raise TemplateRuntimeError(message)