From 70cec103a716721011f543d4957284713460877c Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Thu, 17 Jul 2025 11:31:32 +0200 Subject: [PATCH] [ini] --- .gitignore | 1 + source/main.py | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/build | 15 +++++ tools/install | 16 +++++ 4 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 source/main.py create mode 100755 tools/build create mode 100755 tools/install diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/source/main.py b/source/main.py new file mode 100644 index 0000000..7683e22 --- /dev/null +++ b/source/main.py @@ -0,0 +1,178 @@ +import sys as _sys +import json as _json +import yaml as _yaml +import argparse as _argparse + + +def convey( + x, + fs +): + y = x + for f in fs: + y = f(y) + return y + + +def string_coin( + template, + arguments, + options = None +): + options = ( + { + "character_open": "{{", + "character_close": "}}" + } + | + (options or {}) + ) + result = template + for (key, value, ) in arguments.items(): + result = result.replace( + ( + "%s%s%s" + % ( + options["character_open"], + key, + options["character_close"], + ) + ), + value + ) + return result + + +def file_read( + path +): + handle = open(path, "r") + content = handle.read() + handle.close() + return content + + +def handle_value( + value, + options = None +): + options = ( + { + "indicator": "@", + } + | + (options or {}) + ) + return ( + file_read(value[len(options["indicator"]):]) + if + value.startswith(options["indicator"]) + else + value + ) + + +def data_from_file( + path +): + return ( + {} + if + (path is None) + else + ( + ( + lambda content: ( + _json.loads(content) + if + path.endswith(".json") + else + _yaml.safe_load(content) + ) + ) (file_read(path)) + ) + ) + + +def data_from_arguments( + arguments +): + return dict( + map( + lambda argument: ( + ( + lambda parts: ( + parts[0], + handle_value(parts[1]), + ) + ) (argument.split(":", 1)) + ), + (arguments or {}) + ) + ) + + +def main( +): + ## args + argument_parser = _argparse.ArgumentParser( + prog = "coin", + formatter_class = _argparse.ArgumentDefaultsHelpFormatter, + description = "transforms an input string (stdin) by processing its placeholders; example: »echo '{{flowers}} are {{color}}' | coin -a flowers:roses -a color:red«", + ) + argument_parser.add_argument( + "-d", + "--data-path", + type = str, + default = None, + metavar = "", + help = "path to JSON or YAML file, containing placeholder assignments (e.g. »{\"flowers\": \"roses\", \"color\": \"red\"}«)", + ) + argument_parser.add_argument( + "-a", + "--argument", + type = str, + dest = "arguments", + action = "append", + default = None, + metavar = ":", + help = "prefixing with '@' causes the value to be interpreted as the path to a text file, whose content shall be used", + ) + argument_parser.add_argument( + "-o", + "--character-open", + type = str, + default = "{{", + metavar = "", + help = "placeholder opening character", + ) + argument_parser.add_argument( + "-c", + "--character-close", + type = str, + default = "}}", + metavar = "", + help = "placeholder closing character", + ) + args = argument_parser.parse_args() + + ## exec + content_in = _sys.stdin.read() + data = ( + data_from_file(args.data_path) + | + data_from_arguments(args.arguments) + ) + content_out = string_coin( + content_in, + data, + { + "character_open": args.character_open, + "character_close": args.character_close, + } + ) + _sys.stdout.write(content_out) + + +main() + diff --git a/tools/build b/tools/build new file mode 100755 index 0000000..83c6bf7 --- /dev/null +++ b/tools/build @@ -0,0 +1,15 @@ +#!/usr/bin/env sh + +## consts + +dir_source="source" +dir_build="build" + + +## exec + +mkdir -p ${dir_build} +echo "#!/usr/bin/env python3" > ${dir_build}/coin +cat ${dir_source}/main.py >> ${dir_build}/coin +chmod +x ${dir_build}/coin + diff --git a/tools/install b/tools/install new file mode 100755 index 0000000..8133aa5 --- /dev/null +++ b/tools/install @@ -0,0 +1,16 @@ +#!/usr/bin/env sh + +## consts + +dir_build="build" + + +## args + +if [ $# -ge 1 ] ; then dir_target=$1 ; else dir_target=/usr/local/bin ; fi + + +## exec + +cp ${dir_build}/coin ${dir_target}/coin +