Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
db_window.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk
2 import os
3 from db.bll_database import BLLDatabase, DBConstants
4 from utils.progress_dialog import ProgressDialog
5 from utils.ui_utils import UIUtils
6 from utils.naptime import Naptime
7 
8 class DBWindow():
9  def __init__(self):
10  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
11  self.window.set_title('Naptime Filter')
12  self.window.connect('destroy', lambda x: self.window.destroy())
13  self.window.set_border_width(10)
14  self.window.set_default_size(210, 150)
15 
16  self.build_window()
17 
18  self.window.show_all()
19 
20  def build_window(self):
21  vbox = gtk.VBox()
22 
23  last_update_label = gtk.Label()
24  markup = 'None'
25  if DBConstants.SETTINGS.LAST_NAPTIME_UPDATE:
26  markup = UIUtils.utc_to_local_str(DBConstants.SETTINGS.LAST_NAPTIME_UPDATE)
27  last_update_label.set_markup('Last Update: <b>%s</b>' % (markup))
28  vbox.pack_start(last_update_label, True, True, 0)
29 
30  last_path_label = gtk.Label()
31  last_path_label.set_markup('Last Path: <b>%s</b>' % (str(DBConstants.SETTINGS.LAST_NAPTIME_FOLDER)))
32  vbox.pack_start(last_path_label, True, True, 0)
33 
34  hbox = gtk.HBox()
35  folder_label = gtk.Label('Naptime Folder:')
36  folder_entry = gtk.Entry()
37  folder_entry.set_width_chars(50)
38  browse_button = gtk.Button('Browse')
39  browse_button.connect('clicked', lambda w: UIUtils.browse_folder('Select naptime folder', folder_entry))#, [UIUtils.CSV_FILE_FILTER, UIUtils.ALL_FILE_FILTER]))
40  hbox.pack_start(folder_label, True, False, 0)
41  hbox.pack_start(folder_entry, True, False, 0)
42  hbox.pack_start(browse_button, True, False, 0)
43  vbox.pack_start(hbox, True, False, 0)
44 
45  button_box = gtk.HButtonBox()
46  button_box.set_layout(gtk.ButtonBoxStyle.EDGE)
47  cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL, label='Cancel')
48  cancel_button.connect('clicked', lambda w: self.window.destroy())
49  button_box.add(cancel_button)
50 
51  update_button = gtk.Button(stock=gtk.STOCK_OK, label='Update')
52  update_button.connect('clicked',
53  lambda w: self.update_db(folder_entry.get_text())
54  )
55  button_box.add(update_button)
56 
57  vbox.pack_end(button_box, True, True, 0)
58 
59  self.window.add(vbox)
60 
61  def update_db(self, path):
62  prog_diag = ProgressDialog(title='Processing...', phases=['Please Wait'])
63  prog_diag.show()
64 
65  db = BLLDatabase()
66  error_filenames = Naptime.update_naptime_data(db, path, prog_diag=prog_diag)
67  db.close()
68  prog_diag.ensure_finish()
69 
70  if error_filenames:
71  UIUtils.show_message_dialog(
72  'Unable to process the following files - see the log file for details:\n' +
73  '\n'.join(map(os.path.basename, error_filenames)),
74  dialog_type=gtk.MessageType.ERROR
75  )
76 
77  else:
78  UIUtils.show_message_dialog('Naptime database table updated successfully.')
79  self.window.destroy()