update-python-identifiers.sh 988 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/bin/sh
#
# Author:  Colomban Wendling <colomban@geany.org>
# License: GPL v2 or later
#
# Updates the `identifiers` entry in data/filetypes.python.
# Requires both Python 2 and 3.

set -e

11
file=data/filedefs/filetypes.python
12 13 14

[ -f "$file" ]

15 16 17
py_2_and_3() {
  python2 "$@" && python3 "$@"
}
18

19 20 21
# sort_filter [exclude...]
sort_filter() {
  python -c '\
22
from sys import stdin; \
23 24 25 26 27 28
items=set(stdin.read().strip().split("\n")); \
exclude=['"$(for a in "$@"; do printf "'%s', " "$a"; done)"']; \
print(" ".join(sorted([i for i in items if i not in exclude])))
'
}

29
keywords=$(py_2_and_3 -c 'from keyword import kwlist; print("\n".join(kwlist))')
30 31
builtins=$(py_2_and_3 -c 'print("\n".join(dir(__builtins__)))')

32
primary=$(echo "$keywords" | sort_filter)
33
# builtins, but excluding keywords that are already listed in primary=
34
identifiers=$(echo "$builtins" | sort_filter $primary)
35

36 37 38
sed -e "s/^primary=.*$/primary=$primary/" \
    -e "s/^identifiers=.*$/identifiers=$identifiers/" \
    -i "$file"