forked from epandurski/cmbarter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_language.py
executable file
·99 lines (83 loc) · 2.46 KB
/
set_language.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python
from __future__ import with_statement
import sys, getopt
from cmbarter.settings import CMBARTER_DSN
from cmbarter.modules import curiousorm
USAGE = """Usage: set_language.py LANGUAGE
Set database text search language.
-l, --list display all supported languages
-h, --help display this help and exit
--dsn=DSN give explicitly the database source name
Example:
$ ./set_language.py bg
"""
LANGUAGES = """\
en
bg"""
def parse_args(argv):
global dsn, language
try:
opts, args = getopt.gnu_getopt(argv, 'hl', ['dsn=', 'help', 'list'])
except getopt.GetoptError:
print(USAGE)
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print(USAGE)
sys.exit()
elif opt in ('-l', '--list'):
print(LANGUAGES)
sys.exit()
elif opt == '--dsn':
dsn = arg
if len(args) == 1:
language = args[0]
else:
print(USAGE)
sys.exit(2)
def set_language_en(db):
db.execute("""
ALTER DATABASE cmbarter
SET default_text_search_config TO 'english';
""")
def set_language_bg(db):
try:
db.execute("""
CREATE TEXT SEARCH DICTIONARY bulgarian_ispell (
TEMPLATE = ispell,
DICTFILE = bulgarian,
AFFFILE = bulgarian,
STOPWORDS = bulgarian
);
""")
except curiousorm.PgError:
pass
try:
db.execute("""
CREATE TEXT SEARCH CONFIGURATION public.bulgarian (
COPY = pg_catalog.russian
);
""")
except curiousorm.PgError:
pass
db.execute("""
ALTER TEXT SEARCH CONFIGURATION bulgarian
ALTER MAPPING FOR word, hword, hword_part WITH bulgarian_ispell, simple;
""")
db.execute("""
ALTER DATABASE cmbarter
SET default_text_search_config TO 'public.bulgarian';
""")
if __name__ == "__main__":
dsn = CMBARTER_DSN
parse_args(sys.argv[1:])
db = curiousorm.Connection(dsn, dictrows=True)
try:
if language.lower() == "en":
set_language_en(db)
elif language.lower() == "bg":
set_language_bg(db)
else:
print("ERROR: {} is not supported.".format(language))
finally:
db.close()