python-tools.sh: Code beautification

This commit beautifies module import path deduction a little. It also adds
(disabled) code for importing submodules, which may or may not be enabled by a
command-line option in the future.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2019-10-28 12:20:52 +00:00
commit a935715036

View file

@ -10,29 +10,40 @@ usage()
[ "$1" ] && exit $1
}
module_path()
{
if [ "$module" ]; then
echo $module.$1
return
fi
echo $1
}
cmd_create_init()
{
local import_submodules=0
local e f files base extracted module_path
local del="-------------------------- generated by $myname"
echo "# >> $del >>"
files="$*"
for f in $files; do
test -d $f && continue
base=${f##*/}
base=${base%.py}
if [ "$module" ]; then
module_path=$module.$base
else
module_path=$base
fi
if [ "$sed_extract_command" ]; then
#echo running $sed_extract_command on $f
extracted=`sed "$sed_extract_command" $f`
#extracted="$sed_extract_command"
for e in $extracted; do
echo "from $module_path import $e"
echo "from `module_path $base` import $e"
done
fi
done
if [ "$import_submodules" = 1 ]; then
for f in $files; do
[ -f $f/__init__.py ] && echo "import `module_path $f` as $f"
done
fi
echo "# << $del <<"
}