streamlet-examples/exiv2
author František Kučera <franta-hg@frantovo.cz>
Wed, 29 Jan 2020 00:58:37 +0100
branchv_0
changeset 70 018e2609f5bb
parent 50 22ed5647b235
permissions -rwxr-xr-x
streamlets: move NULL handling from particular streamlets to StreamletAttributeFinder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, version 3 of the License.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# GNU General Public License for more details.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    19
# This streamlet provides EXIF metadata. It calls the tool exiv2.
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    20
# With no options it returns "Image size", "Copyright" and "Exif comment" attributes.
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    21
# Specific attributes can be selected using options – e.g. --option 'attribute' 'Image size'
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    22
# List of available attributes can be obtained by directly calling the exiv2 command on a image file.
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    23
# Two additional attributes are provided by this streamlet: "Image height" and "Image width" (they are extracted from "Image size").
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
50
22ed5647b235 streamlets: include streamlet-common.sh from the directory where the actual streamlet resides (not where is the symlink, if any)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    26
. "$(dirname "$(realpath "$0")")/streamlet-common.sh"
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    29
	streamletFields=()
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    30
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    31
	for (( i=0; i<${#optionNames[@]}; i++)); do
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    32
		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    33
			streamletFields+=("${optionValues[$i]}");
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    34
		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    35
			pdfPrefix="${optionValues[$i]}";
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    36
		else
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    37
			echo "Unsupported option: ${optionNames[$i]}" >&2
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    38
		fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    39
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    40
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    41
	if [[ -z "$streamletFields" ]]; then
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    42
		streamletFields=( "Image size" "Copyright" "Exif comment" );
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    43
	fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    44
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    45
	for (( i=0; i<${#streamletFields[@]}; i++)); do
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    46
		if [[ "x${streamletFields[$i]}" == "xImage height" ]] ||[[ "x${streamletFields[$i]}" == "xImage width" ]]; then local type="integer"; else local type="string"; fi
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    47
		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}"    "$type"
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    48
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    49
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	send WAITING_FOR_INPUT_ATTRIBUTES
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    54
	local streamletInfo streamletValid value isNull;
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    55
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    56
	[[ -d "$currentFile" ]] || streamletInfo="$(exiv2 "$currentFile")";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    57
	streamletValid="$?";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    58
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    59
	for (( i=0; i<${#streamletFields[@]}; i++)); do
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    60
		value="$(echo "$streamletInfo" | grep -P "^\Q${streamletFields[$i]}\E\s*:" | sed -E 's/[^:]+:\s+(.*)/\1/g' | tr -d '\n';)"; # the field name must not contain "\E"
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    61
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    62
		if   [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage width"  ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\1/g')";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    63
		elif [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage height" ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\2/g')";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    64
		fi
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    65
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    66
		# n.b. if file has no exif data, exiv2 exits with error „No Exif data found in the file“ and thus $streamletValid != 0, but there still might be some value like „Image size“
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    67
		if   [[ ! "x$streamletValid" == "x0" ]] && [[ "x$value" == "x" ]]; then value="";  isNull="true";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    68
		else                                                                               isNull="false";
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    69
		fi
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    70
70
018e2609f5bb streamlets: move NULL handling from particular streamlets to StreamletAttributeFinder
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    71
		if ( [[ "x${streamletFields[$i]}" == "xImage height" ]] || [[ "x${streamletFields[$i]}" == "xImage width" ]] ) && [[ ! "$value" =~ ^[0-9]+$ ]]; then isNull="true"; fi
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    72
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    73
		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    74
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    75
	
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	send WAITING_FOR_INPUT_ATTRIBUTES;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
initialize
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
processMessages