Skip to content

Instantly share code, notes, and snippets.

import sys
import json
import yaml
import shutil
import subprocess
from ansible.plugins.action import ActionBase
"""
fails if bitwarden is not unlocked
@simonLeary42
simonLeary42 / stickies-always-match-style.json
Last active October 6, 2024 21:30
Karabiner: rebind ctrl+v to paste and match style in the Stickies app
{
"description": "ctrl+v -> ctrl+shift+option+v (only Stickies)",
"manipulators": [
{
"conditions": [
{
"bundle_identifiers": [
"^com\\.apple\\.Stickies$"
],
"type": "frontmost_application_if"
@simonLeary42
simonLeary42 / highlighted-diff.py
Created September 10, 2024 20:21
parses a patch file and adds highlighting for specific changes made within a block of text. May pass large strings into SequenceMatcher, which scales very poorly.
def _format_diff_strings(before: str, after: str) -> str:
before_lines = before.splitlines()
after_lines = after.splitlines()
diff = difflib.unified_diff(before_lines, after_lines, lineterm="")
output = ""
removed_lines = []
added_lines = []
def _format_added_removed():
# given the current state of the `added` and `removed` variables, add text to the `output`
@simonLeary42
simonLeary42 / diffr-python-strings.py
Last active September 10, 2024 20:24
Runs GNU Diff and [diffr](https://github.com/mookid/diffr) to compare strings without tempfiles. UNIX only.
import os
import threading
import subprocess
before = ""
after = ""
before_read_fd, before_write_fd = os.pipe()
after_read_fd, after_write_fd = os.pipe()
diff_proc = subprocess.Popen(
@simonLeary42
simonLeary42 / _do_paths_exist.py
Created September 3, 2024 14:44
don't use the `stat` module on each, that's too slow. Instead, do all at once
# ansible module
import os
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec=dict(
paths=dict(type="list", required=True),
follow=dict(type="bool", default=True),
),
@simonLeary42
simonLeary42 / dict2xml.py
Last active June 26, 2024 19:11
outputs an obscure subset of XML where the content of a given object must be either a string, or more objects, but not both. Also, the XML output attributes "__content__" and "__children__" are forbidden. Each child in the __children__ list must be a dict, with exactly one key: the data type of the child.
def dict2xml(x: dict, indent=0) -> str:
assert isinstance(x, dict)
assert len(x) == 1
datatype = list(x.keys())[0]
assert re.fullmatch(r"[a-zA-Z0-9_]+", datatype)
data = x[datatype]
body = ""
if "__content__" in data:
assert isinstance(data["__content__"], str)
body += f"{' '*(indent+2)}{data['__content__']}"

find:

(^[ \t]*def[ \t]*([a-zA-Z_]+).*?)\n+([ \t]*)

replace:

$1
$3print("function $2")
$3
function nodes_with_state(){
sinfo -N --json | jq -r '.sinfo | .[] | select(any(.node.state[]; .=="$1")) | .nodes.nodes[0]'
}
def make_string_sortable_numerically(string:str) -> List[int]:
"""
convert string to list of int unicode values
but, any groups of 2 or more digits are converted to an int
the output value for both those indexes = some big number + int
"abc100" -> [97, 98, 99, 10100, 10100, 10100]
"""
output = [ord(c) for c in string]
skip_these_indexes = [False]*len(string)
big_number = 10000
def string_matches_any_globs(x:str, globs:List[str]):
return any(fnmatch.fnmatch(x, y) for y in globs)
def find_files(walk_root_path:str, include_globs:List[str], exclude_globs:List[str]=None) -> list:
"""
excluding takes precidence over including
"""
output = []
for dirname, _, basenames in os.walk(walk_root_path):
for basename in basenames: