diff options
author | David Wührer <def@gmx.at> | 2022-11-03 19:23:18 +0100 |
---|---|---|
committer | David Wührer <def@gmx.at> | 2022-11-03 19:23:18 +0100 |
commit | be555f29f47f81993c12bf76065a0cb6bb0fee96 (patch) | |
tree | eb5991d8b0309609746fe612068faeadf0c52e0b | |
parent | e60829f7b9c61dc870173a8dd8f2f85c17517ec4 (diff) |
Add a few old things.
-rwxr-xr-x | all | 6 | ||||
-rwxr-xr-x | any | 6 | ||||
-rwxr-xr-x | clone | 12 | ||||
-rwxr-xr-x | pf | 38 | ||||
-rwxr-xr-x | spt | 55 | ||||
-rwxr-xr-x | sum_up | 4 | ||||
-rwxr-xr-x | xmlbrowser | 39 | ||||
-rwxr-xr-x | yamlbrowser | 57 |
8 files changed, 217 insertions, 0 deletions
@@ -0,0 +1,6 @@ +#!/bin/bash + +for i in $(seq 1 $#) +do + [[ ${!i} ]] || exit 1 +done @@ -0,0 +1,6 @@ +#!/bin/bash + +for i in $(seq 1 $#) +do + [[ ${!i} ]] && exit 0 +done @@ -0,0 +1,12 @@ +#!/bin/bash + +set -eu + +for arg in "$@" +do + dir=${arg#*:} + dir="$HOME"/src/${dir%/*} + mkdir -pv "$dir" + cd "$dir" + git clone --verbose --progress "$arg" +done @@ -0,0 +1,38 @@ +#!/bin/bash + +set -ue + +get_parent () { + ps hoppid p$1 +} + +get_children () { + ps hopid --ppid "$1" +} + +get_ancestors () { + case $1 in + 1|'') : + ;; *) get_ancestors $(get_parent $1) + esac + echo "$1" +} + +get_descendents () { + for c in $(get_children "$1") + do echo "$c" + get_descendents "$c" + done +} + +declare -i p +for p in "$@" +do + [[ $p -ne 0 ]] && { + get_ancestors "$p" + get_descendents "$p" + } | { + readarray -t pids + ps -Hf "${pids[@]}" | sed "/^[^ ]* *$p /s/.*/[1m&[0m/" + } +done @@ -0,0 +1,55 @@ +#!/bin/bash + +### +# Single pass tabulate. +# +# Turn grid-aligned tables into tab-separated values +# using the first line as a template. +# Pre-existing tabs are preserved, +# which is probably not good. +## +# TODO: +# - Optionally require more than one space for column separation. + +shopt -s extglob + +spitline(){ + declare -i prevtab=0 + for tabstop in ${tabstops[@]} + do + # Strip trailing spaces. + field=${line:$prevtab:$((tabstop-prevtab))} + field=${field%%*( )} + printf '%s\t' "$field" + prevtab=$tabstop + done + printf '%s\n' "${line:$prevtab}" +} + +## +# First line: +## +# The tab stops are where stretches of empty space end. +IFS=$'\n' +read -r firstline +declare -i skip=1 +for ((i=0;i<${#firstline};i++)) +do + case ${firstline:$i:1} in + \ ) + skip=0 + ;; *) + [[ $skip -eq 0 ]] && tabstops+=($i) + skip=1 + esac +done +#echo >&2 ${tabstops[@]} +line=$firstline +spitline + +## +# All other lines: +declare -i prevtab +while read -r line +do spitline +done @@ -0,0 +1,4 @@ +#!/bin/bash + +#sed 's/\t/ + /g'| bc -q +sed 's/\t/ + /g'| python3 -qqIi 2> /dev/null diff --git a/xmlbrowser b/xmlbrowser new file mode 100755 index 0000000..731b466 --- /dev/null +++ b/xmlbrowser @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +import sys +import xml.etree.ElementTree as ET + +from qtpy import QtGui, QtWidgets, QtCore +from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel + +def _make_item(element) -> QTreeWidgetItem: + r = QTreeWidgetItem([element.tag, None, element.text]) + r.addChildren([QTreeWidgetItem([None, name, value]) for name, value in element.attrib.items()]) + r.addChildren([_make_item(child) for child in element]) + return r + +def main(): + app = QApplication(sys.argv) + # TODO: Check for suitable arguments first. + # Parameter may be a Qt option. + if len(sys.argv) == 1: + print("Usage:", sys.argv[0], "[file …]", file=sys.stderr) + return + + items = [] + for arg in sys.argv[1:]: + with open(arg) as xmlfile: + xmltree = ET.parse(xmlfile) + fileItem = QTreeWidgetItem([arg]) + items.append(fileItem) + fileItem.addChildren([_make_item(xmltree.getroot())]) + + tree = QTreeWidget() + tree.setColumnCount(2) + tree.setHeaderLabels(["Element", "attribute", "text"]) + tree.insertTopLevelItems(0, items) + tree.show() + sys.exit(app.exec_()) # Main loop. + +if __name__ == '__main__': + main() diff --git a/yamlbrowser b/yamlbrowser new file mode 100755 index 0000000..464f6c6 --- /dev/null +++ b/yamlbrowser @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import sys +import yaml +from qtpy import QtGui, QtWidgets, QtCore +from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel + +def _make_children (parent:QTreeWidgetItem, d) -> QTreeWidgetItem: + """ + If the item is a container item, i.e. list or dict, + its contents are added as children to the parent. + + If the item is not a container item, + it becomes the value to its parent. + """ + def transform_l (p:QTreeWidgetItem, d:list) -> QTreeWidgetItem: + p.addChildren (_make_children (QTreeWidgetItem (), x) for x in d) + return p + def transform_d (p:QTreeWidgetItem, d:dict) -> QTreeWidgetItem: + p.addChildren (_make_children (QTreeWidgetItem ([key]), value) for key, value in d.items ()) + return p + return { + list: lambda p, d : transform_l (p, d) + , dict: lambda p, d : transform_d (p, d) + , str: lambda p, d : QTreeWidgetItem ([p.text (0), d]) + , int: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)]) + , float: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)]) + , bool: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)]) + }[type (d)] (parent, d) + +def main (): + app = QApplication (sys.argv) + # TODO: Check for suitable arguments first. + # Parameter may be a Qt option. + if len (sys.argv) == 1: + print ("Usage:", sys.argv[0], "[file …]", file=sys.stderr) + return + try: + items = [] + for arg in sys.argv[1:]: + with open (arg) as file: + data = list (yaml.safe_load_all (file)) + items.append (_make_children (QTreeWidgetItem ([arg]), data[0] if len (data) == 1 else data)) + + tree = QTreeWidget () + tree.setColumnCount (2) + tree.setHeaderLabels (["Key", "Value"]) + tree.insertTopLevelItems(0, items) + tree.show () + except Exception as x: + print (x, file=sys.stderr) + label = QtWidgets.QLabel (str(x)) + label.show () # Opens window. + sys.exit (app.exec_()) # Main loop. Display the label. + +if __name__=='__main__': + main () |