Skip to content

Instantly share code, notes, and snippets.

@alexsavio
alexsavio / handlebars_helpers.md
Created October 21, 2023 07:50
A blog post on how to write Handlebars.rs helpers

Author: Alexandre Manhães Savio alexsavio@gmaill.com

Date: 10.10.2023

Handlebars is a modern and extensible templating solution originally created in the JavaScript world. It’s used by many popular frameworks like Ember.js and Chaplin. It’s also ported to some other platforms such as Java.

Handlebars is a template rendering engine but lacks extensive filters and transformers for context data during rendering. Like many such libraries, Handlebars allows you to create custom operations, called 'helpers'.

Here I will write an instruction set on how to write custom helpers for handlebars-rust.

@alexsavio
alexsavio / handlebars_helpers.rs
Created October 10, 2023 09:01
handlebars helpers
use handlebars::{Handlebars, RenderError, HelperDef, RenderContext, Helper, Context, handlebars_helper};
use serde_json::Value as Json;
// #[allow(non_camel_case_types)]
// pub struct is_defined;
// impl HelperDef for is_defined {
// fn call_inner<'reg: 'rc, 'rc>(
// &self,
// h: &Helper<'reg, 'rc>,
@alexsavio
alexsavio / nested_access.py
Created August 30, 2022 12:53
Python simple nested structure access
# -*- coding: utf-8 -*-
from functools import singledispatch
def getvalue(value, path, default=None):
"""Get a named attribute or item from an object; getvalue(x, 'y.z')
is equivalent to x.y.z
When a default argument is given, it is returned when the attribute doesn't
exist; without it, None returned
List indexing supported x.0.y.1.z etc.
@alexsavio
alexsavio / git-crypt-rm-gpg-user.sh
Last active April 20, 2022 16:01 — forked from etam/git-crypt-rm-gpg-user.sh
Git-crypt remove user.
#!/usr/bin/env bash -x
#
# Script to remove GPG user (recipient) with git-crypt
#
# It will re-initialize git-crypt for the repository and re-add all keys except
# the one requested for removal.
#
# Note: You still need to change all your secrets to fully protect yourself.
# Removing a user will prevent them from reading future changes but they will
# still have a copy of the data up to the point of their removal.
@alexsavio
alexsavio / ssm_transfer.py
Created October 26, 2021 08:52
SSM Secrets Repository
"""
pip install boto3 'boto3-stubs[ssm]'
"""
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_ssm.client import SSMClient
@alexsavio
alexsavio / recapitalize.py
Last active June 10, 2021 10:43
Recapitalize names
"""
Recapitalize a string of words that has passed a pre-processing, word-cuttind and case lowering process.
"""
import re
import difflib
from typing import Iterator, List, Tuple
def recapitalize_name(original: str, result: str) -> str:
"""Return the `result` with the words capitalized as they appear in `original`."""
@alexsavio
alexsavio / pubsub.py
Created April 19, 2021 07:13
GCP PubSub publisher
import time
from flask import Request
from google.cloud import pubsub_v1
from lib.logger import get_logger
logger = get_logger(__name__)
@alexsavio
alexsavio / switch_cloudwatch_rules.py
Created December 1, 2020 21:50
Switch on/off AWS CloudWatch Rules
import os
import logging
from typing import Dict, List
import boto3
from botocore.exceptions import ClientError
class CloudWatchRules:
@alexsavio
alexsavio / gnupg_scdaemon.md
Created February 11, 2020 15:19 — forked from artizirk/gnupg_scdaemon.md
OpenPGP SSH access with Yubikey and GnuPG

OpenPGP SSH access with Yubikey and GnuPG

Yubikey, Smart Cards, OpenSC and GnuPG are pain in the ass to get working. Those snippets here sould help alleviate pain.

Yubikey Config under Ubuntu

To reset and disable not used modes on Yubikey you need the ykman program

You can install it using those commands

@alexsavio
alexsavio / test_timed_cache.py
Created February 5, 2020 17:02
Python TimedCache
from time import sleep
from timed_cache import Memoize
def test_memoize():
"""
Test if the cached function results expires in the expected time.
"""