Moved some stuff around.
[jben2_gui.git] / python / jben / dict.py
bloba71b6a4f82c1fac241e61f315c071a534a28dcce
1 # -*- coding: utf-8 -*-
3 from __future__ import absolute_import
6 import os
7 from jben import configure
8 import jblite.kd2, jblite.jmdict
11 class DictManager(object):
13 """Dictionary manager class."""
15 def __init__(self, app):
16 self.app = app
17 self.kd2 = None
18 self.jmdict = None
19 self.find_databases()
21 def get_dict_directory(self):
22 return configure.get_datadir()
24 def find_databases(self):
25 datadir = self.get_dict_directory()
27 kd2_path = os.path.join(datadir, "kd2.db")
28 if os.path.exists(kd2_path):
29 # Load an existing database.
30 self.kd2 = jblite.kd2.Database(kd2_path)
31 else:
32 # Try to create the database.
33 kd2_xml_path = os.path.join(datadir, "kanjidic2.xml.gz")
34 if os.path.exists(kd2_xml_path):
35 print "Creating KANJIDIC2 SQLite database; please wait..."
36 self.kd2 = jblite.kd2.Database(kd2_path,
37 init_from_file=kd2_xml_path)
38 else:
39 self.kd2 = None
41 jmdict_path = os.path.join(datadir, "jmdict.db")
42 if os.path.exists(jmdict_path):
43 self.jmdict = jblite.jmdict.Database(jmdict_path)
44 else:
45 jmdict_xml_path = os.path.join(datadir, "JMdict.gz")
46 if os.path.exists(jmdict_xml_path):
47 print "Creating JMdict SQLite database; please wait..."
48 self.jmdict = jblite.jmdict.Database(
49 jmdict_path, init_from_file=jmdict_xml_path)
50 else:
51 self.jmdict = None
53 def all_dicts_found(self):
54 return (self.kd2 is not None) and (self.jmdict is not None)
56 def jmdict_found(self):
57 return (self.jmdict is not None)
59 def kd2_found(self):
60 return (self.kd2 is not None)
62 def get_needed_dict_names(self):
63 """Returns a list of file names of any missing dictionary files."""
64 needed = []
65 if not self.kd2_found():
66 needed.append("kanjidic2.xml.gz")
67 if not self.jmdict_found():
68 needed.append("JMdict.gz")
69 return needed