Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions cloudsmith_cli/cli/webserver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import html
import os
import socket
from functools import cached_property
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qsl, unquote, urlparse

import click

from .. import templates
from ..core.api.exceptions import ApiException
from ..core.api.init import initialise_api
from ..core.credentials.models import CredentialResult
Expand All @@ -16,32 +16,12 @@

def get_template_path(template_name):
"""Get the absolute path to a template file."""
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_dir, "templates", template_name)
return templates.template_path(template_name)


def render_template(template_name, **context):
"""
Render a template with the given context.

Args:
template_name: Name of the template file
context: Dictionary of variables to replace in the template

Returns:
Rendered HTML content
"""
template_path = get_template_path(template_name)

with open(template_path, encoding="utf-8") as file:
content = file.read()

# Replace placeholders with context values
for key, value in context.items():
placeholder = f"<!-- {key.upper()}_PLACEHOLDER -->"
content = content.replace(placeholder, value if value else "")

return content
"""Render a template with the given context (see :func:`cloudsmith_cli.templates.render`)."""
return templates.render(template_name, **context)


class AuthenticationWebServer(HTTPServer):
Expand Down
28 changes: 26 additions & 2 deletions cloudsmith_cli/templates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
"""Templates for the Cloudsmith CLI (HTML pages, config-file templates).

Templates use ``<!-- KEY_PLACEHOLDER -->`` markers so the files stay valid
HTML/XML and get normal editor validation; :func:`render` substitutes them.
"""
HTML templates for Cloudsmith CLI interface.
"""

import os


def template_path(name):
"""Return the absolute path to the template *name* in this package."""
return os.path.join(os.path.dirname(os.path.abspath(__file__)), name)


def render(name, **context):
"""Render template *name*, replacing ``<!-- KEY_PLACEHOLDER -->`` markers.

Each ``context`` key ``foo`` replaces ``<!-- FOO_PLACEHOLDER -->`` with its
value (empty string when falsy). Callers are responsible for escaping
values for the target format (e.g. XML-escaping).
"""
with open(template_path(name), encoding="utf-8") as handle:
content = handle.read()
for key, value in context.items():
placeholder = f"<!-- {key.upper()}_PLACEHOLDER -->"
content = content.replace(placeholder, value if value else "")
return content