I finally found a solution to this that doesn’t require manual interfaces thanks to @jdstrand!
In my bash install hook (running as root):
#!/bin/bash
mkdir -m 3777 $SNAP_COMMON/users
In my bash remove hook (running as root):
#!/bin/bash
find $SNAP_COMMON/users -maxdepth 1 -mindepth 1|while read filepath; do
if [[ $filepath == *.appvar ]];
then
while IFS= read -r datastring
do
echo "$datastring"
done < "$filepath"
fi
done
And in my python code (running as the user):
import subprocess
subprocess.call('umask 027', shell=True)
subprocess.call('echo "' + my_variable.strip() + '" > $SNAP_COMMON/users/$(id -u).appvar', shell=True)