Various scripts and pieces of code. Some of them may even be interesting.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.2 KiB

#!/bin/bash
# Update Protégé configuration for ODK-style ID ranges
# Damien Goutte-Gattat <dpg44@cam.ac.uk>
#
# /!\ HACK ALERT /!\
# This script is a hideous hack intended to workaround a current
# limitation of Protégé, which does not support ontology-specific
# settings.
#
# Assuming that:
# - you are working on MacOs X;
# - all your ontologies have a <name>-idranges.owl file, describing ID
# ranges to be used by editors;
#
# then you should be able to use this script to automatically update the
# configuration of Protégé to use your assigned ID range for a given
# ontology. Invoke this script with the path to the -idranges.owl file
# for the ontology you want to work with, followed by your name as it
# appears in the -idranges.owl file (on the "allocatedto" line). Then
# re-start Protégé if it was already running.
#
# Protégé 5.6 will have built-in support for per-ontology ID ranges and
# will send that script back into the Cracks of Doom from where it
# should never have emerged. In the meantime, use it at your own risks.
#
# "One does not simply edit Protégé's configuration files."
# -- Boromir of Gondor
prefs_file=$HOME/Library/Preferences/protege_preferences.application_preferences.org.protege.editor.owl.entity.creation.plist
property=/PROTEGE_PREFERENCES/application_preferences/org.protege.editor.owl.entity.creation/
die() {
echo "${0##*/}: $@" >&2
exit 1
}
usage() {
cat <<EOH
Usage: ${0##*/} RANGE_FILE EDITOR
Configure Protégé to use the ID range for the specified EDITOR in the
provided RANGE_FILE.
EOH
exit 1
}
range_file=$1
editor=$2
[ -n "$range_file" ] || usage
[ -n "$editor" ] || usage
[ -f "$range_file" ] || die "Range file not found"
[ -f "$prefs_file" ] || die "Protégé configuration file not found"
# Get ID parameters from the range file
prefix=$(sed -nre 's#^ idprefix: ".+/([^/]+)_",$#\1#p' $range_file)
digits=$(sed -nre 's#^ iddigits: ([0-9]+)$#\1#p' $range_file)
[ -n "$prefix" ] || die "Cannot get prefix from range file"
[ -n "$digits" ] || die "Cannot get number of digits from range file"
# Get ID range for specified editor
range_line=$(grep -A 3 "allocatedto: \"$editor\"" $range_file | tail -n 1)
[ -n "$range_line" ] || die "Cannot find range for $editor in range file"
range_start=$(echo $range_line | sed -nre 's/xsd:integer\[>= 0*([0-9]+) , <= [0-9]+\]/\1/p')
range_end=$(echo $range_line | sed -nre 's/xsd:integer\[>= [0-9]+ , <= 0*([0-9]+)\]/\1/p')
[ -n "$range_start" ] || die "Cannot find range lower bound for $editor"
[ -n "$range_end" ] || die "Cannot find range upper bound for $editor"
# Get user to confirm
cat <<EOS
This script will configure Protégé with the following settings:
Prefix: ${prefix}_
Number of digits: $digits
Range: $range_start - $range_end
EOS
read -p "Proceed (y/N)? " answer
[ "$answer" = y ] || die "Aborting"
# Proceed, then
/usr/libexec/Plistbuddy -c "SET :$property:AUTO_ID_START $range_start" $prefs_file
/usr/libexec/Plistbuddy -c "SET :$property:AUTO_ID_END $range_end" $prefs_file
/usr/libexec/Plistbuddy -c "SET :$property:AUTO_ID_PREFIX ${prefix}_" $prefs_file
/usr/libexec/Plistbuddy -c "SET :$property:AUTO_ID_SIZE $digits" $prefs_file
defaults read $prefs_file >/dev/null