# Render markdown template for nKode enrollment
from pathlib import Path
import numpy as np
from jinja2 import Environment, FileSystemLoader, select_autoescape
from secrets import choice
from string import ascii_lowercase
from src.models import NKodePolicy, KeypadSize
from src.nkode_api import NKodeAPI
from src.utils import select_keys_with_passcode_values
def render_markdown_template(template_path, output_path, context):
"""
Render a Jinja2 markdown template with the given context and save to output_path.
Args:
template_path (Path): Path to the template file
output_path (Path): Path where the rendered file will be saved
context (dict): Template variables
"""
template_dir = template_path.parent
template_file = template_path.name
env = Environment(
loader=FileSystemLoader(template_dir),
autoescape=select_autoescape(['html', 'xml']),
trim_blocks=True,
lstrip_blocks=True
)
template = env.get_template(template_file)
rendered = template.render(**context)
with open(output_path, 'w') as f:
f.write(rendered)
print(f"Template rendered to {output_path}")
def display_icons(icons_array: np.ndarray, kp: KeypadSize) -> str:
icons = "["
for row in icons_array.reshape(-1, kp.numb_of_keys):
icons += ",".join(row)
icons += "
"
icons = icons[:-5]
icons += "]"
return icons
def display_icons_keypad(icons_array: np.ndarray, kp: KeypadSize) -> str:
icons = ""
for idx, row in enumerate(icons_array.reshape(-1, kp.numb_of_keys)):
icons += f"Key {idx}: "
icons += str(row)
icons += "
"
return icons
if __name__ == "__main__":
api = NKodeAPI()
user_icons = np.array([
"😀", "😂", "🥳", "😍", "🤓",
"😎", "🥺", "😡", "😱", "🤯",
"🥰", "😴", "🤔", "🙃", "😇",
"🤖", "👽", "👾", "🐱", "🐶",
"🦁", "🐻", "🐸", "🐙", "🦄",
"🌟", "⚡", "🔥", "🍕", "🎉"
])
policy = NKodePolicy(
max_nkode_len=10,
min_nkode_len=4,
distinct_positions=0,
distinct_properties=4,
)
keypad_size = KeypadSize(
numb_of_keys=5,
props_per_key=6
)
customer_id = api.create_new_customer(keypad_size, policy)
signup_session_id, set_signup_keypad = api.generate_signup_keypad(customer_id)
ordered_set_icons = user_icons[set_signup_keypad]
username = "test_username" + "".join([choice(ascii_lowercase) for _ in range(6)])
passcode_len = 4
passcode_property_indices = np.random.choice(set_signup_keypad.reshape(-1), size=passcode_len,
replace=False).tolist()
selected_keys_set = select_keys_with_passcode_values(passcode_property_indices, set_signup_keypad,
keypad_size.numb_of_keys)
confirm_keypad = api.set_nkode(username, customer_id, selected_keys_set, signup_session_id)
selected_keys_confirm = select_keys_with_passcode_values(passcode_property_indices, confirm_keypad,
keypad_size.numb_of_keys)
ordered_confirm_icons = user_icons[confirm_keypad]
print(f"Selected Keys\n{selected_keys_confirm}")
success = api.confirm_nkode(username, customer_id, selected_keys_confirm, signup_session_id)
context = {
"email": "user@example.com",
"signup_session_id": signup_session_id,
"set_keypad": display_icons_keypad(set_signup_keypad.reshape(-1, keypad_size.numb_of_keys), keypad_size),
"icon_matrix": display_icons(user_icons, keypad_size),
"ordered_keypad": display_icons_keypad(ordered_set_icons, keypad_size),
"passcode_user_icons": str(user_icons[passcode_property_indices]),
"selected_keys_set": str(selected_keys_set),
"confirm_keypad": display_icons_keypad(confirm_keypad.reshape(-1, keypad_size.numb_of_keys), keypad_size),
"confirm_ordered_keypad": display_icons_keypad(ordered_confirm_icons.reshape(-1, keypad_size.numb_of_keys), keypad_size),
"confirm_key_selection": selected_keys_confirm
}
# Render the template
render_markdown_template(Path("./enrollment_diagram.template.md"), Path("./enrollment_diagram.md"), context)