streamlet examples: common functions + inode, lines_count, mime_type v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 11 Jan 2020 19:15:58 +0100
branchv_0
changeset 33 f9cada1d46a4
parent 32 bccda5688d71
child 34 0b9e4af08cc8
streamlet examples: common functions + inode, lines_count, mime_type
streamlet-examples/__relpipe_in_filesystem_script_inode
streamlet-examples/__relpipe_in_filesystem_script_lines_count
streamlet-examples/__relpipe_in_filesystem_script_mime_type
streamlet-examples/streamlet-common.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/__relpipe_in_filesystem_script_inode	Sat Jan 11 19:15:58 2020 +0100
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# This streamlet provides a single attribute: inode number of given file
+
+
+. "$(dirname $0)/streamlet-common.sh"
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
+	send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[0]}"    "integer"
+	send WAITING_FOR_INPUT_ATTRIBUTES
+}
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
+	value=$(ls -d -i "$currentFile" | cut -d" " -f1);
+	send OUTPUT_ATTRIBUTE "$value"    "false";
+	send WAITING_FOR_INPUT_ATTRIBUTES;
+}
+
+initialize
+processMessages
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/__relpipe_in_filesystem_script_lines_count	Sat Jan 11 19:15:58 2020 +0100
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# This streamlet provides a single attribute: number of lines of given file
+# Standard wc -l is used to count the lines.
+# Directories are reported a 0 lines and with a null flag (will be supported in further Relational pipes versions).
+
+
+. "$(dirname $0)/streamlet-common.sh"
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
+	send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[0]}"    "integer"
+	send WAITING_FOR_INPUT_ATTRIBUTES
+}
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
+	if [[ -d "$currentFile" ]]; then
+		value="0";
+		isNull="true";
+	else
+		value=$(wc -l "$currentFile" | cut -d" " -f1);
+		isNull="false";
+	fi
+	
+	send OUTPUT_ATTRIBUTE "$value"    "$isNull";
+	send WAITING_FOR_INPUT_ATTRIBUTES;
+}
+
+initialize
+processMessages
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/__relpipe_in_filesystem_script_mime_type	Sat Jan 11 19:15:58 2020 +0100
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# This streamlet provides a single attribute: inode number of given file
+
+
+. "$(dirname $0)/streamlet-common.sh"
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
+	send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[0]}"    "string"
+	send WAITING_FOR_INPUT_ATTRIBUTES
+}
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
+	value=$(file --preserve-date --brief --mime-type --dereference "$currentFile");
+	send OUTPUT_ATTRIBUTE "$value"    "false";
+	send WAITING_FOR_INPUT_ATTRIBUTES;
+}
+
+initialize
+processMessages
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/streamlet-common.sh	Sat Jan 11 19:15:58 2020 +0100
@@ -0,0 +1,116 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# Unlike the protocol and the message format,
+# these helper functions are not part of the public API.
+# Thus when writing custom streamlets, it is better to copy this file
+# and review its changes while upgrading to new upstream version.
+
+
+send() {
+	local msgVar="EXEC_MSG_$1";
+	printf '%s\0' "${!msgVar}"
+	shift;
+	printf '%s\0' "$#"
+	for param in "$@"; do printf '%s\0' "$param"; done 
+}
+
+read_nullbyte() { local IFS=; for v in "$@"; do export "$v"; read -r -d '' "$v"; done }
+
+processMessages() {
+	while read_nullbyte code length; do
+		local msgVar="EXEC_MSG_$code";
+		local functionName="processMessage_${!msgVar}";
+		local parameters=();
+		for (( i=0; i<$length; i++)); do
+			read_nullbyte parameter;
+			parameters+=("$parameter")
+		done
+		type "$functionName" &>/dev/null || functionName="processUnsupportedMessage";
+		"$functionName" "${parameters[@]}";
+	done
+}
+
+processUnsupportedMessage() {
+	echo "Unsupported message: $*" >&2
+	# TODO: send error, no debug
+	# send STREAMLET_ERROR "UNSUPPORTED_MESSAGE"
+}
+
+initialize() {
+	versionsSupported=();
+	optionNames=();
+	optionValues=();
+	inputAttributeNames=();
+	inputAttributeTypes=();
+	outputAttributeAliases=();
+	currentInputAttributeIndex=0;
+	currentOutputAttributeIndex=0;
+	currentRelation="";
+	currentFile="";
+}
+
+processMessage_VERSION_SUPPORTED() {
+	versionsSupported+=("$1");
+}
+
+processMessage_WAITING_FOR_VERSION() {
+	for v in "${versionsSupported[@]}"; do
+		if [[ "x$v" == "x1" ]]; then
+			send VERSION_ACCEPTED 1;
+			return;
+		fi
+	done
+	send STREAMLET_ERROR "INCOMPATIBLE_VERSION" "Only version 1 is supported by this streamlet."
+}
+
+processMessage_RELATION_START() {
+	currentRelation="$1";
+	return;
+}
+
+processMessage_INPUT_ATTRIBUTE_METADATA() {
+	inputAttributeNames+=("$1");
+	inputAttributeTypes+=("$2");
+}
+
+processMessage_OUTPUT_ATTRIBUTE_ALIAS() {
+	outputAttributeAliases+=("$1");
+}
+
+processMessage_OPTION() {
+	optionNames+=("$1");
+	optionValues+=("$2");
+}
+
+processMessage_INPUT_ATTRIBUTE() {
+	local index="$1";
+	local value="$2";
+	local isNull="$3";
+	if [[ "_${inputAttributeNames[$index]}" == "_path" ]]; then
+		currentFile="$value";
+	fi
+}
+
+processMessage_RELATION_END() {
+	# Now we should stop reading messages,
+	# but the executor also closes our STDIN,
+	# so the loop in processMessages() terminates.
+	# Override this function to do some clean-up.
+	return;
+}